Thursday, September 3, 2015

Python on Intel Galileo/Edison - Part6: Light sensor

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:

[caption id="attachment_382" align="aligncenter" width="319"]The Grove light sensor (photo: seeed studio) The Grove light sensor (image: seeed studio)[/caption]

"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.

[caption id="attachment_386" align="aligncenter" width="417"]Schematic for connecting the Light sensor Schematic for connecting the Light sensor[/caption]

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:

https://gist.github.com/navin-bhaskar/0983c8ae09f7df24c4ff

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

3 comments: