Monday, January 13, 2014

Raspberry Pi to PICAXE via I2C


Two separate programs, one Python program running on the Raspberry Pi and the other in PICAXE basic running on a PICAXE chip and communicating by I2C allow you to use the PICAXE chip as a digital / analog bidirectional I/O expander chip with up to 31 digital/analog I/O pins. So you get a lot more digital input, digital output and analog input pins than you have on the Raspberry Pi alone. As IC2 can support 127 devices, you could support many PICAXES all on different addresses for hundreds of pins of I/O.


You can download the rPI Python and the PICAXE basic code from here. (For some reason, when I tried to paste the code inline in this blog post, it got munged, apparently this is a know issue).

I have wanted to do this for a while, and was thinking about using good old serial but then I decided to see if anyone had done anything similar using I2C. I found this blog post by "AntMan232" which gave me the starting point. Thanks!

I use a PICAXE-40X2 chip which gives up to 31 pins for all kinds of digital and analog I/O, and is a 40 pin DIP package so is easy to work with on a regular proto board. It is available from suppliers in many countries (i.e. PICAXE-40X2 from Canada). The PICAXE is a Microchip PIC microcomputer chip with a special bootloader/OS on it that allows you to develop and download code with the free PICAXE editor.

One could also use the PICAXE ability to do Pulse Width Modulation, control RC servos, drive LCD displays and lots of others, see the PICAXE documentation.

If you have not worked with the PICAXE toolchain, see this older blog post.

Once the rPI has been set up as I2C master and the PICAXE as I2C slave by programming it with the code below, the communications is actually very simple. The rPI Reads/Writes variables into the PICAXE scratch pad where the PICAXE can read and write them. Basically the scratchpad acts as  a bidirectional "window" between the PICAXE program and the rPI Python program. The values are moved back and forth in the background with no further action on the part of the program using the I2C protocol. Very slick!

As the circuit is very simple, I have not drawn a schematic as the hardware setup on the breadboard that I am using is quite simple, but all of the pin to pin connections are shown as a table in the PICAXE source code below.

The PICAXE is powered from rPI 3.3 GPIO pins, draws <10mA. Since rPI can only supply 50 mA, outputs should be buffered. Digital outputs therefore drive 74HC14N buffers to LEDS, powered by a separate 5 volt power supply.  Analog inputs are from a wiper of 10K pot from 3.3V to Ground. Digital inputs are active pullup to 3.3V; sw to Ground. See PICAXE manual for programming cable schematic (don't leave out resistors). Be sure to use lots of capacitor bypassing on your breadboard.



Eric Pierce VA3EP - See the Disclaimer in the Introduction

© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.



No comments:

Post a Comment