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.

serrxd ("dev0"),bo,b1 is the code I used to communicate with the picaxe. It works with ASCII value's...


Now I've tried adding the #'s but then code doesn't work anymore, so if you could clarify how this should be used, that would be great...

Sorry, my bad, I meant 

serrxd ("dev0"),#bo,#b1


Actually I just realised I'm not using that for input, only output. Here's my code;

serout 4,T4800,(#w1,13)

 I'm using this for reading commands; 


serin 0,T4800,b1


if b1 = "2" then

gosub goforward

elseif b1 = "8" then

gosub goback

elseif b1 = "4" then

gosub turnleft

elseif b1 = "6" then

gosub turnright

elseif b1 = "5" then

gosub stopnow


Works for me.

I was actually using the # for reading in values before and I remember I had to send the # to the Picaxe before the actual number for it to work,  so for b1 to read 0 I'd have to send  #0 .

I tried that too ( sending #0 to receive 0 in b1) but that doesn't work either with me...
hey plz help me i dont know any thing about  robotics....
For people like you, who don't know anything about robotics there is a special post showing how to build your first robot... you can find it under "Start here"

M2GR I hope that this is appropriate for the site, it's not exactly robotics. I'm trying to hook a bluetooth module up to a telescope mount (in place of a hand controller)  and think a Picaxe 08M2 (which I have on hand) might be able to do the transform I need but I'm still trying to get up to speed on using the Picaxes.

The Bluetooth module has TX and RX lines, as far as I can tell the telescope mount uses a single data line and a flow control line which is normally high and is pulled low when either side wants to send a message to the other.

I need to be able to listen on for messages coming from the RX line of the bluetooth module (which would be connected back to a computer or android tablet) and a message is received pull the flow control line low then pass the message to the data line. If the flow control line is pulled low by the telescope mount then the message being received from the data line would be sent on to the TX line of the Bluetooth module. Hope that makes sense.

I gather that an interupt on the flow control line would handle the data coming from the telescope mount (I assume that I'd just do some kind of passthrough routine, read a byte, write a bit) but I'm I'm not sure how to handle the pass through of data coming from the RX line of the bluetooth module. I read elsewhere that issuing a serin command leaves the picaxe unable to do anything else (and got the impression that included interupts). Do you know if that's the case?

Any handy suggestions for a picaxe beginner. I get the impression that what I'm trying to do should be fairly simple but don't know enough yet to have much confidence.




I'm having trouble with understanding what you'r trying to acheive... Are you trying to send back any data that arrives at your BT module?

MG2R I'm trying to use the picaxe to passon any data that comes from the bluetooth module to into the data line going to the mount (taking the flow control line low while I do so) and when data comes from the mount (indicated by the mount pulling the flow control line low) I want to send that data to the bluetooth module.

I've had a go at the code but there is so much new stuff for me in this that I don't know if that will make it much clearer. Commands coming via bluetooth should always start with a ':' and end with a char 13. Length will be variable depending on what the command requires.

Normal responses from the mount will start with a ';' and end with a char 13, abnormal responses will start with a ! and end with a char 13.

If this is something you don't find interesting or is to far off line for the site then please fell free to ignore it, I'm not trying to be a pest.

From what I could tell I had to define the flow_control line differently for different commands, some commands want the C.2 syntax, others pin2 (hope I've got that right) then the convention is to use the %00000100 when setting an interupt.

' First pass at a program to use a Picaxe 08M2 as a converter
' between a Skywatcher Dob mount and a bluetooth module.
' The dob appears to use a half duplex protocol with hardware flow control,
' the bluetooth module uses seperate tx and rx lines
' Bob Stephens 2012

symbol mount_data = C.1
symbol flow_control = C.2
symbol flow_status = pin2
symbol bt_rx = C.3
symbol bt_tx = C.4

main: setint 0,%00000100
      ' Commands should start with a ':' and end with a char 13
      serin bt_rx,T9600_8,s_w1 ‘ Receive a character into variable b1
      gosub talk_on_data
      goto main


    setint off

    do: pause 1: loop until flow_status = 1

    if flow_status = 1 then

        low flow_control

        pause 3

        serout mount_data, T9600_8,(s_w1)

        high flow_control

    setint 0,%00000100



    ' Read incoming on mount data line when the line is pulled low
    ' Normal response from the mount will start with a ';'. abnormal with a '!'
    ' All commands and responses should end with a char 13

    if flow_status = 0 then
      serin mount_data,T9600_8,(s_w2)
      serout bt_tx, T9600_8, (s_w2)


First, let me start with saying that you'll find that people on this site aren't very strict on "only robotics". General electronics, help with programming or actually just about anything that is remotely connected to technology is welcome, for as far as I can tell.

Now, it has been ages since I've last used a PicAxe (I use Arduino now), so I don't know the language very well anymore. But I can work a little program in pseudo-code to indicate how such a program should be able to function. I could be mistaking, but I'll try my best!

set interrupt to activate interrupt sequence when flow_controll is pulled low
actively listen to the bluetooth module for data
if BT-data is received then
   disable interrupt
   send BT-data to dob
   activate interrupt

interrupt sequence:
   read dob-data
   send dob-data to BT-TX

That should be about it. Although reading the data can be tricky since the data is of a variable length. I don't know how you can work around this with a PicAxe since it always reads a fixed amount of data (defined by the variables you put in the serin-instruction).

You could try posting your problem in the forums, I'm sure that there will be far more capable people than me who are able to answer your question fully.

Good luck!

MG2R I may save myself some grief and do this with an arduino, I was thinking that a picaxe would be a cheap and compact solution but from what I can see issuning a serin on picaxe overrides interupts - yuck.

Sould be easy enough to do with an Arduino and I think i have a couple spare.