Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Sensors and Cameras

Analog Sensors and the ADC

The Raspberry Pi has no analog inputs. Add an ADC such as the MCP3008 or ADS1115 to read light, soil moisture and potentiometers, with scaling code.

Many useful sensors are analog: a photoresistor that gets less resistive in bright light, a potentiometer knob, a soil-moisture probe, a sound level meter. They output a voltage that varies smoothly, say from 0 to 3.3 V. The Raspberry Pi, however, has no analog inputs. A GPIO pin sees only high or low. To read an analog signal you need an analog-to-digital converter (ADC), a small chip that measures a voltage and reports it as a number.

What an ADC does

An ADC samples a voltage and returns an integer between 0 and a maximum that depends on its resolution in bits:

Resolution Levels Step for a 3.3 V reference
8 bit 256 about 12.9 mV
10 bit 1024 about 3.2 mV
12 bit 4096 about 0.8 mV
16 bit 65536 about 0.05 mV

More bits mean finer steps. The ADC also has a reference voltage; a reading of 0 means 0 V and the maximum count means the reference. The conversion is:

voltage = reading / max_count * reference

Analog on the Pi

Sensor Outputs a voltage
ADC chip Voltage to number
1 / 2

Two common chips

The MCP3008 is a 10-bit ADC with 8 channels that talks over SPI. It is cheap and very popular in tutorials. The ADS1115 is a 16-bit ADC with 4 channels (it effectively gives 15 usable bits for single-ended readings) and talks over I2C; it also has a programmable gain amplifier for small signals. Pick the MCP3008 for speed and simplicity and the ADS1115 for accuracy.

Wire an MCP3008 like this: VDD and VREF to 3.3 V, AGND and DGND to ground, CLK to SCLK (GPIO11, pin 23), DOUT to MISO (GPIO9, pin 21), DIN to MOSI (GPIO10, pin 19), CS to CE0 (GPIO8, pin 24). Your sensor's output goes to a channel pin such as CH0.

Reading with gpiozero (read-only; SPI must be enabled):

Python
from gpiozero import MCP3008
from time import sleep

pot = MCP3008(channel=0)       # value is 0.0 to 1.0
while True:
    print(round(pot.value, 3), round(pot.value * 3.3, 2), "V")
    sleep(0.5)

The value property is already scaled to the 0 to 1 range. With an ADS1115 you use the Adafruit CircuitPython library (read-only):

Python
import board, busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
chan = AnalogIn(ads, ADS.P0)
print(chan.value, chan.voltage)

Voltage dividers and photoresistors

A photoresistor (LDR) changes resistance, not voltage. To read it, build a voltage divider: put the LDR in series with a fixed resistor between 3.3 V and ground and connect the ADC to the point between them. The voltage there changes as the resistance changes. The divider formula is Vout = Vin * R2 / (R1 + R2).

Python Stand-in: ADC reading to voltage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Scaling to useful units

A number is not information. You usually map the raw reading to something people understand, like "percent brightness" or "percent wet". A calibration records the reading in two known conditions (dry air and a glass of water, say) and converts everything linearly in between. The stand-in below calibrates a soil-moisture sensor and also applies a small moving average to calm noise.

Python Stand-in: calibrate a moisture sensor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A threshold with hysteresis

Turning a pump or a light on and off at one threshold makes it flicker when the reading hovers around the limit. Use hysteresis: switch on below one value and off above a different, higher one. This is the logic behind thermostats and plant waterers.

Python Stand-in: threshold with hysteresis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the pump stays on from 29 up to 46, instead of flickering at 30.

Common pitfalls

  • Wrong reference voltage. If you power an ADC from 5 V, its output reaches the Pi's pins at 5 V levels on MISO. Run ADCs at 3.3 V.
  • Noisy wiring. Keep analog wires short and away from motors and PWM lines.
  • No calibration. Two identical sensors can read very differently; calibrate each one.
  • Sampling too fast over I2C and filling the bus; add a short sleep.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the Raspberry Pi need an external ADC for analog sensors?

    The Pi has no analog inputs, so an ADC converts the voltage to a number.
  2. 2. What is the maximum reading of a 10-bit ADC?

    10 bits give 2^10 = 1024 levels, from 0 to 1023.
  3. 3. Which interface does the MCP3008 use?

    The MCP3008 is an SPI chip; the ADS1115 uses I2C.
  4. 4. What is hysteresis in a threshold controller?

    Separate switch-on and switch-off levels create a stable band.

Summary

  • An ADC turns a voltage into a number; resolution sets how fine the steps are.
  • MCP3008 (10-bit, SPI) and ADS1115 (16-bit, I2C) are popular choices.
  • Use a voltage divider for resistive sensors such as photoresistors.
  • Calibrate, smooth and add hysteresis to make analog data usable.

Next: Camera Module and picamera2