There are many ways to measure the light intensity using a Raspberry Pi and one of them (that doesn’t require an ADC) is the TSL-235. It is a photodiode connected to a small circuit that generates a pulse of which the frequency depends on the light intensity. This frequency can be directly read out using a Raspberry Pi as explained below.
First check out the datasheet of the TSL-235, so you know you are not going to blow up your pi. I used the following layout to connect the TSL235 to the Pi:
TSL235 <-> PI Pin 1 GND - Pin GND Pin 2 VDD - Pin 1 3.3V Pin 3 Out - GPIO 25
Check this small Pi layout to see which pin is where
As is writting in the datasheet, connect a 0.01-μF to 0.1-μF capacitor between the VCC and GND line close to the TSL-235.
I first tried to interface using a simple python script that just measures how long it takes before the GPIO pin falls again, inspired by this topic on Stackoverflow. However it didn’t run smoothly and the output was very noisy. Also running the script using ‘nice’ (to give it higher priority) didn’t work out. There for I switched to C, which runs obviously faster than python.
Luckily there is already a small script taking care of that on Github RaresPlescan/daisypi.
It relies on the famous Wiring Pi library that can be downloaded here and is easy to install:
git clone git://git.drogon.net/wiringPi cd wiringPi git pull origin cd wiringPi ./build
However the daisypi c version runs as root, to circumvent this put in /etc/local.rc the following to make GPIO port 25 readable as pi user
sudo -u pi /usr/local/bin/gpio export 25 in
I’ve forked the daisypi C file which makes use of wiringPiSetupSys() instead of wiringPiSetup()
Download this version here: https://github.com/EvdH0/daisypi/blob/master/sense/tsl235r/t13.c
Next compile this C file using the following command
gcc -o tsl_read t13.c -lm -lrt -L/usr/local/lib -lwiringPi -lwiringPiDev
Next the executable can be run using
./tsl_read 300000 10
This takes about 5 seconds and results in some output like this
TSL235READ--poll_time--itterations--avg_value--autoscale 300000 10 47.000 16384
9 Responses to Measuring light intensity with the TSL-235 and a Raspberry Pi
I followed this well written tutorial. … but could not get avg values over 100 and something … where as if I hooked the tsl235 up to an ardunio and used a freqcount library I could get it to top off at the spec 738w/m2 … Is it a limination of this code .. I’m also using a B + PI.
I haven’t tried that myself since I was interested in reading out low levels (hence low frequencies from the TSL235) of light. Do you have the capacitor in place between the Vdd and ground?
If so, it is probably indeed due to the code/Pi that has difficulties sampling above >100 kHz (>100 uW/cm2), that is why I already switch from the python code to the more low level C code.
Yes I have a capacitor there … and recently just tried 2 sensors on the same breadboard next to each other … both seemed ok up to around 180 w/m2 then things went south as the one driven by the arduino increased and topped out around 790, the PIs topped out around 300 and seemed jumpy and noisy (Ie would drift down at certain points as light increased.)
This does seem to indicate it has issues sampling higher.
I was recently trying to play with this… : http://abyz.co.uk/rpi/pigpio/examples.html which had a freqcounter too but same issue.
PI limitation? If so that is ashame…
Also anyone wanting to use this sensor for low light on the PI .. this is the tutorial for you! .. That works great.
pigpio works by sampling (maximum rate 1 MHz) which means that the limit for an individual gpio is 500kHz and higher frequencies will give wrong results. You should be able to reach that limit though. As an experiment I started 50% dutycycle PWM at 500 kHz on gpio 12 and 13 and a 500 kHz clock on 4. I got results of 4=498259 12=499914 13=499914 over a second.
Did you use the pigpio second frequency example?http://abyz.co.uk/rpi/pigpio/code/freq_count_2.zip Would be interesting to check pigpio in combination with the TSL-235, this will probably also solves Rob his problem.
Yes, built with gcc -o fc freq_count_2.c -lpigpio -lrt -lpthread, invoked with sudo ./fc 4 12 13 -s1 -r10. In a separate window start PWM and clock pulses with pigs, e.g. pigs hc 4 500000 will start a 500kHz clock on gpio 4. Only a few gpios support hardware clocks and PWM. Warning, I am the author of pigpio so my views are unlikely to be impartial.
@Rob maybe you can give this a try? Looks like with 0.5 MHz you should be able to reach the 738w/m2 you observed using the Arduino.
Pingback: Frequency Counting on a Raspberry PI | Question and Answer