I got a bit disappointed with my little bus pirate yesterday. I'll investigate how to update it to fix the odd SPI behaviour another time, but for now, its on to trying to read the MCP3002 SPI ADC chip directly using the Raspberry Pi. However, this one I'm going to do a bit differently. My desk is filling up with breadboards and floating wires, and working out each time which wire to connect to which header pin is becoming annoying. So I have decided to wire up a simple plugin board with the MCP3002 connected to the correct SPI pins on a 26-way header. That way I can just plug it in when I want to use it, and unplug it if I want to use the rspberry Pi for something else.I have a fairly diverse collection of prototype boards, some with strips, some with grouped pads and some with just a grid (a.k.a. “pegboard”, “pad per hole”, etc.) of tinned holes. For this little project I thought I'd have a go at using one of the “pad per hole” boards. They are very cheap, particularly when ordered from China, so I'm not too worried if I mess it up. One of my collection has a grid of 14 x 20 holes. This is almost exactly the right width for the 2×13 Raspberry Pi GPIO header and it looks like it will sit nicely on top of the Raspberry Pi. My plan is to add a socket for the MCP3002 (just in case I suddenly want to use it for something else) and wire the socket and the GPIO header together.
Reading analogue data on a Raspberry
While thinking about how to lay out the board, I found that if I put the IC socket on the “header” side of the board, and arrange it in the right way, the MOSI, MISO and CLK pins line up with each other. That certainly makes wiring simpler, so that's the way I have decided to do it.
Of course, I can't resist adding a little bit of extra. The Raspberry Pi provides two SPI /CS lines (labelled CE0 and CE1). Although I only plan to use CE0 at the moment, I thought I might as well add a little pin-and-jumper switch to allow me to choose which one to use without re-soldering. Also, I guess it makes sense to add a header to connect whatever the ADC will be reading. This one will provide GND, Vcc and a signal input pin. Other than that, the circuit is very simple, mainly a matter of connecting the right Raspberry Pi headers to the right MCP3002 pins.
When it comes to putting it together, that's a different matter. I learned my soldering and circuit-building in the late 1970s and early 1980s when Veroboard was king and only the rich kids who read “elektor” magazine would dream of using a printed circuit board. I never encountered any of these pad-per-hole boards at that time, though, and I quickly realized that I was unsure how to use it! Luckily the internet is full of helpful people, and this video gave me enough confidence to give it a go.
After soldering and double-checking all the connections (by eye, and also with a multimeter to check for accidental shorts and bridges) I was ready to try it out. I plugged the MCP3002 chip into its socket, added the jumper to select CE0 and connected my resistor divider (still on the breadboard) to the analogue input connector. However, I hit a problem straight away. Although I had checked the heights of components on the board to make sure that they did not foul the Raspberry Pi main board, I had not allowed for the extra height of the connectors I was using to wire the analogue input. They were only a few mm too high, but it was enough to prevent the expansion board from sitting properly on the Raspberry Pi. As a quick and dirty fix I just plugged in another header which boosted the height by about 1cm. If this board seems useful, I'll probably replace the analogue input header with a right-angle one for the future.
For more detail: Reading analogue data on a Raspberry Pi using MCP3002