Get your Pi to read analog data

Valery Kachaev, 123RF.com

A/D Tunnel

The Raspberry Pi still lacks analog GPIOs that would allow it to read directly from temperature and light sensors, or even humble potentiometers. With an inexpensive chip and some software-fu you can grant the Pi the gift of analog sensing.

Even the most basic Arduino, the UNO, has seven analog pins. In fact, the last time around we tried to make the Pi read analog inputs [1], we used a Digispark – a tiny Arduino compatible board with analog pins. We read from an analog source and passed the digitized data on to the poor old Pi via one of its USB ports.

The solution was inelegant, bordering on messy overkill. It was also much more expensive than it needed to be. We were using an $8 device capable of much more than acting as a mere translator.

What we really need is an ADC, or an Analog to Digital Converter (Figure 1). These modest little ICs costs about one Euro each, or less if you buy several together.

Figure 1: The unassuming MCP3202 analog to digital converter.

However, before you start shifting analog data to and fro, you have to learn a little about the SPI protocol.

SPI

The 3202 is a SPI device. SPI [2], or Serial Peripheral Interface, is a cousin of the I2C protocol, that thing Martin Mohr is always droning on about [3]. Similarly to the I2C, SPI is used to send data between microcontrollers and small peripherals, such as shift registers, sensors, SD cards and of course the 320x family of ADCs.

Luckily, the Pi can handle SPI out of the box. To activate SPI on the Raspberry Pi, use the raspi-config tool: go to Advanced > A6 SPI and choose Yes. Alternatively, simply edit /boot/config.txt and remove the # from the line that says

#dtparam=spi=on

to uncomment it.

After making your changes, reboot your Pi to load the SPI drivers into the kernel. Two new devices will appear in /dev: spidev0.0 and spidev0.1.

Use the command gpio readall to see how the pins assigned to the SPI protocol change when the drivers are loaded. In Listing 1, for example, GPIOs 19, 20, 21, 22, 23, 24 and 26 on a Pi 3 change when the SPI system is activated (below) from when deactivated (above).

Listing 1

SPI drivers unloaded and loaded

$ gpio readall # SPI driver not loaded (default)
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
.
.
.
|     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
.
.
.
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
$ sudo nano /boot/config.txt
$ sudo reboot # See ya later!
$ gpio readall # SPI driver loaded
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
.
.
.
|  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
.
.
.
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+

If this seems like a lot of changes for one analog input to you, take a look at Figure 2. This will help you understand better how things work. To the left are the MCP3020 pins, the analog to digital converter we have chosen. The Raspberry Pi 3 pins are on the right (this also applies to the Pi 2). These are the pins you're going to use.

Figure 2: Schematics of connecting the 3202 to the Pi.

Start by looking at the Pi's pins in order. Starting from the top, pin 18 is a 3.3v output. This will provide power to the 3202, so connect it to its VDD port.

In any SPI relationship, there is a Master device (in this case the Pi) and a Slave device (in this case the MCP3202). The master device issues commands and the slave responds. Pin 19 on the Pi is the MOSI pin, that is, the Master Out/Slave In pin. This is the pin through which you send commands to the slave. It connects to the 3202's Data In (DIN) port.

As you may have guessed, pin 21, aka the MISO pin works in reverse. MISO stands for Master In/Slave Out, and is the pin through which the Pi receives data from the slave. It connects to the 3202's Data Out port.

SPI transactions are synchronous. This means only one transaction can be executed at a time, much like an old fashioned grandfather clock pendulum swings back and forth. In fact, the correct moment for each transaction is dictated by the master device's clock. When the master sends a request for data to the 3202, it counts as one transaction (tick!). When the 3202 answers with the corresponding data, this counts as another transaction (tock!). That's where pin 23 (SCLK) on the Pi comes in. This pin connects to the 3202's CLK (CLocK) port and sets the pace at which transactions will occur.

CE0 (or CE1) connects to the slaves's CS port, also referred to as Chip control, port. It tells your Pi what SPI device it is talking to. By default the SPI driver on the Pi gives you two CEx pins, so your Pi can even talk to two SPI devices simultaneously.

Finally, the 3202's VSS port connects to the Pi's GND on pin 25, closing the circuit.

Note that the 3202 has two ports (2 and 3) labeled CH0 and CH1. You will be connecting your analog sensor to one of them. These are the ports through which the ADC receives the analog data. As you have two, you can connect two sensors to one 3202 chip.

Wiring up

By way of a practical example, let's connect a potentiometer to the 3202 and read in the different values when we turn the knob. You can see a diagram of what the wiring will look like in Figure 3.

Figure 3: Pi, 3202 and potentiometer all wired up.

Apart from connecting the Pi to the 3202, you will need to connect the potentiometer's leftmost pin to a power source (the Pi's 3.3v pin will do), and its rightmost pin to GND. The potentiometer's center pin, the one that actually outputs the varying voltage, connects to the 3202's CH0 pin as mentioned above.

Table 1 lists a summary of what connects to what. The first column lists the 3202's pins (see Figure 2), the second columns shows the Pi's pins, and the last column shows the potentiometer's pins.

Table 1

Connection Summary

3202

Raspberry Pi

Potentiometer

1

24

-

2

-

2

3

-

-

4

25

3

5

19

-

6

21

-

7

23

-

8

18

1

Figure 4 shows what the set up may look like in real life if you are as messy at wiring as I am.

Figure 4: The messy set up in real life.

Note to self: acquire shorter jumper cables.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content