Skip to content

Connecting to ADC with Raspi

Brendan Smith edited this page Jul 9, 2020 · 5 revisions

To collect information from sensors, analog measurements need to be made.

Overview

The Raspberry Pi does not come with analog read capabilities by default, so an additional device is required. In this section we connect Adafruit's ADS1115 16bit ADC to make analog measurements.

The Raspberry Pi can use it's pins to communicate with other devices. So when there is something the Pi cannot do, an additional device can perform it. Then we just need to establish communication.

Like the RTC, the ADS1115 and Raspberry Pi will communicate via the I2C(inter-integrated circuit) protocol.

Using the ADS1x15

This ADS1x15 setup link will help you assemble the ADC chip.

Adafruit has this excellent ~20 minute tutorial for getting started with their device. Please create a sample voltage with a potentiometer, and connect the variable voltage output to A3 of the ADS1115. If you do not have a potentiometer, a voltage between 0 and 3.3V can be made with a voltage divider.

Now with your voltage(~10-50mV if possible) get out a multimeter and write down the voltage. Also write down the output of sampletest. Now for some unit conversion to see how accurate the ADC really is!

This calculation helps turn our steps back into a voltage.

Here we use 2^15 not 2^16 steps because the full 2^16 actually covers the range -4.096 -> 4.096V, and we only use the positive half of this range.

Adding ADC to our Datalogger

Lets add this to the part6_datalogger.py(here).

import Adafruit_ADS1x15

Variables for the new sensor

adc = Adafruit_ADS1x15.ADS1115()
ADC_GAIN = 16 #1->16, powers of 2. programmable gain, see datasheet for more info
POTENTIOMETER_ADC_PIN = 3

Update the list of headers

csv_headers = ['Date', 'Time','Temperature', 'Humidity', 'Potentiometer mV']

Here is a python function to do the steps to millivolt conversion. Add this before the while loop. It is important that this function declaration is before the function call.

def steps_to_millivolts(adc_output, volt_range, gain, num_steps):
  volts_per_step = volt_range * (1.0 / gain) * (1.0 / num_steps)
  millivolt_equivalent = adc_output * volts_per_step
  return millivolt_equivalent

Add a math import so we can use exponents

import math

And now, add the new sensor to our main loop

while True:
    ...

    #sensor2
    potentiometer_output = adc.read_adc(POTENTIOMETER_ADC_PIN, ADC_GAIN)
    potentiometer_millivolts = steps_to_millivolts(potentiometer_output, 4.096, ADC_GAIN, math.pow(2, 15))
    new_log.append(potentiometer_millivolts)

Let it run!

Connecting Environmental Sensors

Many of the environmental sensors create an "analog signal", or small voltage, to indicate their measurements. And, we now can accurately read a small voltage with our Raspberry Pi.

Under the "Environmental Sensors" section of the wiki, there is a list of analog sensors. For each sensor, there should be a sensor overview and connection instructions.

The Kipp and Zonen's PAR sensor makes for a great example!

From Here

The logger is pretty much complete. We have 2 sensors connected, and we are writing logs with timestamps. From here, there is some formatting cleanup and small changes to get our logger field-ready..

Clone this wiki locally