How to build IoT sensors using Python and the Onion ADC Expansion

To go along with the launch of the new Onion ADC Expansion board we've got a great blog post made by Onion on how they've used it in a project.

Follow along to learn how to build an IoT sensor using Python and the Omega2 ADC Expansion! We built our sensor to be able to track the baby tea tree we have in our office, but this IoT sensor setup is a great template for building your own IoT sensor and tracking data that’s important to you.

Parts List:

The IoT Computer components:

For our project we used the following sensors, but the ADC Expansion is flexible and supports many analog sensors:

  • Temperature Sensor
    • TMP36
  • Soil Moisture Sensor
  • Light Intensity Sensor
    • Photoresistor
    • 1kΩ resistor, a small breadboard, and some jumper wires


Physical Setup

The wiring with the ADC Expansion is very straight-forward: connect each sensor’s signal, power, and ground to the ADC Expansion. Each sensor should be connected to a different input channel on the ADC Expansion.

Since our light sensor is based on a photoresistor, we’ll need to make a voltage divider to sample the light intensity. See our guide on using a photoresistor for details.

Position your IoT sensors so you can track the data you’re interested in. We wanted to be able to keep an eye on our office tea tree:


Install Python

Access your Omega’s command line, make sure you’re connected to the internet, and run the following:

opkg update

opkg install python-light python-adc-exp

The program will first instantiate an object to interact with the ADC Expansion, and then it will run an infinite loop that will read the voltage from each sensor, convert it to meaningful measurements for each sensor, and display the measurements.

Create a file on your Omega and populate it with this code:

import time
import os
# Import the ADC Expansion module.
from OmegaExpansion import AdcExp
# create ADC Expansion object
adc = AdcExp.AdcExp()
#infinite loop to read sensors and display data
while 1:
# read ADC channels
a0 = adc.read_voltage(0)
a1 = adc.read_voltage(1)
# a2 = adc.read_voltage(2)
a3 = adc.read_voltage(3)
# convert temperature sensor voltage to temperature
temp = (a0 - 0.5) * 100
# convert soil sensor voltage to percentage
soil = (a1 / 5) * 100
# convert light sensor voltage to lux
rPhoto = 5.0 / a3 * 1000 - 1000
lux = 500/(rPhoto/1000)
# clear screen
os.system('cls' if == 'nt' else 'clear')
# write sensor readings to screen
Temperature\t| %.02f C
Soil Moisture\t| %.02f %%
Light Intensity\t| %d lux
'''%(temp, soil, lux))
#wait half a second
time.sleep (0.5)


Running the program

Let’s run the Python program by entering python into the command line. You will see something like this:

The sensor readings will update twice a second, providing sensor real-time data!


Previous article How to make a bookcase light with the Light Up Board
Next article Getting started with the Onion Servo (PWM) Expansion

Check out our latest products...

TFT LCD Breakout - 1.8 (128x160) (LCD-15143) - LCD Displays
Qwiic Scale - NAU7802 (SEN-15242) - Sensor

Qwiic Scale - NAU7802 (SEN-15242)

£13.99 inc VAT
£11.66 ex VAT
VEML7700 Lux Sensor - I2C Light Sensor (ID:4262) - Visible Light
11x7 LED Matrix Breakout - LED Displays

11x7 LED Matrix Breakout

£8.99 inc VAT
£7.49 ex VAT
What The Flex - Accessories and Breakout Boards

What The Flex

£5.99 inc VAT
£4.99 ex VAT
GNSS Chip Antenna Evaluation Board (GPS-15247) - GPS
Molex Flexible GNSS Antenna - U.FL (Adhesive) (GPS-15246) - GPS
GPS Breakout - Chip Antenna SAM-M8Q (Qwiic) (GPS-15210) - GPS