RPi – Analog to Digital

In this episode of Cooking with Team 279, we explore the wonderful world of Analog signals.

Many sensors that will be used with a device connected to the “real world” will be analog and not digital, usually varying a voltage level to indicate the measured value. Unfortunately, the Raspberry Pi has absolutely no way to directly read these values.

Enter the Analog to Digital Converter (ADC)!

An analog to digital converter takes an input analog signal, and translates it to a number that can be read.  A very popular set of ADCs for tinkers to use are the MCP3002, MCP3004, and MCP3008 ICs.  These cost very little ($1.50 – $4), and can measure either 2, 4, or 8 separate input signals respectively.

These ADCs are 10bit devices, which means that the number that allow to be read will fall in the range of 0 – 1027 (ie.. 1028 values, which is 2 ^ 10)

These devices work by comparing a single to measure against a reference voltage.  This reference voltage define the range.  If the measured signal was equal to it, then the number returned would be 1027. If the measured signal was zero, then the number returned would be zero.
For example, if the reference voltage is 5V, and the measured signal is 1.5V, then the ADC will return ~308  (1.5/5 * 1027)


Attached is a worksheet for learning to use the MCP3002, MCP3004, or MCP3008 Analog to Digital Converter (ADC) with the Raspberry Pi.

In this worksheet, you’ll build a simple circuit that has an analog component, and use the ADC to measure the value.  You’ll also be introduced to SPI, which is how the RPi talks to these particular ADCs.



Example script for reading values from the MCP300x ADCs

(After saving this, remove the “.txt” extension to run as a Python script)