Thursday, 20 July 2017

Optimizing light sensor

This page leads on from the first development of the light sensor.

I have now stripped the system down to just the Arduino, the Grove Base Shield, the light sensor, the LCD and a battery to make the unit portable. However the sensitivity of the light sensor is not at all in the range that I need it. I detects from indoor light down to darkness well, but anything above indoor light intensity is off the scale. I need it to distinguish up from room light to the brightest outside summer light.

This is the code I am using:

#include <Wire.h>
#include <rgb_lcd.h>
rgb_lcd lcd;

//Set lcd backlight to off.
const int colorR = 0;
const int colorG = 0;
const int colorB = 0;

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
// Print a message to the LCD.
lcd.print("Light intensity");

void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
// For LCD: set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:

To improve the range of light sensitivity I have ordered a better light sensor called a DFRobot Light Sensor-BH1750.

Hopefully that will get me more useful data.


The new BH1750 light intensity sensor has arrived:

Image from

Here is the wiring diagram from

This is a schematic of the wiring from

I tried the code from several different webpages but they did not work. Finally I found that the code from this page works:

This is the working code:

#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup(){

void loop() {
uint16_t lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.println(" lx");

The important thing seems to be the library that comes with it, which is called BH1750-master. The original version of this library called BH1750 doesn't work for me.

The sensor works and seems to have enough range to measure from darkness up to bright sunlight. Bonus. Now I just need to get data on which numbers represent which brightnesses and document it meaningfully. I also need to add the screen in and the battery so it becomes a hand-held meter.

This is continued at Working light sensor.