Picaxe Programming From Linux


In this article we will look at how to programme PICAXE microcontrollers from Linux. The Picaxe Programming Editor (v5) and the newer Picaxe Editor 6 are only available (at the time of writing) for Windows PCs. For anyone with a Mac or Linux machine, the only official software provided is AXEpad, a far more limited development environment.PICAXE programming from Linux using AXEpad

During a recent move from Windows XP to Linux, I had no trouble finding the same or equivalent software for everything I need to do – until finally I came to PICAXE. I have installed Ubuntu 13.10, Mint 16 Xfce, and Debian Wheezy 7.4 Linux on three different PCs, but I could not get AXEpad to run on any of them. Looking around on the internet, it seems that a lot of people have been experiencing similar issues.

With 7-8 years of experience programming PICAXE microcontrollers I only used the Picaxe Programming Editor to compile and download my code. I therefore decided to just use the command line on Linux to fulfil these functions having found out this is possible while trying to resolve the problems with AXEpad.

After lots of trawling around I came up with the following resources which seemed to give all the answers:
Setting up to Program a Picaxe via Shell Script
Use Picaxe Compiler with Gedit as an Alternative to AXEpad
PICAXE USB Cable Data Sheet.

Unfortunately, blindly following the instructions provided by each of those three resources in turn also did not bring success, but starting again with a mixture of the information led to success. Below are the steps I eventually took which worked for me on all three Linux distributions I tested it on – hopefully they will also work for you!

Download the Compilers

Each of the many PICAXE chips has its own compiler which takes the code (basic text file) you type in, compiles it into machine code which can be understood by the chips, and then downloads that code to the microcontroller. These compilers are available to download free of charge from Rev-Ed, the company behind PICAXE.

I created a new directory called ‘picaxe’ in my home directory (mkdir picaxe), moved into it (cd picaxe), and then downloaded the tarball (equivalent to a Zip file) of the compilers:

wget http://www.rev-ed.co.uk/software/picaxe.tgz

I then extracted the compilers with the command:

tar -xzvf picaxe.tgz

Checking the contents of the directory (ls), it was now full of the individual compiler files, e.g. picaxe08, picaxe08m, picaxe08m2, and so on – one for each chip in the PICAXE range.
I then created an empty text file called test.bas (touch test.bas) which was the file I planned to use to test if I could download to the picaxe chip successfully.

Download PIXAXE compilers for Linux

The PICAXE USB Download Cable

For Linux, when you plug the PICAXE USB download cable (AXE027) into your computer, you do not have to install any drivers. You do however have to change some settings in order to use it. I entered the following command:

sudo modprobe ftdi_sio vendor=0x0403 product=0xbd90

Then I plugged the USB cable into the PC. Entering the command:

ls /dev

the following file should be listed: ttyUSB0. This is the PICAXE USB cable file.

I then needed to set some ‘rules’ so that the PC knows what to do the next time the cable is plugged in and it detects it as being AXE027. To do this I create a file using the nano text editor entering the following command:

sudo nano /etc/udev/rules.d/99-axe027.rules

I then entered the following text into that file:

# /etc/udev/rules.d/99-axe027.rules
# contains axe027 udev rule to patch default
# FTDI product id to AXE027 product id (0xbd90)
ATTR{idProduct}=="bd90", ATTR{idVendor}=="0403", RUN+="/sbin/modprobe -q ftdi_sio product=0xbd90 vendor=0x0403"

(It mentioned in one of the resources referenced that for Ubuntu 12.404 or earlier, replace ATTR with SYSFS each time)

The referenced resource then said to enter the following to avoid having to reboot the system:

sudo udevcontrol reload_rules

which should load the new rule in, however, none of the three distributions knew what udevcontrol is so I just rebooted (sudo reboot).

Downloading to PIXAXE from Linux

So, now I had the compilers ready, and the USB cable connected between the PICAXE and the Linux box and operational. I fitted a Picaxe08M2 chip into the development board, and entered the following command (from within my ‘picaxe’ directory):

./picaxe08m2 -c/dev/ttyUSB0 test.bas

which did not run, so I instead tried

sudo ./picaxe08m2 -c/dev/ttyUSB0 test.bas

