It's easy to use the ADS1115 and ADS1015 ADC with CircuitPython and the Adafruit CircuitPython ADS1x15 module.  This module allows you to easily write Python code that reads the analog input values.

You can use this ADC with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up the ADC to your board exactly as shown on the previous pages for Arduino using an I2C interface.  Here's an example of wiring a Feather M0 to the ADS1x15 with I2C:

  • Board 3V (red wire in STEMMA QT version) to ADS1x15 VDD - Remember the maximum input voltage to any ADC channel cannot exceed this VDD 3V value!
  • Board GND (black wire in STEMMA QT version) to ADS1x15 GND
  • Board SCL (yellow wire in STEMMA QT version) to ADS1x15 SCL
  • Board SDA (blue wire in STEMMA QT version) to ADS1x15 SDA

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired to the ADS1015 with I2C:

  • Pi 3V (red wire in STEMMA QT version) to ADS1x15 VDD - Remember the maximum input voltage to any ADC channel cannot exceed this VDD 3V value!
  • Pi GND (black wire in STEMMA QT version) to ADS1x15 GND
  • Pi SCL (yellow wire in STEMMA QT version) to ADS1x15 SCL
  • Pi SDA (blue wire in STEMMA QT version) to ADS1x15 SDA

CircuitPython Installation of ADS1x15Library

Next you'll need to install the Adafruit CircuitPython ADS1x15 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  For example the Circuit Playground Express guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_ads1x15
  • adafruit_bus_device

You can also download the adafruit_ads1x15 folder from its releases page on Github.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_ads1x15 and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of ADS1x15 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-ads1x15

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the ADC we will initialize it and read the ADC channel values interactively using the REPL.  First run the following code to import the necessary modules and initialize the I2C bus:

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

Next, import the module for the board you are using. For the ADS1015, use:

import adafruit_ads1x15.ads1015 as ADS

OR, for the ADS1115, use:

import adafruit_ads1x15.ads1115 as ADS

Note that we are renaming each import to ADS for convenience.

The final import needed is for the ADS1x15 library's version of AnalogIn:

from adafruit_ads1x15.analog_in import AnalogIn

which provides behavior similar to the core AnalogIn library, but is specific to the ADS1x15 ADC's.

OK, now we can actually create the ADC object. For the ADS1015, use:

ads = ADS.ADS1015(i2c)

OR, for the ADS1115, use:

ads = ADS.ADS1115(i2c)

Now let's see how to get values from the board. You can use these boards in either single ended or differential mode. The usage for the two modes are slightly different, so we'll go over them separately.

Single Ended Mode

For single ended mode we use AnalogIn to create the analog input channel, providing the ADC object and the pin to which the signal is attached. Here, we use pin 0:

chan = AnalogIn(ads, ADS.P0)

To set up additional channels, use the same syntax but provide a different pin.

Now you can read the raw value and voltage of the channel using either the the value or voltage property.

print(chan.value, chan.voltage)

Differential Mode

For differential mode, you provide two pins when setting up the ADC channel. The reading will be the difference between the two. Here, we use pin 0 and 1:

chan = AnalogIn(ads, ADS.P0, ADS.P1)

You can create more channels by doing this again with different pins. However, note that not all pin combinations are possible. See the datasheets for details.

Once the channel is created, getting the readings is the same as before:

print(chan.value, chan.voltage)

Gain

Both the ADS1015 and the ADS1115 have a Programmable Gain (PGA) that you can set to amplify the incoming signal before it reaches the ADC. The available settings and associated Full Scale (FS) voltage range are shown in Table 3 of the datasheet.

You set the gain to one of the values using the gain property, like this:

ads.gain = 16

Note that setting gain will affect the raw ADC value but not the voltage (expect for variance due to noise). For example:

>>> ads.gain
1
>>> chan.value, chan.voltage
(84, 0.168082)
>>> ads.gain = 16
>>> ads.gain
16
>>> chan.value, chan.voltage
(1335, 0.167081)
>>> 

The value changed from 84 to 1335, which is pretty close to 84 x 16 = 1344. However, the voltage returned in both cases is still the actual input voltage of ~0.168 V.

Single Mode

Single mode (not to be confused with single-ended mode) always waits until the analog to digital conversion is completed by the ADC to read the value. This is slower, but it is really the only way to read from more than one pin on the ADC. This is the default mode, but if you ever need to manually set it, here's how:

ads.mode = Mode.SINGLE

Continuous Mode

In continuous mode, the board will read the latest value that the ADS1x15 device has converted. This is faster as it does not have to wait for the ADC to finish converting the value, but it only really works when reading data from one of the four pins on the ADC. You can set it to continuous mode using the line below:

ads.mode = Mode.CONTINUOUS

Challenges to Reading Quickly

The best approach to reading data from the ADC quickly would be to use interrupts which would enable the digital value to be recorded from the desired pin as soon as the ADC has converted it. However, CircuitPython does not support the use of interrupts, so this isn't possible. To partially solve this, the continuous mode was added, which will always return the most recently converted value with very low latency since it does not wait for the new value to be ready.

More Info

The above examples cover the basic setup and usage using default settings. For more details, see the documentation.

This guide was first published on Nov 29, 2012. It was last updated on Mar 08, 2024.

This page (Python & CircuitPython) was last updated on Mar 08, 2024.

Text editor powered by tinymce.