How to Use ADS1115 with the Raspberry Pi (Part 1)

 Are you looking to expand the capabilities of your Raspberry Pi beyond simple digital tasks? One common limitation of the Raspberry Pi is its inability to directly read analog signals, which are crucial for a wide range of sensors, including environmental sensors like the MQ-135 gas sensor. Enter the ADS1115, a versatile and powerful analog-to-digital converter that unlocks the full potential of sensors with analog outputs. This guide will walk you through integrating the ADS1115 with your Raspberry Pi, using the MQ-135 gas sensor as a practical example to monitor air quality.

The Raspberry Pi is a fantastic tool for digital computing tasks, but it lacks built-in support for analog inputs. This limitation can be a barrier when dealing with sensors that output analog signals, which are essential for capturing nuanced real-world data. The ADS1115 addresses this gap by providing precise 16-bit analog-to-digital conversion, allowing the Raspberry Pi to process detailed analog data from sensors like the MQ-135, which measures various gases in the air.

Before reading the remainder, be sure to subscribe and support the channel if you have not!
Subscribe:
Support:
Hire me at UpWork to build your IoT projects:

Step 1-) Assemble the Components

Gather all necessary components:

  • Raspberry Pi (any model with GPIO pins)
  • ADS1115 ADC module
  • MQ-135 gas sensor
  • Jumper wires
  • Breadboard (optional, but recommended for easier prototyping)

You can Purchase the ADS1115 Pre-Soldered at ShillehTek!

 - Buy it on our Store:

Buy ADS1115 on ShillehTek

- Or buy it on Amazon:

Buy ADS1115 on Amazon

Step 2-) Set Up the Hardware

See diagram for exact details.

  • Connect the ADS1115 to the Raspberry Pi:

    • VDD to Pi 3.3V: Connect the VDD pin of the ADS1115 to one of the 3.3V pins on the Raspberry Pi.
    • GND to Pi GND: Connect the GND pin of the ADS1115 to one of the GND pins on the Raspberry Pi.
    • SCL to Pi SCL (GPIO 3): Connect the SCL pin of the ADS1115 to GPIO 3 (SCL) on the Raspberry Pi.
    • SDA to Pi SDA (GPIO 2): Connect the SDA pin of the ADS1115 to GPIO 2 (SDA) on the Raspberry Pi.
  • Connect the MQ-135 Sensor to the ADS1115:

    • AOUT to ADS1115 A0: Connect the Analog Output (AOUT) pin of the MQ-135 to the A0 pin on the ADS1115. This allows the ADS1115 to read the analog output from the MQ-135 sensor.
  • Power the MQ-135 Sensor:

    • VCC to Pi 5V: Connect the VCC pin of the MQ-135 to one of the 5V pins on the Raspberry Pi. This supplies the necessary power to the MQ-135 sensor.
    • GND to Pi GND (Additional): Connect another GND pin of the MQ-135 to a GND pin on the Raspberry Pi to ensure a common ground for all components.
  • Assembly Tips:

    • Use a Breadboard: Utilize a breadboard and jumper wires for easier and safer connections. This setup allows for modifications without soldering and provides a clear visual of all connections.
    • Ensure Secure Connections: Check that all connections are secure to prevent loose connections that can lead to erratic readings or hardware damage.

 

Step 3-) Configure the Raspberry Pi for I2C Communication

To communicate with the ADS1115, we need to use I2C communication which is characteristic for these applications. If this is your first time using sensors with the Raspberry Pi, chances are that you do not have I2C enabled on the device. You can read more about I2C here if you like.

What is I2C?

  • Enable I2C on the Raspberry Pi:
    • Open a terminal window on your Raspberry Pi.
    • Enter sudo raspi-config.
    • Navigate to Interfacing Options, select I2C, and enable it.
    • Exit the configuration tool and reboot the Raspberry Pi if prompted.
  • Verify that I2C devices are recognized:
    • Run sudo i2cdetect -y 1 in the terminal. You should see the address of the ADS1115 listed (typically 0x48 unless the address pin is configured differently).

Step 4-) Install Necessary Python Libraries

To interface the ADS1115 ADC with our Raspberry Pi using Python, we need to install specific libraries that simplify the process of reading analog data. These libraries handle the complexities of digital-to-analog conversion and allow us to work with the data in a more straightforward, human-readable format.

Install the Adafruit ADS1x15 library to interface with the ADS1115 ADC:

  • Open a terminal on your Raspberry Pi.
  • Update your package list: sudo apt-get update
  • Install pip if it is not already installed: sudo apt-get install python3-pip
  • Install the Adafruit library: sudo pip3 install adafruit-ads1x15

Verify Installation: Confirm that the library is installed correctly by importing it in a Python shell

Step 5-) Write the Python Script to Read Sensor Data

  • Create a new Python script using a text editor or the Python IDE on your Raspberry Pi:
    • Write a script to initialize the ADS1115 and continuously read the voltage from the MQ-135 sensor.
    • Use the provided sample code, adjusting parameters as necessary based on your specific setup.
    • Save the script and run it to start monitoring the air quality.

FYI, you can use any analog output device you want, we are just using the MQ135 in our case because it is very common and easy to interpret.


import time
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn


i2c = busio.I2C(board.SCL, board.SDA)

# Create the ADS object and specify the gain
ads = ADS.ADS1115(i2c)
ads.gain = 1 
chan = AnalogIn(ads, ADS.P0)

# Continuously print the values
while True:
    print(f"MQ-135 Voltage: {chan.voltage}V")
    time.sleep(1)

You can run this script in Python now, if you have everything setup properly you should begin to see values in the terminal window. To test the analog readings are making sense, you can place the MQ135 sensor above a bottle of alcohol to test that the sensor detects properly. If so, you should see an increase in the voltage values in the terminal which indicates the ADC is working properly as well. 

Voltage increases to above 1V after alcohol exposure!

Step 6-) Adjust Settings as Needed

Proper calibration of the ADS1115's gain is crucial for optimizing the accuracy and reliability of your sensor readings. Gain adjustment helps tailor the ADC’s input sensitivity to the output range of the MQ-135 sensor, thereby enhancing the precision of your data.

  • Understanding Gain Settings: The gain setting on the ADS1115 defines the full-scale range of input voltages it can accurately read. The gain setting acts as a multiplier for the input signal, allowing the ADC to detect smaller changes in voltage at higher gain settings. Here’s what happens with different gain settings:

    • Higher Gain (e.g., Gain = 4 for ±1.024V range): Increases the ADC’s sensitivity to smaller voltage changes. Use this if the MQ-135 outputs low voltage levels, as it allows for finer resolution. However, be cautious, as setting the gain too high for the sensor's output voltage might cause the input signal to exceed the ADC’s maximum range, leading to signal clipping.
    • Lower Gain (e.g., Gain = 1 for ±4.096V range): Decreases sensitivity but allows higher voltage inputs without saturation. This setting is suitable if the sensor outputs higher voltage levels, ensuring that the ADC does not clip the signal at its input.
  • How to Adjust Gain:

    • Open your Python script where the ADS1115 is initialized.
    • Locate the line where the gain is set, typically ads.gain = 1.
    • Modify this line to adjust the gain to a level suitable for your sensor's output. For example, if you notice that the sensor’s voltage rarely exceeds 1V and you want more precision, set ads.gain = 4.
    • Save the changes and rerun your script to observe the effects of the gain adjustment.
  • Monitor the Output: After adjusting the gain, monitor the output closely. Look for these indicators:

    • Saturation: If all readings hit the same maximum or minimum value, the gain might be too high, causing the voltage to saturate.
    • Resolution: If the readings are more detailed and provide finer distinctions with smaller changes in detected gases, your gain setting is likely well-tuned.
  • Iterative Adjustments: Adjusting the gain may require several iterations to find the perfect balance. Experiment with different settings while monitoring the output to determine the best gain setting for your application.

This step ensures that you are using the ADS1115 to its fullest potential, accurately capturing and analyzing the analog signals from the MQ-135 sensor. Adjusting the gain properly can significantly affect the quality of your data, making your air quality monitoring project more reliable and effective.

Conclusion

We've explored how to enhance your Raspberry Pi projects by integrating the ADS1115 ADC, using the MQ-135 gas sensor as a practical example for monitoring air quality. This setup not only broadens the capabilities of your Raspberry Pi by enabling it to read analog signals but also improves the precision and utility of your environmental monitoring projects.

By carefully following the steps outlined, you’ve learned how to set up the hardware, install necessary software, and adjust settings to optimize the performance of your sensor system. Whether you're a hobbyist, a student, or a professional, the skills you've gained will serve as a solid foundation for more advanced projects involving a variety of sensors and data types.

Expand Your Knowledge and Skills If you enjoyed this tutorial and are eager to dive deeper into Raspberry Pi applications or want to explore other sensors and electronics projects, don’t hesitate to subscribe. By subscribing, you’ll get access to a wealth of resources, including:

  • Advanced Tutorials: Learn more complex setups and integrations.
  • Project Ideas: Get inspired with new project ideas that challenge your skills.
  • Community Insights: Join a community of like-minded enthusiasts sharing tips, tricks, and support.

Stay Connected Subscribing will keep you updated on the latest posts, updates, and community projects. Your journey into electronics and environmental monitoring is just beginning, and there’s so much more to discover. Subscribe today and transform your curiosity into expertise.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Learn how to safely control high-power components like a 5V laser diode using the Raspberry Pi Pico W and a...

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.