Raspberry Pi Port expander (with ultrasonic range finder for fun)!
I’ll not bother with some OTT premise to this. Kept simply, I wanted to be able to connect more stuff to the Pi, so I made it happen. Here’s how!
So, here’s the Raspberry Pi. I’m sure you’ve seen pictures of it before, but here’s another. As you can see, on the bottom right is a set of pin headers. 26 of them. These are where some of the Pi IO functionality is brought out. Theres GPIO pins, i2c, SPIO UARTS and the like (http://elinux.org/RPi_Low-level_peripherals). I decided to use i2c to connect my port expander because it uses the least pins, leaving more open for later use, plus it seems to be quite easy to use. As you can see, there’s a heatsink on the CPU/Memory. I’ve no idea if it’s actually any use or not, but it was sat in a drawer doing nothing, so I thought it might as well sit on the Pi doing nothing instead.
This is what I used as the port expander. It’s an Atmega 32u4 project board that I snagged 3 of from ebay last year at a few quid each. Handy little boards, as the 32u4 has a USB module, so it can expose itself as a USB device. It also happens to be the same chip used by the newest version of the Arduino, so for this project, to make life easier, I programmed it with the Arduino Leonardo firmware using a USBtinyISP. In the future I’ll probably drop the Arduino firmware, but for now, it does the job nicely.
This is the Ultrasonic range finder I used. It’s a HC-SR04. It finds ranges. ;)
Anyway, on to the making!
I put a piece of paper on top of the pi, and traced around the video out and LAN ports. I cut them out, put the paper back, then trimmed it to the size I wanted. Ignore the orange marks for now, the original pic was a bad one so I had to take another after, so you’ll see soon why the orange is there. I also pushed the paper down gently on the GPIO pins to get a few of them to punch through. This came in handy for lining up traces on the stripboard.
What I did here was very simple. I placed the paper on the top of some stripboard. I made sure the holes made by the GPIO pins lined up with the holes in the stripboard. I then used a marker (orange, see, told you there was a reason) to mark out the areas to cut out. This was across the top, down the left, and the rectangles for the LAN and video. To cut it out, I used a dremel tool, though a small hacksaw or whatever your tool of choice is will suffice. I had to sand down some of the edges both for a better fit, and for a nicer, slightly less sharp finish.
Annoyingly, I lost some photos. Not entirely sure how. Anyway, as you can see, the board has been cut out, and now has some bits soldered in place. Blue and Yellow connect the Range Finder to the 32u4. Orange is +5v (I ran couldn’t find my red wire). The one coming from the GPIO header is going to the 32u4 boards +5v. The other is coming from the boards VCC. VCC is jumpered either to VUSB or +5V, so in this case, I’m using +5v. Purple and Green are connecting the Pis i2c SDA and SCL lines to the 32u4 SDA and SCL. Black is ground. To attach the GPIO header connection, and not have to have the board upside down, I cheated a little. I sanded off the copper underneath that area completely, then used 2 part epoxy glue to glue the headers there. This left the pins poking through the top just enough to solder to. When I get around to order some, I’m going to order female headers with longer pins, so I can use jumpers on the top. The blue component above a 3 wide header is just a potentiometer. I put it there for testing stuff out.
Here’s the underneath of the board. As you can see, I did a pretty bad job of gluing the headers. I tried superglue to start with, but it was too brittle, kept running, and got inside the headers a little. You probably don’t want to do this. It’s a very stiff pulling the board away from the Pi now. When I had scraped as much superglue off as I could, I put the headers in place, and used a toothpick to put epoxy around the edges between the board and the headers. This seems to be more than enough to hold it in place. Down in the bottom right, there’s a 5 long set of headers all connected to ground. In some later pictures, you’ll see this has snapped off. It’s not really needed though, so that’s fine.
This is what it looks like with the board, sensor and 32u4 in place.
And a nice fritzing image of the board. Note that this is a top down view, and the copper should be underneath, and not visible.
Whelp, now software time. As I mentioned earlier, the 32u4 board has the Arduino Leonardo firmware installed, so I set up a sketch using the Ultrasonic library from here. It uses the Wire library to do i2c. The sketch is Ultrasonic.ino is below.
[gist id=3380883 file=Ultrasonic.ino ]
The code to read it is UltrasonicReader.py below. The reader code requires the Quick2Wire python libs from here.
[gist id=3380883 file=UltrasonicReader.py ]
I’m using the latest Raspbian distro, with the latest kernel with i2c drivers enabled.
At some stage, I will program the 32u4 to expose all outputs over i2c, but for now, the Ultrasonic rangefinder is enough fun :p
Update! Here’s a link to the fritzing files if you want a clearer view of the circuit.