Como ler sinais analógicos no Raspberry Pi usando ADS1015/ADS1115

How to read analog signals on Raspberry Pi using ADS1015/ADS1115

Raspberry Pi is the most popular single-board computer. As it is an embedded microcomputer, data acquisition is almost essential on the Raspberry Pi. Although Pi has several GPIO (general purpose input/output) pins, it does not have any built-in or built-in analog-to-digital converters. Therefore, Pi itself cannot sample data from analog sensors. In embedded applications, analog sensors are often used. In fact, many sensors, such as light dependent resistors (LDRs) and IR sensors, are simply not available in digital form. In this case, analog sensors can interface with the Raspberry Pi in two ways. One is to use an external analog-to-digital converter; the second is to sample data from analog sensors using a microcontroller and transfer all captured data to Raspberry Pi via a serial interface.

Interfacing the Raspberry Pi with a microcontroller just to fetch analog data is not a viable solution. An external ADC is the best solution when the Raspberry Pi operates as a standalone embedded microcomputer and analog data acquisition is required.

Acquiring analog data on Raspberry Pi brings several benefits. It is much easier to apply machine learning algorithms and deep learning models on a microcomputer like Raspberry Pi than on any microcontroller. Microcontrollers are very feature-limited and have limited software tools and APIs for applying machine learning algorithms or implementing deep learning networks. Available machine learning and deep learning software tools for microcontrollers are also limited in their capabilities. For example, computer vision applications often require large RAM and efficient camera sensors for any practical application. Machine learning tools for microcontrollers are not very performant for resource-intensive embedded applications.

ADS1015 ADS1115

This project will explore how to perform analog data acquisition on Raspberry Pi using ADS1115 or ADS1015 ADC chips. In the same context, we will also discuss how we can read analog data from IR sensors on Raspberry Pi for obstacle detection on a Raspberry Pi robot.

Required components

  1. Raspberry Pi 3B/4B x1
  2. ADS1115/ADS1015 ADC x1
  3. IR sensors x1
  4. Breadboard x1
  5. Connecting wires/jumper wires

ADS1115/ADS1015 Pi vs. microcontrollers
As already discussed, we have two ways to sample analog data on Raspberry Pi. Before discussing the use of external ADCs like ADS1115/ADS1015, it is worth noting the difference between the two approaches. When we connect a microcontroller like an Arduino board with Raspberry Pi, we connect a completely separate embedded solution with the microcomputer. If the Arduino board operates at a different voltage level, as most Arduino boards are 5V circuits, we need to cautiously use a TTL logic level converter between the microcontroller and the Raspberry Pi. Otherwise, we will damage the entire Pi, or at least the pins on the board. In case of external ADC, we can drive the ADC from the voltage outputs of the Raspberry Pi.

A microcontroller will work independently with its data sampling cycle. In this case, a Python script on the Raspberry Pi will need to keep up with the microcontroller's sampling speed. This also means that data acquisition control remains with the microcontroller and not the Raspberry Pi. This is sometimes useful when implementing time-critical applications where sensor data must be processed immediately for fast response. In the case of an ADC, data acquisition and sampling rate are controlled by the Raspberry Pi. Some developers feel that the Raspberry Pi running a normal operating system may delay the acquisition and processing of analog data with external ADCs, as other processes may override the currently running Python scripts. However, this problem can be easily solved by delegating analog data detection to a parallel thread, employing multi-threading.

A microcontroller should only be used with Raspberry Pi when multiple time-critical embedded tasks need to be run in parallel. The Raspberry Pi in this case can be deployed on the backend to perform extensive data analysis, implement machine learning or deep learning models on sensor data, manage communication with a cloud platform, and manage IoT (Internet of Things) operations. ).

ADS115/ADS1015 ADC
ADS1015 and ADS1115 are low power, high precision analog-to-digital converters. They are widely used with Raspberry Pi as these chips also operate at a voltage of 3V3. ADS1015 is a 12-bit ADC with four analog input channels. ADS1115 is a 16-bit ADC with four analog input channels.

