In this recipe, we will be using the upm library to interface the Grove Light sensor with the Galileo/Edison using python.
The light sensor is made up of primarily of LDR and opamp:
“LDR” stands for light dependent resistor (in above schematic, it is marked as “LIGHT”) as the name suggests, it is made up of a material that changes it resistance depending on the intensity of the ambient light.
Normally, the resistance decreases when the ambient light intensity increases.
Theoperational amplifier(op amp) in the Light sensor Grove module is configured in voltage follower mode. Which means that the opamp will output whatever voltage is applied at the “+” input. In case if you are wondering if opamp just outputs the voltage then why use it at all, then the reason behind that is, it acts as"buffer"that provides us with a signal with reasonable strength for us to measure.
The LDR along with resistor R1 forms the voltage dividercircuit applied at “+” input. The LDR is connected between Vcc and the rest of the voltage divider circuit. Now with the varying resistance of the LDR (with light intensity) which is part of the voltage divider circuit, the current flowing through LDR and hence the voltage at point “+” changes (in accordance to ohm’s law), this is output by the opamp and this is what will be measured by the Galileo/Edison using ADC.
Connect the light sensor to port A0 and LED to port D5 either using the Grove LED module or on a breadboard via the current limiting resistor as shown in the schematic.
#!/usr/bin/pythonimport pyupm_groveimport mraaimport timeimport sys"""This script demonstrates the usage of the light sensor using upmsetup:The light sensor is connected to port A0 and an LED is connected to portD5.Demo:start the application in the command line by using following command:python light_sensor.pyWhile running the demo, you should see a bar appear in the console whoselength ins controlled by the intensity of the ambient light and also the LED connected at port D5 should light more brightly if the intensity of the ambient light is less and vice versa.You can exit this demo by hitting ctrl+c"""LIGHT_SENSOR_PIN=0 # Analog input port where the the light sensor is connectedMAX_LIGHT = 50LED_PWM_PIN=5def main(): light = pyupm_grove.GroveLight(LIGHT_SENSOR_PIN) pwm = mraa.Pwm(LED_PWM_PIN) pwm.period_us(5000) # Set the period as 5000 us or 5ms pwm.enable(True) # enable PWM pwm.write(0) print"Light sensor bar:"while True: ambientLight = light.value() sys.stdout.write("Light sensor: %02d"%ambientLight) sys.stdout.write("[") # Control the intensity of the LED connected to PWM depending on the # intensity of the ambient light, if intensity is more, the LED will light less brightly tempLight = ambientLight if tempLight > MAX_LIGHT: tempLight = MAX_LIGHT # Nromalize the value pwmValue = (MAX_LIGHT - tempLight)/float(MAX_LIGHT) pwm.write(pwmValue) for i in range(0, MAX_LIGHT): if ambientLight > i: sys.stdout.write("=") elif ambientLight == i: sys.stdout.write("|") else: sys.stdout.write("") #sys.stdout.write("] pwm:%f\r"%pwmValue) # un comment this line if you want to see PWM value sys.stdout.write("] \r") sys.stdout.flush() time.sleep(0.1)if __name__ =="__main__": main()
In the script upm library is imported to interface the Light sensor and mraa is imported to control the LED. An instance of Light sensor object is created using:
light = pyupm_grove.GroveLight(LIGHT_SENSOR_PIN)
Also an instance of PWM is created to control the LED. Within an infinite loop, the Light sensor data is read into variable “ambientLight” using:
ambientLight = light.value()
The value thus read is used to draw a bar on the console and also to control the intensity of the LED. The value we get from the light sensor is used to calculate the duty cycle for the PWM that is used to control the LED.
The stdout.write() is used since we do not want the default behavior of print which automatically emits a new line after printing a string. We want to redraw the bar at the same location and not on a new line. Hence using the '\r' escapade character to position the cursor to the beginning of the line and write again from there. This way, we achieve the bar graph like behavior.
On your Edison/Galileo console type the following to download the script:
Run the script using following command:
You should see a vertical bar on the console whose length is controlled by the intensity of the ambient light and the LED connected at port D5 should light more brightly if the intensity of the ambient light is less and vice versa. You can press ctrl-c to exit the script anytime.