Accessing Analog Sensor Data on the Raspberry Pi with ADC

A step-by-step guide on how to connect and use an analog-to-digital converter (ADC) with a Raspberry Pi to read data from analog sensors. …


Updated October 11, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


A step-by-step guide on how to connect and use an analog-to-digital converter (ADC) with a Raspberry Pi to read data from analog sensors.

In this article, we will cover how to connect an Analog-to-Digital Converter (ADC) to a Raspberry Pi and use it to read data from analog sensors. An ADC is a device that converts analog signals into digital data that can be processed by a microcontroller or computer. This is useful for connecting devices like temperature sensors, light sensors, and potentiometers to the Pi, which only have an analog output.

Step 1: Choose an ADC Module

There are many different ADC modules available on the market, but we will be using the MCP3008, a popular 8-channel 10-bit ADC module based on the Microchip MCP3008 chip. This module communicates via the SPI (Serial Peripheral Interface) protocol and is easy to integrate with the Raspberry Pi’s GPIO pins.

Step 2: Connect the Module to the Raspberry Pi

The MCP3008 has four pins: VDD, VREF, GND, and CLK, Dout, DIN, CS/SHDN. You will need to connect these pins to the corresponding GPIO pins on your Raspberry Pi:

  • Connect VDD to 3.3V (Pin 1)
  • Connect VREF to 3.3V (Pin 1)
  • Connect GND to Ground (Pin 6)
  • Connect CLK to SCLK (Pin 23)
  • Connect Dout to MISO (Pin 21)
  • Connect DIN to MOSI (Pin 19)
  • Connect CS/SHDN to CE0 (Pin 24)

Step 3: Enable SPI on the Raspberry Pi

The Raspberry Pi uses the Serial Peripheral Interface (SPI) to communicate with the ADC module. To enable SPI, open a terminal window and run the following command:

sudo raspi-config

Navigate to “Interfacing Options” and select “SPI”. Enable SPI and reboot your Raspberry Pi when prompted.

Step 4: Install the required libraries

We will be using the spidev library to interface with the MCP3008. To install it, run the following commands in a terminal window:

sudo apt-get update
sudo apt-get install python-spidev

Step 5: Read ADC data from the Raspberry Pi

Now that everything is set up, we can read analog sensor data using Python. Create a new file called “adc_read.py” and open it in your favorite text editor. Add the following code to the file:

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)

def read_adc(channel):
    adc = spi.xfer2([1, (8 + channel) << 4, 0])
    data = ((adc[1] & 3) << 8) + adc[2]
    return data

while True:
    voltage = read_adc(0) * 3.3 / 1023
    print("Channel 0: {} ({} V)".format(read_adc(0), voltage))
    time.sleep(1)

This code will read data from channel 0 of the ADC and convert it to a voltage value. You can replace “0” with any channel number between 0 and 7 to read data from other channels. Save the file and run it using Python:

python adc_read.py

You should see output like this, with values changing as you move the potentiometer:

Channel 0: 123 (1.23 V)
Channel 0: 456 (0.456 V)
...

And that’s it! You can now use this code to read analog sensor data from your Raspberry Pi with an MCP3008 ADC module. Feel free to experiment with different sensors and channels to gather data from various sources.