Tuesday 30 December 2014

Swinging the Picaxe on Linux

One of the more unusual gifts from Santa this year was a plastic bag containing a pcb, a USB cable and a small selection of components.


It must be 15 years since I found myself working in a development team alongside a crazy Picaxe fanatic ("hi Ian G").


I promised at the time that I would get familiar with these little critters one day. So I guess that day has finally arrived.


What is a Picaxe?


It's a British (Hooray!) microcontroller system pre-programmed with a Basic interpreter. This means that you can write simple (Basic-like) instructions on a regular computer, compile the code, then download it and run it on a Picaxe chip.

The chip range starts with an 8 pin device which costs around £2, and goes up in size (pins) and price (£). There's loads of free stuff on the Picaxe website including documentation and software.

The company behind all this is Revolution Education. Sadly, Linux is not one of their strong points, and I don't understand why their Linux code is not open source. After all, they make their money from hardware and give the software away. If the Linux stuff was open source it could be developed by keen, clever people "out there" and Rev-Ed would probably sell more stuff!

What's in the bag?


The pcb came from Spiratronics, but it is not the development board currently shown in the illustration on their webstore.

Picaxe download cable plus the Spiratronics kit


It is a pukka Picaxe board, but I searched the internet looking for it without success, so I had to draw the circuit myself.



This board is designed to provide one switch input, and 4 outputs for relays, motors or lights.

The assembled board plus l.e.d. and 470R resistor



But you don't really need this pcb to get started. You could use a small bread board, a couple of resistors, a capacitor and a 3.5mm jack socket....Oh! and a Picaxe.

Doing the biz on Lubuntu


So far so good, but I need a development environment on my Lubuntu 64bit laptop. And the first challenge is the AXE027 USB cable.

According to the documentation, it should be possible to use modprobe in a terminal command like this:-

sudo modprobe ftdi_sio vendor=0x0403 product=0xbd90

...which should then give you a device in /dev like this:-

/dev/ttyUSB0

But Linux does not work like this anymore. Or should I say it doesn't work for Linux kernel 3.13 and possibly one or two earlier releases. The vendor and product parameters appear to have been dropped.

So if you are running a 'buntu version from 14.04 onwards (or probably any Linux distro using kernel 3.13) you may need to do something like this:-

Connect the AXE027 to your Linux computer and run from a terminal:

 lsusb

You should see the cable listed as Future Technology Devices International:-

steve@steve-X401A1:~$ lsusb
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 04f2:b2e3 Chicony Electronics Co., Ltd
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 002: ID 0403:bd90 Future Technology Devices International, Ltd


Note the vendor id (0403) and product (bd90).

Run command:-

sudo modprobe ftdi_sio

Navigate with file manager to:-

/sys/bus/usb-serial/drivers/ftdi_sio/new_id

You can open new_id in a text editor to have a peek, but it will be a blank file.

Change file permissions so we can write to new_id.

 sudo chmod 777 /sys/bus/usb-serial/drivers/ftdi_sio/new_id
 sudo echo "0403 bd90" > /sys/bus/usb-serial/drivers/ftdi_sio/new_id

Opening new_id now should show:-

0403 bd90

If this malarkey has worked, you should now have in your file system: /dev/ttyUSB0

To test this, download the Picaxe Linux compilers, unzip and put them in a folder somewhere handy. In the same folder, create a blank file with a .bas extension (e.g. test.bas).

Launch a terminal from this folder (e.g. hit F4 from file manager) and then run this command:-

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

Hopefully you will see output like this:-

PICAXE-08M2 Compiler
Version 3.1
Copyright (c) 1996-2014
Revolution Education Ltd

Compiled successfully.
Memory used = 3 out of 2048 bytes.

Connect power now!
Searching for hardware on /dev/ttyUSB0.

line# 0, col# 0

Error: Hardware not found on /dev/ttyUSB0!


...which shows that the compiler works and your Linux box is happy with ttyUSB0.

Note: Using this process, this configuration is temporary (e.g. it works until we log-out or remove the AXE027 cable).

All we need now is to write a program and connect our cable to a Picaxe.

My first program


I have an 8 pin Picaxe (the 08m2) with an l.e.d. + 470R resistor connected to pin 7 via a transistor driver. Pin 7 is i/o number 0, so my first program is called testP0.bas and looks like this:-

main: high B.0
    pause 1000
    low B.0
    pause 3000
    goto main


...it won't win any prizes, but it should flash my l.e.d.

To compile and load, I navigate to the folder containing both the 08m2 compiler and the test file, and then press F4 (i.e. open a terminal at this point in the file system) and run:-

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

...the output looks like this:-

PICAXE-08M2 Compiler
Version 3.1
Copyright (c) 1996-2014
Revolution Education Ltd

Compiled successfully.
Memory used = 14 out of 2048 bytes.

Connect power now!
Searching for hardware on /dev/ttyUSB0.
Downloading program.
.................
Downloading data.
..................

PASS - programmed PICAXE-08M2 v4.A successfully.



...and my l.e.d. flashes for 1 second and is off for 3 seconds. Hooray!

The program is stored on my Picaxe, so I can disconnect the battery and the USB cable from my test board, and when power is reconnected, the program will run again.

Whatever next?


Now I need to make these changes to my laptop permanent (by creating dev rules) and I also need to think of some interesting things to do with my Picaxe.


See also: Picaxe Programming with the Raspberry Pi

No comments:

Post a Comment