DEV Community

Cover image for Raspberry Pi - ADC
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi - ADC

The Raspberry Pi has no analog input pins, so it can't read a potentiometer or a gas sensor directly - every GPIO pin only knows about HIGH and LOW. The fix is an external Analog-to-Digital Converter. This walks through wiring an ADS1115 module to a Pi and reading sensor values over I2C from Python.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi - ADC

What you need

Wiring

Raspberry Pi - ADC wiring diagram

The ADS1115 is a 16-bit, 4-channel ADC that talks to the Pi over I2C, so it only needs a handful of connections for power and data plus one input pin per sensor. This tutorial wires up a potentiometer as the example analog source, but the same module and setup work for a light sensor, a water sensor, or a soil moisture sensor since they all just output a varying voltage between 0 and 3.3V.

Because the ADS1115 has four input channels, you can wire up to four analog sensors to one module at the same time and read them independently. If your breadboard wiring gets messy, a screw terminal block shield for the Pi makes the connections more secure and easier to manage.

How it works

The ADS1115 measures an analog voltage between 0 and 3.3V and converts it into a 16-bit digital number, giving you much finer resolution than a simple on/off reading. It communicates with the Raspberry Pi over I2C, a two-wire protocol that lets multiple devices share the same data lines, which is why the module needs so few connections.

Before any of this works, I2C has to be turned on in the Pi's configuration, since it's disabled by default.

Code

On the software side, the Adafruit_ADS1x15 library handles the I2C communication with the module. The script creates an ADS1115 object, reads the 16-bit value from the channel the sensor is wired to, and prints it out once a second in a loop, so you get a continuous stream of readings in the terminal as you turn the potentiometer or change the sensor's conditions.

raspberry-pi-adc.py

"""
This Raspberry Pi code was developed by newbiely.com
This Raspberry Pi code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-adc
"""


import time
import Adafruit_ADS1x15

# Create an ADS1115 ADC object
adc = Adafruit_ADS1x15.ADS1115()

while True:
    analog_value = adc.read_adc(0, gain=1)  # Read from channel 0
    print(f"Analog Value: {analog_value}")
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If you see no output or errors instead of readings, double check that I2C is actually enabled in raspi-config under Interfacing Options, and that you rebooted the Pi afterward for the change to take effect. Also confirm the Pi has internet access the first time you run the script, since the Adafruit_ADS1x15 library needs to be installed before it can be imported.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-adc

Top comments (0)