which did.

This compiled the code (my empty file test.bas), but then it threw out a permission denied error.
I double checked the PDF of the USB cable datasheet and saw that in an alternative option for programming PICAXE from Linux it mentioned the need to change the file permissions for the /dev/ttyUSB0 file using the command:

sudo chmod 777 /dev/ttyUSB0

(this seems to need to be done each time the USB cable is unplugged and plugged back in again or the system restarted)

I then tried to run the compiler again:

sudo ./picaxe08m2 -c/dev/ttyUSB0 test.bas

and the code compiled AND downloaded to the PICAXE chip successfully (as shown below).

Running PICAXE compiler on Linux

Converting Files from DOS to UNIX Format

So, so far seemingly so good. I then copied all of my Windows based PICAXE programming basic code files over to a new directory called picaxe-codes in the picaxe directory on the Linux machine and tried to run the compiler on a few of them, for example:

sudo ./picaxe08m2 -c/dev/ttyUSB0 picaxe-codes/NAME-OF-FILE.bas

Each time I immediately got the following error message during compiling:

Error: Illegal character: ('^M' (0x0D))

A quick check on google found this handy script:

sed -i -e 's/\r//' FILENAME.bas

which will turn MS-DOS (Windows type) carriage returns into UNIX style carriage returns for every line in the file FILENAME.bas to resolve this issue. I tested it on one of my files and it worked fine, compiled without errors, and downloaded to the chip as it was supposed to; so I modified the script to run on all of the picaxe programming files in my picaxe codes directory with the following:

find picaxe-codes/*.bas | xargs sed -i "s/\r//"

This did the job, and I can now use the following command to quickly get my code onto my PICAXE chips:

sudo ./picaxe08m2 -c/dev/ttyUSB0 picaxe-codes/NAME-OF-FILE.bas

Programming PICAXE from Raspberry Pi

As a side note, and so that you do not make the same mistake, I decided to set up a Raspberry Pi (also of course Linux based) to programme my PICAXE chips. I connected the PICAXE USB cable to my Pi and it rebooted itself automatically immediately. When I subsequently checked the contents of the /dev directory I could not find the expected ttyUSB0 file. Thinking something was awry (as I had entered the sudo modprobe ftdi_sio vendor=0x0403 product=0xbd90 command before the unwanted reboot, I ran dmesg and it showed that all was well in terms of detection of the USB device:

[ 3.631574] usb 1-1.3: New USB device found, idVendor=0403, idProduct=bd90
[ 3.649410] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 3.658428] usb 1-1.3: Product: AXE027 PICAXE USB
[ 3.678465] usb 1-1.3: Manufacturer: Revolution

So, I again typed in sudo modprobe…etc, did another ls /dev to check the ttyUSB0 file was there – and it was. I quickly did the chmod command on that file to set the required permissions:

sudo chmod 777 /dev/ttyUSB0

so as not to forget it later.

Then I again created a picaxe folder, downloaded the PICAXE compilers, extracted the files from the tarball, and created a blank test.bas file for testing. I set the rules as before and rebooted the Pi.

When I ran the usual command to compile test.bas and download the code to the PICAXE chip I got the following error message:

./picaxe08m2: 1: ./picaxe08m2: Syntax error: "(" unexpected

Then I (finally) remembered that the compiler files (binaries) I had downloaded were not made for ARM processor chips such as the one which powers the Raspberry Pi, and therefore they will not work – they are made for x86 processors of the type found in PCs.
(However, here is some information which makes it sound like it is possible: PICAXE Programming from Raspberry Pi, but of course not simple….)

Comments

this command:
sudo chmod 777 /dev/ttyUSB0
is not appropriate. Instead do:
ls -a /dev/ttyUSB0
which will output for example:
crw-rw—- 1 root dialout 188, 0 mars 5 11:07 /dev/ttyUSB0

check with the command “id” with the simple user (your_user)

If this command doesn’t read dialout, do:
adduser your_user dialout
and then try again.

For Picaxe and Debian Jessie or testing kernel 3.12 do
modprobe ftdi-sio
echo 0403 bd90 > /sys/bus/usb-serial/drivers/ftdi_sio/new_id

Pierre, France
4th March 2014