Tuesday, June 09, 2015

JAVA Photoresistor sensor with Raspberry Pi

Hello there, one more little hack.

Keyes GL55 Photoresistor
This is a quick post on how to read values from a photo resistor sensor using a Raspberry Pi and Pi4j. This exercise is based on the lesson 21 of the the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". The kit's documentation can be found here. The original C code of the lesson works fine but I am not sure what the value represents. So I did my research to get real values which makes more sense. The sensor itself is a GL55 and the small PCB is configured as a voltage divider. The photo resistor itself is a 10K Ohms resistor and in the PCB there is another 10K resistor. In Roboticlab's sited there is a nice and very complete article which explains all the details of what is and how does a photo resistor work.

Wiring diagram for the ADC0832
Based on this article I was able to implement the code to calculate the different values which we can interpret from the sensor. We start by reading the voltage drop across the voltage divider with the help of an Analog to Digital Convertor IC. In this case I am using the ADC0832 which comes with the kit. It is only and 8-bit precision IC but it works well for learning and hacking purposes. From the voltage value we can calculate the resistance of the sensor and from the resistance value we can then calculate the luminance level in LUX units. Again, full details for all this calculations are very well explained in the Roboticlab's photoresistor article. And the code for this exercise can be found at Ex19_PhotoResistor.java.

Luminance Intensity ref. values
The only IMPORTANT thing that I had to sort out is the wiring. For the ADC0832 its easy, just follow the diagram above and connect the sensor's signal wire to the channel 0 (CH0). However the keyes sensor has bug. I had to swap the GND and VCC and connect VCC to +5V for the calculations to make sense and get luminance values that make sense on the reference table from the Roboticlab's article. 

With all this I got fairly good reading which one day will be wisely used for a nice little robotics project.


For full details about how to set up your Raspberry Pi and how to run these exercises please check out my old post Raspberri Pi with Pi4j and Junit testing mockups. Or leave me a comment if you have any issue or question.

Many thanks for reading, 
I hope it helps,
Keep hungry, keep foolish and keep on hacking ;-)

Posted by Marc Andreu.

No comments: