In Python on RPi with GrovePi+ we have two ways of reading out data from the sensor, depending weather the sensor is connected to Analog or Digital port.
All the necessary python functions for GrovePi+ are contained in the grovepi library. import grovepi
Analog sensor data reading
All the sensors connected to Analog ports are accessed by using the same function. Only difference between different analog sensors are in how you translate the sensor output value into the equivalent phenomenon value (for example: analogSensorValue –> SoilMoistureValue). Formulas for this can be found on the Wiki page of a specific sensor.
analogPortNumber = 1 # Sensor connected to port A1
grovepi.pinMode(analogPortNumber,"INPUT") # initialise port on number 1 for INPUT
# Run continuous loop
while True:
analogSensorValue = grovepi.analogRead(analogPortNumber) # Read sensor value from port A1
# Here Do Something!
As any sensor reading function might throw and error, due to sensor malfunction, it is highly recommended to use Try-Except block(s)! This will be shown in examples below.
Example: Rotary Angle sensor on A0
# Import necessary library
import grovepi
import time
# Connect the Grove Rotary Angle Sensor to analog port A0
# SIG,NC,VCC,GND
potentiometer = 0
grovepi.pinMode(potentiometer,"INPUT")
# Allow 1 sec for board to initialise
time.sleep(1)
# Reference voltage of ADC is 5v
adc_ref = 5
# Vcc of the grove interface is normally 5v
grove_vcc = 5
# Full value of the rotary angle is 300 degrees, as per it's specs (0 to 300)
full_angle = 300
while True:
try:
# Read sensor value from potentiometer
sensor_value = grovepi.analogRead(potentiometer)
# Calculate voltage
voltage = round((float)(sensor_value) * adc_ref / 1023, 2)
# Calculate rotation in degrees (0 to 300)
degrees = round((voltage * full_angle) / grove_vcc, 2)
print("sensor_value = %d voltage = %.2f degrees = %.1f" %(sensor_value, voltage, degrees))
except Exception as e:
# Output error in case exception arises
print ("Error: " + str(e))
# Wait 1 second before reading next measurement
time.sleep(1)
Digital sensor data reading
In the context of GrovePi sensors connected to Digital port, sensor specific value-read functions have been developed. For the student kit of Smart Urban Environments course, we have two sensors that can be connected to D ports of GrovePi+.
Example: Temp&Humidity (blue) sensor data read at D2
For reading out Temp and Humid. we use predeveloped function from the grovepi package. This function is grovepi.dht(digital_port_number, temp_humidity_sensor_type)
where temp_humidity_sensor_type is 0 for blue sensor and 1 for white.
# This example uses the blue colored sensor.
import grovepi
import time
# Connect the Grove Temperature & Humidity Sensor Pro to digital port D2
# SIG,NC,VCC,GND
dhtSensor = 2 # The (digital humidity & temperature) Sensor goes on digital port 2.
# temp_humidity_sensor_type
# Kit comes with the blue sensor.
blue = 0 # The Blue colored sensor.
while True:
try:
# This example uses the blue colored sensor.
# The first parameter is the port, the second parameter is the type of sensor.
[temp,humidity] = grovepi.dht(dhtSensor, blue) # Function returns temperature and humidity
print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))
time.sleep(1) # Wait 1 sec before nex readout
except Exception as e:
print ("Error: "+str(e))
Example: Dust sensor on D4
Dust sensor must be connected to port D4! Keep in mind that example here is slightly different then in the SEEED STUDIO Wiki page. This is due to development of new functions for data collection from the dust sensor. In order to read data from it, we use grovepi.dust_sensor_read(dust_sensor_digital_port_number)
. Additionally we have to enable the dust sensor before using it, by calling grovepi.dust_sensor_en(dust_sensor_digital_port_number)
.
This sensor uses counting method to measure dust concentration, not weighing method, and the unit is pcs/L or pcs/0.01cf.
Please pay attention to the warnings listed here:
Please keep it upright.
3 min preheat time is required while using for the first time.
Arbitrary operation may cause unexpected damage.
Following widgets (red rectangle marked) is used only for the factory setting. Please DO NOT change the default configuration.
The function dust_sensor_read, might lock/block sometimes… In this case the best is to restart you script!**
import time
import grovepi
# Connect the Grove Dust Sensor to digital port D4 (Only works on D4!)
# SIG,_,VCC,GND
dustSensor = 4 # Must be on port D4!
# First enable dust sensor
grovepi.dust_sensor_en(dustSensor) # enable dust sensor
time.sleep(180) # Allow some time for sensor to initialise (first time 3min is recommended)
msg = '' #initialise variable for storing text value of a low-pulse occupancy
while True:
try:
# This example uses the blue colored sensor.
[lowpulseoccupancy, lpo_percentage, concentration] = grovepi.dust_sensor_read(dustSensor)
# (optional) check if values are correct (higher than 0)
if lowpulseoccupancy > 0:
msg = "Concentration = %.02f pcs/0.01cf " % (lowpulseoccupancy)
print(msg)
time.sleep(1)
except Exception as e:
print ("Error: "+str(e))