serial input while slowly pulsing an LED?



I'm afraid this must be a terribly naive question but I haven't been able to find the answer in the manual, a picaxe book I have, or the forums. I want to make a tri-color LED slowly pulse (like Apple laptops when they are asleep). This seems fairly straightforward. However, I want to control the color from my computer over a serial line. I don't see a way to have the picaxe both waiting for serial input and changing the LED brightness using pwm because the SerIn command precludes other activity. What are my options here? The only thing I could think of was add a 2nd picaxe to handle serial, but even then it isn't obvious to me how to communicate from the picaxe doing serial to the picaxe controlling the LED.

So what am I missing? Is there some trick to getting serial input while doing other processing?


Senior Member
X1 parts have hardware serial that can run in the background.
Or even just serial with short timeout.

So to change color, your pc will send the data over and over again for maybe 10seconds, and the picaxe will read once every few secs with a fast timeout, if using pwm it will still be lit, but not changeing brightness for that duration.

Ofcause the hardware serial is better, then the data is ready and you just check a flag to see if new data was recieved.
New X1 parts are the gold standard and it also is possible to do it with an 08M but you need a few software tricks. You need a protocol where a device makes a line go high then the receiving picaxe detects that the line is high and goes into serin mode and gets the data. The PC can send a 255 which can be the start byte then the data bytes with a qualifier like "ABC". A 255 is a series of 8 high bits, and at 2400 baud the line will be high for 8/2400th of a second. Use a slower baud rate if the loop is missing these start bytes.

You can't use PWM or servo commands but if you write a really tight loop you can do individual pulsout commands. So the loop would be pulsout to one led, then a variable length pause, then a pulsout to another led, and then a quick check of the data line from the PC. If the loop is quick enough the 255 start byte from the PC won't get missed. If the line is high then jump to a serin and get the data which will only pause the loop briefly.

It isn't perfect by any means - even with 2400 baud I found this method still produced noticable glitches on a motor controller but these were tolerable. But it is the cheapest option.

Edited by - Dr_Acula on 04/09/2007 02:57:13


Technical Support
Staff member
If you can use bit-banged PWM to control the intensity/colour of the LED you can use polling as suggested by Dr_Acula. If using an 18X it is possible to configure it so a single byte can be received from a PC in the background which may help. A main loop would do the PWM, check to see if a byte had been received, obey it if it were, then continue with PWM. Almost the same as using a 28X1, but may be a more suitable solution for what you need.
Thanks for the replies, that gives me something to try. The one other approach I was considering was external ICs, either on the input side (serial buffers?) or the output (shift registers? LED drivers?) but nothing particular is coming to mind. Has anyone ever gone down this avenue?


Technical Support
Staff member
Buffering serial in can get complex quickly, and it's probably more cost effective to move up to a PICAXE which will do what you want without the aggravation.

You can control the LED using external logic, latches and shift registers but that could simply slow down the ability to switch the PWM at a rapid rate.

If this tri-colour LED is an RGB LED, one potentially 'perfect solution' could be an 08M plus 14M. That would give three genuine background PWMOUT's to drive the RGB signals while receiving PC serial.

There's still be an issue of having glitch-free fading while handling serial, but that could probably be dealt with by using a third PICAXE.

In the commercial world, a project like this would be handled by a dedicated, cheap processor programmed to do the specific task. It's not one which the PICAXE is ideally designed for.