0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Read Analog Sensor on Raspberry Pi Easily

The Raspberry Pi cannot read analog sensors directly because it lacks an analog-to-digital converter (ADC). To read an analog sensor, connect it to an external ADC chip like MCP3008 and use Python libraries such as spidev to communicate and get sensor values.
📐

Syntax

To read an analog sensor using the MCP3008 ADC on Raspberry Pi, you use the spidev library to communicate over SPI. The main steps are:

  • Open SPI connection
  • Send command to read a specific ADC channel
  • Receive and convert the response to a sensor value

Here is the basic syntax for reading one channel:

python
import spidev

def read_adc(channel):
    spi = spidev.SpiDev()
    spi.open(0, 0)  # Open SPI bus 0, device 0
    spi.max_speed_hz = 1350000

    # MCP3008 protocol: start bit, single-ended mode, channel (3 bits), 5 dummy bits
    cmd = 0b11 << 6 | (channel & 0x07) << 3
    resp = spi.xfer2([cmd, 0, 0])

    # Combine response bytes to get 10-bit value
    result = ((resp[1] & 0x0F) << 8) | resp[2]

    spi.close()
    return result
💻

Example

This example reads the analog value from channel 0 of the MCP3008 connected to a sensor like a potentiometer. It prints the raw 10-bit value repeatedly.

python
import spidev
import time

def read_adc(channel):
    spi = spidev.SpiDev()
    spi.open(0, 0)
    spi.max_speed_hz = 1350000

    cmd = 0b11 << 6 | (channel & 0x07) << 3
    resp = spi.xfer2([cmd, 0, 0])
    result = ((resp[1] & 0x0F) << 8) | resp[2]

    spi.close()
    return result

try:
    while True:
        value = read_adc(0)  # Read channel 0
        print(f"Analog sensor value: {value}")
        time.sleep(1)
except KeyboardInterrupt:
    print("Stopped by user")
Output
Analog sensor value: 512 Analog sensor value: 520 Analog sensor value: 530 ... (updates every second)
⚠️

Common Pitfalls

  • Not using an ADC chip: Raspberry Pi GPIO pins are digital only; connecting an analog sensor directly will not work.
  • Incorrect SPI setup: Make sure SPI is enabled on Raspberry Pi via raspi-config and wiring matches SPI pins.
  • Wrong channel number: MCP3008 has 8 channels (0-7); using invalid channel causes errors.
  • Not closing SPI connection: Always close SPI after reading to avoid resource conflicts.
python
import spidev

# Wrong: Not closing SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000
resp = spi.xfer2([0b11000000, 0, 0])
# Missing spi.close() here

# Right: Close SPI after use
spi.close()
📊

Quick Reference

Summary tips for reading analog sensors on Raspberry Pi:

  • Use MCP3008 or similar ADC chip to convert analog to digital.
  • Enable SPI interface on Raspberry Pi before running code.
  • Use spidev Python library to communicate with ADC.
  • Read from channels 0-7 depending on sensor wiring.
  • Close SPI connection after each read or reuse connection carefully.

Key Takeaways

Raspberry Pi cannot read analog signals directly; use an ADC like MCP3008.
Enable SPI on Raspberry Pi and use the spidev library to communicate with the ADC.
Always specify the correct ADC channel and close SPI connections after reading.
Test sensor readings with simple loops and print values to verify wiring and code.
Common mistakes include missing SPI setup and trying to read analog sensors directly.