LDR

LDR is a sensor that reads the intensity of light. It gives analog reading. When connected with Arduino analog pins, its value ranges from 0 to 1023 depending on the amount of light shining on it.

Materials

  • 1 Arduino Uno board
  • 1 Basic Education shield
  • 1 TinkerKit LDR
  • 1 TinkerKit wire

or …

  • 1 LDR sensor
  • 1 10k ohm resistor
  • 3 jump wire

Instructions

  1. Connect the LDR with TinkerKit wire
  2. Connect the other end of the wire to TinkerKit port analog A1 on the shield.

Or …

  1. To connect a normal LDR, connect the 10k ohm resistor between GND and an analog pin(A1 in this example) on the breadboard.
  2. Connect the LDR between the analog pin and 5V.

Read the Values

To test the LDR, open Example>BasicEducationShield>Help>LDRTest.

/*
LDR test
*/
#include <BasicEducationShield.h>

//Tinkerkit LDR is connected to analog 1.
LDR sensor = LDR(A1);

void setup(){
Serial.begin(9600);
}

void loop(){
//test() prints data to Serial port.
sensor.test();
delay(100);
}

Upload the sketch and open the serial monitor, you should be able to see the readings of the LDR. If you cover the LDR, reading value should get smaller. If you shine light on the LDR, the reading should get higher.

It’s not working?

  1. First of all make sure the connections between cables, sensor and board are correct.
  2. Make sure your sensor is connected to the same analog pin as you’ve stated in your code.
  3. If the LED at the back side of TinkerKit LDR does not light up, either the component is broken, or Arduino is not powered.

Note: Remember not to use A4 orA5

Switch

Because the reading of LDR is heavily influenced by the environment, you should calibrate it before using. If the LDR is used as a button, or using getState() feature, you need to get two values from LDR. The baseValue of LDR when nothing is covering/shining on it, and threshold. To get the threshold, you should first put the desired object/light on LDR, which gives you the topValue. And the threshold is one picked by you between baseValue and topValue. The closer to baseValue, the more sensitive your LDR switch is, but more likely to get false clicks as well.

Open Example>BasicEducationShield>Help>LDR.

/*
LDR
*/#include <BasicEducationShield.h>LDR sensor = LDR(A1);

void setup(){
Serial.begin(9600);
sensor.config(700,900);
}

void loop(){
Serial.println("Please press...");
//Wait until the LDR gets cover-uncovered.
Serial.println(sensor.pressed());

Serial.println("Please press...");
//Wait until the LDR gets uncover-covered.
Serial.println(sensor.pressed());

While(true){
//Continuously output whether the sensor has
//passed threshold.
Serial.println(sensor.getState());
delay(30);
}

}

In this example the LDR is used as a button. So replace the arguments in sensor.config(700,900); using the baseValue and threshold measured previously. Upload the sketch and open serial monitor. If you press/shine light on the sensor, you should see the program take it as pressedreleased, and status change.