Measuring the light intensity (DS2438 and SFH203P)


1-Wire and RPi should allow to measure "environmental variables".  Sole temperature is by far not enough to feel a full satisfaction from the chosen solution.  I have found in the net a few examples of how the light intensity can be measured with the use of the DS2438Z (Smart Battery Monitor) and have build a simple board.  Surprisingly - it all works! This constant success in making and connecting things to 1-wire is surprising.  It shows that the idea is quite easy and even a simple guy with a soldering iron can manage it quite well.

I do not know if my browser is to blame, but any time I ask Google about 'light sensor' or 'humidity sensor' I receive only products of HobbyBoards. The store has ceased to publish the bord layouts -;all that is lefts are some posts on forums saying that at some point in the past those were available.  Slowly, looking at theoretical models, I have built the following board:

SolarSensor BoardIt should, in theory, enable connecting a SFH203P light-sensing diode, HIH humidity sensor and DS18B20 temperature sensor at once.  The resistor R1 is 390Ohm, R2 is 100K, capacitors C1 i C2 are 0,1uF.

My first board uses just the photodiode.  I needed a simple sensor to hand on the wall of my house to measure the light intensity and temperature.  The DS2438 has a built-in temperature sensor just like the DS18B20.  You get all in a quite simple package.  The spot for adding another DS18B20 makes sence only if you treat the bard as a 'base' to which different dispersed sensors are connected.

I have used the  Z43J Kradex case because... it was the smallest.  It is not, generally, a correct solution as it does not guarantee any waterproofness.  I have not, however found anything better, any case, which would be equally small and would have a glass orb so that there is no need to drill holes for the sensor head.

SolarSensor Ready

The choice of the photodiode and the humidity sensor was in my case limited to what is supported by OWFS and OWFS uses mainly... products of Hobby Boards.  Once you connect a DS2438Z to your 1-wire network, you should find it as a separate directory with a structure of directories and sub-directories reflecting the potential usages:

SolarSensor tree

OWFS does all the necessary recalculations for each supported usage.  Using the given elements you do not need to correct anything.  To see to light intensity just connect the wires and check in S3-R1-A directory in "illuminance" file.  The value there is given in LUX.

To simplify reading the sensor I have prepared a few scripts:

1. A PHP file placed in  /var/www.  It enables reading the sensor through a web browser

if ($MainDir = opendir('/mnt/1wire/26.27XXXXXXXXXX/S3-R1-A')) {
	$TempFile = fopen("/mnt/1wire/26.27XXXXXXXXXX/S3-R1-A/illuminance", "r");
	if (!$TempFile) {
		echo "

Problems with illuminance file!

"; } else { $Luminance = fgets($TempFile); echo "

Luminance = "; echo $Luminance." LUX

} } else { echo "

Sensor missing!!

"; }

2. A PYTHON script, which sends the data to SQL database


import MySQLdb;

from time import localtime, strftime
timer = strftime("%Y-%m-%d %H:%M:%S", localtime())

	fo = open("/mnt/1wire/26.27XXXXXXXXXX/S3-R1-A/illuminance", "r")
	print timer + " : Error reading sensor data"
	illuminance =
	lux = illuminance.split(".")
	lux = lux[0].strip()
		db = MySQLdb.connect("yourserver","youruser","yourpassword","yourdatabase" )
		print timer + " : Error connecting to the SQL database"
		cursor = db.cursor()
		sql = "INSERT INTO Light(Time, Lux) VALUES ('" + timer + "',"+ lux + ")"
			print timer + " : Sending data ok"
			print timer + " : Error executing query"

The file with the code presented above must be made 'executable' with a command "chmod +x".  In order to run the code, you need to enter "./"

If you do not have the needed softer/libraries, after running the script, you will see:

Traceback (most recent call last):
	File "", line 3, in 
	import MySQLdb
	ImportError: No module named MySQLdb

Run "apt-get install python-mysqldb" and it all should work well.

Additionally you can make the python script run automatically every XXX minutes.  Just add it to your CRON table by:

crontab -e

and adding a line:

*/20 * * * * /var/yourdir/ >> /var/yourdir/log/yourfile.log 2>&1

"*/20" means that the script will be run every 20 minutes.  The part marked in red is not necessary.  It enables writing script responses to a separate file.

That's all for now.  You can see the effects of how the scripts work at