ADC Reading Tutorial on the Expander Pi

Created 17/06/2017 | A tutorial showing how to use the ADC inputs on the Expander Pi

This demo shows how to read from the ADC input channels on the Expander Pi

With your Raspberry Pi switched off install the Expander Pi on the Raspberry Pi GPIO port and insert a CR2032 coin battery into the battery holder.  Using the Expander Pi without a battery installed may damage the Expander Pi and will stop it from appearing on the I2C bus.

We will use the AB Electronics python library to talk to the Expander Pi, to download the library visit our Python Library and Demos knowlege base article.

You will need to enable i2c on your Raspberry Pi, see our other tutorial on i2c: I2C, SMBus and Raspbian Linux

The AB Electronics python library uses another library called python-smbus, you can install it using apt-get with the following commands.

sudo apt-get update

sudo apt-get install python-smbus

With the libraries installed and the Raspberry Pi configured to use i2c we can begin building our project.

 

For this tutorial we will start by creating a new python program file called demo-adcread.py. You can use your favourite text editor for writing the program. You can find a complete example of demo-adcread.py in the ABElectronics_Python_Libraries/ExpanderPi/ folder.

At the top of your program you will need to import the ADC class from the ExpanderPi library and time library.

from ExpanderPi import ADC
import time

Now we create an instance of the ADC class

adc = ADC()

We need set the reference voltage. this should be set to the exact voltage measured on the Expander Pi Vref pin

adc.set_adc_refvoltage(4.096)

Now we create a loop to read the voltage from channel 1 in single ended mode and display on the screen

while True:
    # read the voltage from channel 1 in single ended mode and display on the screen
    print adc.read_adc_voltage(1,0)
    time.sleep(0.5)

To run the demo in a console window enter:

python demo_adcread.py

The full code for this demo is:

#!/usr/bin/env python

from ExpanderPi import ADC
import time

"""
================================================
ABElectronics Expander Pi | ADC Read Demo
run with: python demo_adcread.py
================================================

this demo reads the voltage from channel 1 on the ADC inputs
"""


adc = ADC()  # create an instance of the ADC

# set the reference voltage.  this should be set to the exact voltage
# measured on the Expander Pi Vref pin.
adc.set_adc_refvoltage(4.096)

while True:
    # read the voltage from channel 1 in single ended mode and display on the screen
    print(adc.read_adc_voltage(1,0))
    time.sleep(0.5)

 

Shop

Support

Products added to basket

Items

Total (inc shipping)

CheckoutView Basket

Free Shipping on all orders over £30

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.