Using Analog Sensors with a Raspberry Pi
January 2, 2022 | 4 minutesIn This Post
The general-purpose input-output (GPIO) pins on a Raspberry Pi only support digital signals natively. However, many sensors, peripherals, and other microcontrollers communicate using analog signals. The analog signals need to be converted so that the Raspberry Pi can understand them. Connecting a device that communicates with analog signals directly to a Raspberry Pi will either throw an error or cause wildly inaccurate data readings from the device.
Let's look at the differences between analog and digital signals and how we can convert them.
What's the Difference Between Digital and Analog Signals?
Digital signals represent two states: high (1) and low (0). On these Raspberry Pi boards, a GPIO output is about 3.3 V when it’s high and 0 V when it’s low. An analog signal, on the other hand, can vary continuously over a range of voltages.
Don’t connect 5 V directly to a GPIO pin. The Raspberry Pi’s GPIO pins use 3.3 V logic. The header also has separate 5 V power pins, but applying 5 V to a GPIO input can permanently damage the Pi.
To get analog devices to work with the Raspberry Pi's digital GPIO pins, you need to convert the analog signals using an analog to digital converter.
What is an Analog to Digital (ADC) Converter?
An ADC does what it sounds like: it converts analog signals to digital signals. It does this by sampling an analog voltage at a point in time and converting it into a digital signal representing the analog voltage.
ADCs come with different resolutions, and the resolution determines the number of discrete analog levels that it can detect. More specifically, the resolution determines the smallest signal and the size of incremental signal changes that the converter can detect. For example, a 10-bit analog to digital converter can detect 1,024 (or 2^10) analog levels, while a 16-bit converter can detect 65,536 (or 2^16) analog levels.
How to Convert Analog to Digital Signals with a Raspberry Pi, an Analog Sensor, and an ADC
I originally used a Raspberry Pi 3B, an MCP3008 ADC, and a TMP36 analog temperature sensor. The Raspberry Pi 4 Model B and Raspberry Pi 5 use the same 3.3 V power, ground, and SPI0 header pins for this circuit, so you can use the same wiring table below. That compatibility is based on the official pinout. I haven’t tested the circuit on the newer boards.
Shut down and unplug the Pi before wiring it.
Connecting the Raspberry Pi to the MCP3008
Use the Pi’s 3.3 V power rail for both VDD and VREF in this example. The MCP3008 connects directly to the Pi over SPI, so it needs to use the same logic voltage. VREF also sets the top of the voltage range the ADC measures.
The Pi numbers below are physical header pins, not GPIO numbers. See the Raspberry Pi SPI0 pinout, the MCP3008 pin functions, or my SPI overview.
| Raspberry Pi physical header pin | MCP3008 pin |
|---|---|
| 1 (3.3 V power) | 16 (VDD) and 15 (VREF) |
| 6 (ground) | 14 (AGND) and 9 (DGND) |
| 19 (SPI0 MOSI) | 11 (DIN) |
| 21 (SPI0 MISO) | 12 (DOUT) |
| 23 (SPI0 clock) | 13 (CLK) |
| 26 (SPI0 CE1) | 10 (CS) |
Connecting the TMP36 to the MCP3008
For the three-lead TMP36 in a TO-92 package:
- Connect pin 1 (
+VS) to the same 3.3 V power rail. - Connect pin 3 (
GND) to the common ground. - Connect pin 2 (
VOUT) to MCP3008 pin 2 (CH1). That leaves seven other channels available.
If you use a different sensor, check its supply requirements and output range first. For this circuit, the voltage at an ADC input should stay between ground and the 3.3 V reference. If the sensor can output more than 3.3 V, don’t connect it directly to the ADC input.
Once you’ve wired everything and enabled SPI, select SPI0 chip select 1 and ADC channel 1 in your code.
Suggested Analog to Digital Converters
I've used the following analog to digital converters in my Raspberry Pi projects. Both work well, but the ADS1115 is more precise. Disclosure: I get commissions for purchases made through links in this post.
Once you have analog sensor data flowing, you can use MQTT to share that data among multiple devices or build something practical like an automated plant watering system.
