Anth's Computer Cave

Build a Cheap and Easy Light Sensor

Today we are making a super cheap, super easy light sensor to tell AAIMI whether or not to turn the lights on.


The sensor

The light sensor for the AAIMI Project

This one cost less than 25 cents to build, so I will eventually have many of these scattered around the project area.

Here are the components for the build.

The light sensor for the AAIMI Project

You'll need a PCB board, a LDR (Light Dependent Resistor), a 4.6K resistor and a pin header.


The PCB board for the AAIMI Project light sensor. The PCB board for the AAIMI Project light sensor.

These PCB boards I bought from this Ebay seller at the fantastic price of $1.40 for 10! That's 14 cents each and you could cut each one into four for these little circuits. I am using the full board because I will be adding a temperature sensor to it later.


The Light Dependent Resistors for the AAIMI Project light sensor.

The LDRs cost five cents each in a pack of 20 from this seller.


How it works

LDRs are a type of resistor that change their resistance depending on the light level. The LDR is combined with a standard resistor in a voltage divider. The circuit takes 5V power from the Arduino and feeds the lower power from the centre of the voltage divider back into the Analogue to Digital pin. The Arduino then calculates the light level according to the voltage it receives.

The circuit drawing for the AAIMI Project light sensor.
Image created with Estimcad CircuitDraw.

Now to build it

The first thing I noticed about the LDRs was how responsive they are. Trying to pin down some basic resistance levels was difficult without an auto-ranging multimeter. In good light (roughly the level at which I would like my lights to turn off) the LDR seemed to be around 800Ohms, but even a shadow sends the reading spiralling up so quickly that it is hard to follow.

This made it difficult to calculate the ideal resistance for the companion resistor, R1. Instead I just grabbed a breadboard and tried lots of different resistors.

I wanted the voltage going into the A0 pin to read about 3V at the point I would like my lights to turn on.

After trying a few different resistor values I settled on a 4.8K value for R1. This gives me some headroom. The sensor is reading close to 0V in direct sunlight and 5V just short of full darkness.

I then grabbed my soldering iron, put the circuit together and plugged it into the Arduino.

The code

The following Arduino code reads the sensor every three seconds and prints the voltage to the serial monitor.

int sensorPin = A0;  // This is the analogue to digital pin we will use
float levelOne = 0.0; // This creates a floating-point variable to store the reading

void setup() {

  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println("Serial Connected");
}
// This function reads the A0 pin voltage and prints it to the serial monitor
  void readOne() { 
    int sensorValueOne = analogRead(sensorPin);
    levelOne = sensorValueOne * (5.0 / 1023.0);
    Serial.println(levelOne);
  }
  
void loop() {
  readOne();
  delay(3000);
  }

That is all I need the Arduino to do for this part of the project. This raw data will be sent to the Raspberry Pi where AAIMI will crunch the numbers in Python and set a variable to either "Lights On", or "Lights Off". AAIMI will then use this variable in conjunction with my movement sensors and decide when lights should be turned on or off.

I will post the Python code for the Raspberry Pi later in the series.

Cheers

Anth


Previous:

Next: Movement Sensor



_____________________________________________


Comments

Leave a comment on this article