Introduction: LDRs + Arduino = a Simple Robot That Can Sense the Light!

An LDR is a Light Dependent Resistor and has many uses, for example, a photocel that can turn the lights of your house on when its getting dark outside.
An interesting way to understand how it works is reading its values with an Arduino.
So, let's see what we will need:


- an Arduino Uno;
- two LDRs;
- a 9v battery;
- a protoboard;
- and some wires.

Step 1: LDRs + Arduino = a Simple Robot That Can Sense the Light!

Here is how an LDR looks like.

Now, to attach the LDR sensors in the Arduino board, you can use the circuit diagram above.

Step 2: LDRs + Arduino = a Simple Robot That Can Sense the Light!

To make it works, I used an H-Bridge (in this case an SN754410 Quad Haf H-bridge), a protoboard and wheels.
You can easily find it all at the sparkfun store ;)

Step 3: LDRs + Arduino = a Simple Robot That Can Sense the Light!

Now the Arduino part!
Below is an example of the code to be uploaded into the Arduino:

void loop()
  //read the actual LDRs values and substract with the first sensor read, it must be done to determine an initial state that will be
  //used as a reference point for all behaviors.
  _leftSensorReading = analogRead(SENSOR_LEFT) - _leftSensorDif;
  _rightSensorReading = analogRead(SENSOR_RIGHT) - _rightSensorDif;


  //if the values are higher than the servos velocity, then call calibrateSensors() method.
  if(_leftSensorReading > MAX_VEL*3 || _leftSensorReading < MAX_VEL*-3) calibrateSensors();
  if(_rightSensorReading > MAX_VEL*3 || _rightSensorReading < MAX_VEL*-3) calibrateSensors();  

  int leftForward = (_rightSensorReading > 0 ? ((_rightSensorReading > MAX_VEL) ? MAX_VEL : _rightSensorReading) : 0);
  int leftBackward = (_rightSensorReading > 0 ? (((_rightSensorReading*-1) > MAX_VEL) ? MAX_VEL : (_rightSensorReading*-1)) : 0);
  int rightForward = (_leftSensorReading > 0 ? ((_leftSensorReading > MAX_VEL) ? MAX_VEL : _leftSensorReading) : 0);
  int rightBackward = (_leftSensorReading > 0 ? (((_leftSensorReading*-1) > MAX_VEL) ? MAX_VEL : (_leftSensorReading*-1)) : 0);

  go(LEFT_FORW, leftForward);
  go(LEFT_BACK, leftBackward);
  go(RIGHT_FORW, rightForward);
  go(RIGHT_BACK, rightBackward);

And below you can see it working.
Please, feel free to post your opinion.



sacnorth (author)2014-05-17

Usage of LDR with your Arduino board is very well explained in simple words here "" i personally recommend this website

About This Instructable




Bio: web developer programmer and electronic hobbyist
More by alexongarato:LDRs + Arduino = A simple robot that can sense the light!
Add instructable to: