To follow this tutorial you will need
And of course a working Raspberry Pi.
Why we need an ADC
The Raspberry Pi computer does not have a way to read analog inputs. It's a digital-only computer. Compare this to the Arduino, AVR or PIC microcontrollers that often have 6 or more analog inputs! Analog inputs are handy because many sensors are analog outputs, so we need a way to make the Pi analog-friendly.
We'll do that by wiring up an MCP3008 chip to it. The MCP3008 acts like a "bridge" between digital and analog. It has 8 analog inputs and the Pi can query it using 4 digital pins. That makes it a perfect addition to the Pi for integrating simple sensors like photocells, FSRs or potentiometers, thermistors, etc.!
Let's check the datasheet of the MCP3008 chip. On the first page in the lower right corner there's a pinout diagram showing the names of the pins:
In order to read analog data we need to use the following pins:
VDD (power) and DGND (digital ground) to power the MCP3008 chip. We also need four "SPI" data pins: DOUT (Data Out from MCP3008), CLK (Clock pin), DIN (Data In from Raspberry Pi), and /CS (Chip Select). Finally of course, a source of analog data. We'll be using the basic 10k trim pot.
The MCP3008 has a few more pins we need to connect: AGND (analog ground, used sometimes in precision circuitry, which this is not) connects to GND, and VREF (analog voltage reference, used for changing the "scale" - we want the full scale, so tie it to 3.3V).
Below is a wiring diagram. Connect the 3.3V cobbler pin to the left + rail and the GND pin to the right - rail. Connect the following pins for the MCP chip
- MCP3008 VDD -> 3.3V (red)
- MCP3008 VREF -> 3.3V (red)
- MCP3008 AGND -> GND (black)
- MCP3008 CLK -> SCLK (yellow)
- MCP3008 DOUT -> MISO (purple)
- MCP3008 DIN -> MOSI (white)
- MCP3008 CS -> #22 (green)
- MCP3008 DGND -> GND (black)
Next connect up the potentiometer.
- Pin #1 (left) goes to 3.3v (red)
- Pin #2 (middle) connects to MCP3008 CH0 (analog input #0) with a purple wire
- Pin #3 (right) connects to GND (black)
Below we provide to wiring diagrams that will work with all versions of Raspberry Pi released so far (except the compute node which has no header). The first diagram is for the most recent Pi v3 and Pi Zero models which have a 40-pin GPIO header. The second is for the first two generations of Raspberry Pi which had a smaller 26-pin header. In both cases we are using the same GPIOs so the code will not be any different.