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.
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
- Soil Moisture Sensor
- Light Intensity Sensor
- 1kΩ resistor, a small breadboard, and some jumper wires
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:
Access your Omega’s command line, make sure you’re connected to the internet, and run the following:
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 the ADC Expansion module.|
|from OmegaExpansion import AdcExp|
|# create ADC Expansion object|
|adc = AdcExp.AdcExp()|
|#infinite loop to read sensors and display data|
|# 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|
|Temperature\t| %.02f C|
|Soil Moisture\t| %.02f %%|
|Light Intensity\t| %d lux|
|'''%(temp, soil, lux))|
|#wait half a second|
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!