StatCounter

Saturday, December 7, 2013

My Experiments with the PICAXE 08M2+

PICAXE 08M2+
I generally prefer to do things the hard way. For example, you can either microwave a frozen dinner or cook from scratch. The microwave option is a good backup plan, but cooking from scratch has far too many advantages, as I outlined in a recent blog post on the do-it-yourself way of life.

In the electronics and robotics world, the analogy of cooking from scratch is to build circuits using a bare bones micro-controller chip (e.g. the PICAXE) rather than a fancy board (e.g. the Arduino). Therefore, once I get a circuit working with the packaged Arduino approach (e.g. this robot I built recently), I usually try to replicate the circuit using more basic components like the PICAXE.

My decision to consider the PICAXE was influenced by Charles Platt's coverage of it in his awesome book Make: Electronics. But essentially, I am a minimalist and I want to see how much I can get done with a bare bones chip rather than a bulky board-based micro-controller like the Arduino. My current favorites are the Nano (quite small, just $7, breadboard friendly, and just as easy to work with as the Uno). My next experiment will be the ATtiny85V-10PU chip because I believe it's still fairly easy to work with (it can be programmed via a regular Arduino and has many of the same capabilities as the Arduino -- many programs written for the Arduino can easily be modified to work on the ATtiny), costs just $2.50, and is of course breadboard friendly. (I don't mind soldering, but for most of my experimentation breadboards are the way to go.)

And the actual PICAXE I chose was the latest model of the smallest chip available, i.e. the 08M2 ($7), for which (as a gentle soul on one of the forums clarified to me) the label on the chip actually reads 08M2+.

If you're planning to use the PICAXE, step one is to figure out how to program it. PICAXE programs are written on and uploaded to the PICAXE chip using an integrated development environment (IDE) known as the PICAXE Programming Editor, which is available as a free download. Henceforth I will refer to this software as simply the IDE. As it turns out, this IDE is fairly handy and includes a simulator to show you how your program will play out on the PICAXE chip once you upload it. Note that PICAXE programs are written in BASIC (yeah, not the most popular language out there) as opposed to Arduino programs, which are written in C and Raspberry Pi programs, which are written in Python.

Uploading your program to the PICAXE is the first major hurdle you're likely to encounter. As I have discovered, everything involving the PICAXE is harder to do than it is with the Arduino. That's because the PICAXE is just a bare bones chip, whereas the Arduino is a micro-controller chip on a board along with a fairly large supporting cast of components and connectors (e.g. USB).

The standard way to program the PICAXE is by using the AXE027 (USB to 3.5mm jack plug male audio cable) or the USB010 (USB to RS232 9-pin male cable). In either case, additional components are needed to complete the setup.

The AXE027 cable uses an FTDI chip to perform the USB to RS-232 serial conversion required to program most micro-controller chips. However, I was inspired by this video by Kevin Darrah and other postings on the web to employ means other than having to buy this pricey $30 cable.

Kevin Darrah's approach uses an Arduino Duemilanove, which I don't have. So, I tried doing this with the Arduino Uno. The Uno didn't work because the Uno uses the Atmega16U2 chip to perform the USB to RS-232 serial conversion. According to my testing, using the PICAXE IDE's "Test Port" feature, the Uno's serial converter did not seem to support the RS-232 "break" command. The IDE's "Test Port" feature works by turning the signal on and off and a multimeter hooked up to measure the voltage across the PICAXE's serial input pin and 0V (or ground) is supposed to detect a change in voltage as the test LED is switched on and off.

This port test did succeed for me when I tried it with the Arduino Nano, probably because the Nano uses an FTDI chip to perform the USB to RS-232 serial conversion, which clearly does support the RS-232 "break" command.

Note: the exact chip I used was the PICAXE 08M2+, which is a significant upgrade on the 08 and 08M chips. I paid about $7 for it (compare with $35 or so for the Arduino, $45 or so for the Raspberry Pi, and $2.50 for the ATtiny85).

Note that the FTDI chip and most other USB to serial converters that perform the conversion often stop short of inverting the signals, as is needed for programming the PICAXE. The FTDI chip can be configured to invert the signals using FTDI's FT_PROG tool. However, I decided to heed the warning on the FTDI site and use the circuit I've described in this blog to invert the signals rather than mess around with the FTDI chip on my Nano and risk having my Nano being rendered useless.

As I mentioned previously, the reason this circuit is required at all is because signals are inverted in the RS-232 serial protocol. Using 0V to represent logic 0 (a bit value of 0) and 5V to represent logic 1 (a bit value of 1) is the more common approach and is known as the transistor-transistor logic (TTL) level. The RS-232 serial logic levels are not only higher (+/- 12V, so you wouldn't want to connect them directly to your chip since most chips operate on 5V or 3.3V) but also inverted. Therefore a USB logic 1 signal needs to be converted to a logic 0 signal and a USB logic 0 signal needs to be converted to a logic 1 signal. The circuit described here accomplishes that inversion of signals.

To program the PICAXE the IDE needs to send data to the PICAXE and receive data back from the PICAXE. Think of the RX (receive) pin on the Arduino as representing data arriving from the PICAXE IDE via the USB-to-serial FTDI converter. When the IDE is sending data to the PICAXE, the RX logic 1 signal is applied to the transistor's base, which results in completing the circuit from the transistor's collector to emitter and a logic 0 signal being applied to the PICAXE serial-in pin (the logic 1 signal from the Arduino's RX pin got inverted to a logic 0 signal prior to being sent to the PICAXE's serial in pin).

Conversely, when the PICAXE sends data back to the IDE, a logic 0 signal from the PICAXE's serial out pin causes the transistor to remain off and the 5V (logic 1) signal is applied to the Arduino's TX pin. And a logic 1 signal from the PICAXE's serial out pin, when applied to the transistor's base, turns the transistor on, thereby causing a logic 0 (0V) signal to be applied to the Arduino's TX pin. Hence, the inversion of signals is accomplished via the use of these two NPN 2N2222 transistors and four 10K resistors.

So, finally, here's the circuit that I've been describing in the paragraphs above.

Schematic From Kevin Darrah's Video
And here's the breadboard version of the diagram I created, to help you implement this for your own use. I recommend purchasing a bunch of smaller breadboards for this kind of thing so that, for example, once you've setup the breadboard for programming the PICAXE 08M2+ you can set that breadboard aside and leave it dedicated to that purpose. In fact, what I do is to simply move the jumpers off the PICAXE's serial in and serial out pins, move them to an used part of the board, wire the PICAXE's serial in and serial out pins to ground (assuming they will not be used) and then use the same breadboard for operating the PICAXE circuit. Bottom line, you probably want to dedicate a board per micro-controller so that you can have the circuit required to program it left in tact for the next time you wish to use it, and possibly use the same board to operate the micro-controller project you're developing.

Breadboard Circuit for Programming the PICAXE with an Arduino
Also, here's the actual simple program that utilizes this circuit.

LED Blink Program for the PICAXE
But my challenges did not end once I had figured out how to program the PICAXE. After I first wired up the LED it wouldn't blink at all. After some soul searching I realized that the pin assignments on the PICAXE 08M2 follow a port.pin address space in order to to provide for a greater number of options. (Note that the 08M2+ chip is vastly improved and expanded relative to the 08 or 08M.) I have to say, PICAXE has done a poor job of explaining how port.pin addressing works. Also, the fan base seems to be declining since there aren't enough YouTube videos or web posting on these important topics. Accordingly, my next stop is likely to be the ATtiny85, which is roughly comparable to the PICAXE 08XX series, but might enjoy a somewhat better community due to the traction it gets via its inclusion into the Arduino boards.

However, to be fair, I should acknowledge that Atmel also uses different physical/hardware and software/code pin numbering schemes for its chips, e.g. the ATtiny85. Regardless, I'd appreciate it if anyone could point me to a good tutorial on how to use the four ports (A, B, C, D) on the PICAXE 08M2+.

The ATtiny85 by Atmel
Anyhow, after some trial and error, I realized that based on the port.pin addressing scheme, the "traditional" pin 3 is what mapped to B.4 (the port I had programmed to turn on and off every second, using the port.pin notation). I now had my LED blinking, but very erratically. For a brief moment I considered abandoning the PICAXE as a lost cause. But that was a very fleeting moment indeed. As any engineer knows, perseverance bears sweet fruit So, I summoned all of the resources on the web and my learning in the past about the ll-effects of "floating" pins (i.e. pins that are neither clearly low nor high). And so, I added a rectifier diode (1N4001) and ceramic capacitor (0.1 micro Farads) between 5V and the PICAXE's voltage in pin. Additionally, I tied the unused pins to ground via a 560 ohm resistor (the only one I had handy). And low and behold, the LED started blinking the with the clockwork regularity you expect from a swinging pendulum.

PICAXE 08M2+ Pin Assignments
This was a learning experience for me about how working with chips is different (and a bit more challenging) than working with boards, such as the Arduino Uno, that come with supporting circuitry to ensure that unused pins are not left "floating" etc.

Here's the circuit I put in place to get a reliably blinking LED. Note that this is the configuration you'll need in case you wish to have the serial in/out pins to double duty, e.g. digital I/O. However, my favorite configuration is one in which I leave serial in/out pins dedicated to programming and use the other four pins for I/O. That way, I can program on the fly. Also, note that here the Arduino is merely supplying power (5V) and I have not shown here the USB cable from my laptop providing power to the Nano.

Breadboard Circuit for PICAXE 08M2+ Blinking LED
Something I noticed about the PICAXE Programming Editor or IDE may be useful to point out. On Windows 8 the IDE seems to get orphaned (i.e. the process continues to run in the background even after you've closed the UI) and chews up a fair bit of CPU (50% in my case). Something to watch for.

In closing I'd like to share one final observation. So far, I have been lucky to have fried or damaged very few components. The first was a set of three LEDs that came with my Raspberry Pi starter kit from Adafruit. That was until I realized that I simply must add a 1K resistor in series with LEDs at all times. And I also realized that once you know exactly what you want (which requires a bit of learning and research), it's a lot cheaper to buy components via eBay. The second was the Raspberry Pi's SD card slot, which broke upon the slightest application of vertical stress. (BeagleBone's MicroSD card slot is a much better idea since it's smaller and less susceptible to vertical stress.) And finally, the first PICAXE I ordered either arrived damaged or was damaged during my experiments with trying to program it. I suspect it might have been the former (a ding against eBay), because when it arrived the painted branding information (PICAXE 08M2+ and serial number) had already worn off and I had to strain to read the engravings (a shout out to the iPhone magnifying app "Mag Light" for making this a lot easier). The second PICAXE I ordered (again via eBay), which is the one I successfully programmed, seems pretty resilient. I have even accidentally applied reverse voltage and all it did was pop the software fuse on my laptop's USB port (requires a laptop reboot to reset). To be fair, I have ordered many, many components from eBay at extremely reasonable prices and have had very few negative experiences (although the best deals do take forever to ship and will test your patience and stamina for electronics). Note that I always make sure that the seller has at least a few hundred reviews and a 98% or higher ratio of positive feedback.

Update: Good news! The first PICAXE, which I had set aside as "damaged" has turned out to be perfectly fine and healthy. I swapped it into my programming/test operation circuit (described above in this blog post) and I am able to program and operate it. So, it was human error after all. However, when I'm at my wits end I often fall back to swapping components like chips that cannot easily be tested with a multi-meter. And it is certainly good to have plenty of spare breadboards and jumpers so that once, for example, you've established a working circuit for programming a micro-controller chip you can set it aside and not have to tear-down and build it each time you need to program the chip. Now on to programming my 2D string array of remote control codes for my Arduino robot project (see link above), which I eventually want to try controlling with the smallest bare bones micro-controller that'll do the job, e.g. a PICAXE or an ATtiny. Also, I am told that faded etching on top of the engraved chip markings are quite normal and are not necessarily a sign of wear.

I know I had a bunch of trouble getting my PICAXE 08M2+ to do my bidding. So, I decided to document my experiences in order to help others and perhaps even myself, if several days from now I can't recall how I actually got it working. As I mentioned, working with the PICAXE (or any other bare bones micro-controller chip like the ATtiny85) is sure to be more challenging than working with a board like the Arduino boards. Also, relative to the PICAXE, there is a lot more help available for the Arduino in the form of videos, web posts, and books. But if you're up for the challenge, then the PICAXE can give you more control at a smaller cost. All said and done, I feel very positive about the PICAXE and its prospects on future projects of mine.

3 comments:

  1. You don't need any fancy stuff. All you need is a standard USB-RS232 cable, and the rest you can find it in an old monitor box. The board design is ultra-easy, and costs nothing.

    ReplyDelete
  2. There is a hands-on full course in spanish about it: www.cursoderobotica.com where they use that chip. Also the Logicator programming ide is of a great help. The only thing you must be aware of, is that even when they don't tell it, the standard 2 cents USB to Serial cable DO the work, so there's no need for any AXE027 connector.

    The picaxe08m2 you bought I think is a bit expensive, in my belief..

    ReplyDelete
  3. I’m not that much of a online reader to be honest but
    your sites really nice, keep it up! I'll go ahead and
    bookmark your website to come back later. Cheers

    Take a look at my page ... novoline kostenlos spielen ohne anmeldung

    ReplyDelete