In this post, 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.
The operational 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.
The LDR along with resistor R1 forms the voltage divider circuit 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.
Hardware connections:
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.

Usage:
On your Galileo/Edison, checkout the latest code base from github:
git clone https://github.com/navin-bhaskar/Python-on-Galileo-Edison cd Python-on-Galileo-Edison/part6-Light_sensor
Or you can update your git workspace if you already have this repo using:
git pull origin master
Run the script using following command:
python light_sensor.py
You should see a vertical bar on the console whose length is controlled by the intensity of th 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.
The script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import pyupm_grove | |
| import mraa | |
| import time | |
| import sys | |
| """ | |
| This script demonstrates the usage of the light sensor using upm | |
| setup: | |
| The light sensor is connected to port A0 and an LED is connected to port | |
| D5. | |
| Demo: | |
| start the application in the command line by using following command: | |
| python light_sensor.py | |
| While running the demo, you should see a bar appear in the console whose | |
| length 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 connected | |
| MAX_LIGHT = 50 | |
| LED_PWM_PIN=5 | |
| def 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 crrated 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.
Other parts:
part0: Getting started on Galileo/Edison
part1: GPIO output
part2: GPIO input(button)
part3: pwm
part4: adc
part5: Temperature sensor
part6: Light sensor
8 thoughts on “Python on Intel Galileo/Edison – Part6: Light sensor”