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

About: web developer programmer and electronic hobbyist

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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.

Arduino Challenge

Participated in the
Arduino Challenge

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest



    5 years ago on Introduction

    Usage of LDR with your Arduino board is very well explained in simple words here "http://newchip.in/blog/arduino-analog-port-reading-ldr-tutorial/" i personally recommend this website