Skip to content

Connecting to ADC with Raspi

Brendan Smith edited this page Aug 31, 2016 · 5 revisions

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

The Raspberry Pi does not come with analog read capabilities by default, so an additional device is required. Here we are using Adafruit's ADS1115 16bit ADC

The idea is that 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. 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 datalogger.py.

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(potentiomter_adc_pin, adc_gain)
    millivolts = steps_to_millivolts(potentiometer_output, 4.096, adc_gain, math.pow(2, 15))
    new_log.append(millivolts)

Let it run!

Clone this wiki locally