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

Step 1: 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.

Step 2: Follow the instructions on how to install and configure I2C on Raspbian Linux.

Step 3: Follow the instructions on how to install and configure SPI on Raspbian Linux.

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 ABE_ExpanderPi library and time library.

from ABE_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/python

from ABE_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.