In this project, we will use ADS1115. The ADS1115 supports two types of data acquisition – single-ended and differential. In single-ended conversion, analog data from each of the four analog input pins is sampled independently. The difference between the voltages on two analog input pins is sampled in a differential mode of operation. The ADS1115 has 16-bit resolution, of which one bit remains reserved for the plus/minus sign of the voltage difference in differential mode, and the remaining 15 bits are used to store converted voltage values. The chip has an I2C interface for serial data communication with a microcomputer/microcontroller.

The sampling rate on the ADS1115 can be set between 8 and 860 samples per second. Higher is the sampling rate; The shorter the time it takes the ADC to capture and convert the analog signal. The chip also has a built-in gain amplifier, amplifying weak voltage signals by 2-3x to 16x times. The conversion (single-ended or differential) can be done in two ways. The conversion is initiated by the Raspberry Pi (or control microcomputer/microcontroller) into a method. The converted value is stored in a register on the ADS1115. The Raspberry Pi then reads the registry. In the second method, the conversion is done continuously by the ADS1115. The signal is sampled at a pre-programmed rate (8~860 sps) and the converted value is repeatedly updated in a register on the ADS1115. The Raspberry Pi must repeatedly read the ADS1115 register according to the programmed sampling rate. If the Raspberry Pi reads the record at a lower rate, it will lose several captured samples. On the other hand, if the Raspberry Pi reads the register at a higher rate, it will register multiple redundant values ​​for the same samples.

ADS1115 has the following pin diagram.

ADS1015 ADS1115 Pin Configuration

ADS115/ADS1015 wiring with Raspberry Pi
ADS1115 and ADS1015 sample analog data from sensors and transfer the converted values ​​to Raspberry Pi via the I2C interface. Both chips use the same I2C protocol and have the same pin configuration. To connect the ADS1015/ADS1115, follow the circuit connections summarized in the table below.

Enabling I2C on Raspberry Pi The analog output of a sensor must be connected to A0, A1, A2 or A3 of the ADS1015/ADS1115. A maximum of four sensors can be connected to the ADS1015/ADS1115. The sensor can receive voltage from the Raspberry Pi itself. If an external source powers the sensor, it must not exceed 3.3V.

ADS1015 and ADS1115 communicate with Raspberry Pi via the I2C protocol. Before interfacing the ADS1015/ADS1115 with Raspberry Pi, the I2C port must be enabled on the microcomputer. To do this, navigate to Preferences -> Raspberry Pi Configuration.

Raspberry Pi configuration window A configuration window will open as shown below.

Interfaces in Raspberry Pi Configuration

Select the Interfaces tab. Enable I2C and click ok.

I2C port on Raspberry Pi And that. Now the Raspberry Pi can communicate via an I2C port. The I2C port on the Raspberry Pi is shown in the image below.

To learn more about I2C communication on Raspberry Pi. You may also be interested in knowing how the I2C protocol works.

Adafruit ADS1x15 Raspberry Pi python library
Fortunately, we have the Adafruit ADS1x15 Python library available to work with ADS1015 and ADS1115. We don't need to write any Python script from scratch to interface ADS1015/ADS1115 with Raspberry Pi.

Installing the ADS1x15 python library on Raspberry Pi
The Adafruit ADS1x15 Python library can be installed on the Raspberry Pi manually or via the PiP package manager. To install the library from source code, open the Bash Terminal by navigating to Accessories->Terminal or simply clicking the Terminal icon; then run the following commands.

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus git
CD ~
clone
CD Adafruit_Python_ADS1x15
sudo python setup.py install

To install the library using the PiP package manager, open the Bash Terminal by navigating to Accessories->Terminal or simply clicking the Terminal icon; then run the following commands.

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus python-pip
sudo pip install adafruit_ads1x15

If you are using Python3, you will need to install the library using pip3 as follows.
sudo pip3 install adafruit_ads1x15

Once the library is successfully installed in your Python installation, you can use it by simply importing the library into a script.

Python code for analog data acquisition using ADS1115/ADS1015
To use the Adafruit ADS1x15 library in a python script, import the library using the following statement.

