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 iotSensor.py 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 os.name == 'nt' else 'clear')
# write sensor readings to screen
print('''
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 iotSensor.py 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...

Arduino Mkr Eth Shield - Accessories And Breakout Boards

Arduino MKR ETH Shield

£25.99 inc VAT
£21.66 ex VAT
Arduino Mkr 485 Shield - Accessories And Breakout Boards

Arduino MKR 485 Shield

£31.99 inc VAT
£26.66 ex VAT
Arduino Mkr Can Shield - Accessories And Breakout Boards

Arduino MKR CAN Shield

£31.99 inc VAT
£26.66 ex VAT
Arduino Mkr Mem Shield - Accessories And Breakout Boards

Arduino MKR Mem Shield

£19.99 inc VAT
£16.66 ex VAT
Arduino Mkr Connector Carrier - Accessories And Breakout Boards

Arduino MKR Connector Carrier

£19.99 inc VAT
£16.66 ex VAT
Arduino Mkr Vidor 4000 - Fpga

Arduino MKR Vidor 4000

£68.99 inc VAT
£57.49 ex VAT
Arduino Mkr Wifi 1010 - Cortex Dev Boards

Arduino MKR WiFi 1010

£31.99 inc VAT
£26.66 ex VAT
Redbear Link Shield - Accessories And Breakout Boards

RedBear Link Shield

£14.99 inc VAT
£12.49 ex VAT