Let's Make Robots!

Controlling Picaxe microcontrollers with a computer or let them communicate

Controlling Picaxe microcontrollers with a computer or let them communicate

On general demand (ok, Fritsl asked me...), I will show and explain you how to let several Picaxe microcontrollers communicate with each other or how to control a picaxe microcontroller with your computer.

This is actually fairly easy with the serrxd and sertxd commands. Serrxd reads the serial in port and sertxd writes to the serial out port. Now, when you check the Picaxe manual for these commands you will see this:

SERRXD (qualifier,qualifier...)
SERRXD (qualifier,qualifier...),{#}variable,{#}variable...
SERRXD {#}variable,{#}variable...

 What this means is:

  • Qualifier: when this is used, the Picaxe will wait until it reads the value that has been entered as qualifier and then continue with its operation
  • Variable: b0... you know these ;)

Little example: serrxd ("LMR"),b1 will wait until it reads the letters LMR and then put the first byte after thos three letters in b1.

 For sertxd you find:

SERTXD ({#}data,{#}data...)

 This means:

  • Data: this can be anything... Sentences (always place text that needs te be read as text by the receiver between ""), variables (b0, w1 and so on) or special orders (more on that later)


Now, to control the outputs on a Picaxe 28X1 I used this code:


    serrxd("dev0"),b0,b1                'Read serial in port and wait for string dev0 then put next two bytes in b0 and b1
    let b2=b1-48
    if b0 = 49 then
    high b2
    sertxd("Light ",b1," switched on. ")    'Send a message to serial out port
    else if b0 = 48 then
    low b2
    sertxd("Light ",b1," switched off. ")
    sertxd("Waiting for command...",13,10)
    goto main


(I used this codes to turn on LEDs that's why you see "light switched on/off")

Now, I think some explanation wouldn't be bad...

  • serrxd("dev0"),b0,b1 ==> Reads the serial in port. The qualifier "dev0" makes sure the controller waits until dev0 is read on the serial in port. After it has read the qualifier the command will put the next two bytes the controller receives in variables b0 and b1.
  • sertxd("Light ",b1," switched on. ") ==> Sends 3 messages to the serial out port, when received by the terminal in the programming editor on the computer these 3 messages will be displayed right next to each other and form a sentence
  • sertxd("Waiting for command...",13,10) ==> Sends the message Waiting for command... to the serial out port. 13  and 10 are special commands and will be explained later on.

This program will do the following: when I send dev011 the controller will enable output one, when I send dev001 the controller will disable output one. This can be doen for outputs 0 till 7 by altering the last number in the code that is being sent.


Now, before you start programming you controller, read this paragraph very carefully! The serrxd and sertxd don't work with numbers but with ASCII values... This means that when dev012 is sent to the controller running the program above, the controller will put value 49 in b0 and value 50 in b1. This is because the character 1 is value 49 in the ASCII table. This also means that it is possible to send special function: if you look at ASCII value 13 you will see that it corresponds to the character 'carriage return', this makes sure the next characters will be displayed at the beginning of the line. ASCII value 10 corresponds to 'line feed' and places the cursor on a new line. So if you follow the logic, executing the code sertxd(13,10) will start a new line at the beginning (or do the same as if you would hit 'return' when typing in a text editor).

This also implies that you can't use high b1 straight away in the program above, because that would try to enable output 48 or higher  (character zero corresponds to ASCII value 48, char. 7 to 55). That's why I've put in a line of code that subtracts 48 out of b1 and puts the result in b2, that way I can use the variable b2 to enable or disable outputs and variable b1 to give feedback to the computer.


Now, if you apply the same logic to two Picaxe microcontrollers instead of a Picaxe and a PC, you can let the microcontrollers communicate with each other.

The Picaxe microcontrollers also have the commands serin and serout, these do exactly the same as serrxd and sertxd but let you decide what pin to use. 

Any questions will be answered with pleasure.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

It should be noted that the serrxd/txd function does not work for all of the picaxes while the serin/serout do. You also are limited to a set baud rate where serin/serout can be defined by the user. Last point is that the you can use a timeout feature on certian higher end picaxes which will prevent your axe from sitting and waiting indefinately.

 Can you also clarify this point:  The serrxd and sertxd don't work with numbers but with ASCII values...

The manual seems to state otherwise as far as how to send raw vs ascii characters.
I know the manual says that you can add #'s to work with raw characters, but that doesn't work with me... So if anyone can tell me how to work with the #'s that'd be great :)

I can test this on a 08m, 18x and 14m this eve. I don't have a 28x though.

Maybe it's an issue with the proc firmware?

I does not work with my 08M

apologies if this is a noob question - but I assume then that you're interacting with the picaxe via axepad? If not - what software are you using?

I'm looking for a way to let picaxe communicate with my MAC via wireless.. yep, mac. (in my defense; it's not my mac, and getting anything else in this country is too difficult and expensive!). 

When you say wireless, are you saying WIFI or any type including bluetooth.

If so, you should look at processing. It works pretty well with the bluetooth library. 

I use processing with the BT lib to communicte with a picaxe at the moment....just experimenting, but I have been playing around with an echo terminal (send from comupter picaxe recieves and responds with the same char).

oh right. no I mean wifi. simply because I have a fistful of tx/rx modules on hand.

Can I just clarify VDB: you're interfacing linux with your picaxe? I wonder if I can replicate that on my mac? 

I haven't played with wifi as of yet, just RF and bluetooth so wouldn't be of much help in the way of connecting wifi..  :/

I am running processing on windows. the db is running on a linux box. I have an ssh tunnel to the linux box that forwards the local(windows) db port to the remote(linux) port....it sounds more complicated than it is.

You can replicate it by just running processing on your mac. I have it set up on my mac as well but I got the bt lib running on the mac as well which is one less thing to worry about . I would just need to set up the port forwarding on the mac to connect to the linux box for db interaction. just a simple ssh command.

What's wrong with a mac?

 Anyways, WIFI is fairly difficult to master, what with all the protocols and everything. Instead, I would just use two micros, one connected to the MAC and one transmitting... And if you want cheap, try some of these- http://www.sparkfun.com/commerce/product_info.php?products_id=8945



Actually a better way to read numbers in is to use the optional # before your variable. In your example this will read a 0 into b1 rather than 48, avoiding the arithmatic. So your code would be

serrxd ("dev0"),bo,b1