import Adafruit_ADS1x15

So, you need to instantiate an object of class Adafruit_ADS1x15 using the following statement. The instance is named 'adc' here; can be any identifier.

adc = Adafruit_ADS1x15.ADS1115

Next, you need to set the gain of the integrated amplifier. The gain needs to be set according to the Programmable Amplifier Gain table provided in the ADS1015/ADS1115 datasheet. A quick reference of amplifier gain and desired detection voltage range is summarized in the table below.


Gain can be set using the following instruction.

GAIN = 1

The converted voltage reading ranges from -32768 to +32767 for ADS1115 and -2048 to +2047 for ADS1015. A value of 0 refers to ground level. The actual detected voltage is determined by the proportionality of the voltage range defined by the amplifier gain. For example, if the gain is set to 1, the value of 32767 refers to a voltage of 4.096V on the ADS1115. Any value in the range -32768~32767 refers to an analog voltage (4.096/32767)*a. The analog reading of the A0 analog input of the ADS1115/ADS1015 can be read using the following instruction.
value = adc.read_adc(0, gain=GAIN)

Similarly, analog readings from analog inputs A1~A3 of the ADS1015/ADS1115 can be read using the following instructions.
value = adc.read_adc(1, gain=GAIN)
value = adc.read_adc(2, gain=GAIN)
value = adc.read_adc(3, gain=GAIN)

Here 'value' is a variable in the Python script. ADC readings can be obtained in a loop if multiple analog sensors are connected to the Raspberry Pi via ADS1015/ADS1115. Actual voltages can be calculated using the equation given above. Below is an example of deriving the actual voltage reading.
GAIN = 1
value = adc.read_adc(0, gain=GAIN)
analog_voltage = value*(4,096/32767)

Interfacing IR sensors with Raspberry Pi via ADS1115/ADS1015
IR sensors are commonly used sensors that are only available in analog form. IR sensors are widely used in Raspberry Pi robots for line following or obstacle detection. IR sensors can be easily connected to Raspberry Pi via ADS1015/ADS1115. The IR sensor has three pins – VDD, GND and Out. Connect the Out pin of the IR sensor with any analog input pin of the ADS1015/ADS1115 and supply positive and negative power to the sensor from the Raspberry Pi itself. Then interface the ADS1015/ADS1115 with Raspberry Pi as mentioned above. This way, an IR sensor, an analog sensor is now connected to the Raspberry Pi.

Python script to read IR sensor output on Raspberry Pi
Run the following script to fetch analog IR sensor readings from the Python console.
import time
import Adafruit_ADS1x15
adc = Adafruit_ADS1x15.ADS1115
# Choose a gain of 1 to read voltages from 0 to 4.09V.
# Or choose a different gain to change the range of voltages read:
# – 2/3 = +/-6.144V
# – 1 = +/-4.096V
# – 2 = +/-2.048V
# – 4 = +/-1.024V
# – 8 = +/-0.512V
# – 16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more information on gain.

GAIN = 1
while True:
value = adc.read_adc(0, gain=GAIN)
print(value)

time.sleep(0.5)

Results
The IR sensor emits a higher voltage when there is no obstacle in its path. When the sensor faces an obstacle, its output is reduced. The ADC reading when there is no obstacle in the path of the IR sensor is 24550 ~ 24729. With an amplifier gain of 1, these readings correspond to a voltage of 3.06~3.09V. The ADC reading when an obstacle blocks the IR sensor is 660~670. With an amplifier gain of 1, these readings correspond to a voltage of 0.081~0.083V. It should be noted that the output of the IR sensor depends on its calibration by the integrated potentiometer.

Conclusion
ADS1015 and ADS1115 are low power, high resolution analog to digital converters. They can be easily interfaced with Raspberry Pi and communicated via the I2C protocol. These ICs can detect a maximum of four analog voltages simultaneously in a single-ended configuration. Therefore, making ADS1015 and ADS1115 the best ADCs for Raspberry Pi robots.

Related Content

Back to blog

Leave a comment

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