Instructables
Picture of LDR Robot
logoclear.png
LDR Robot with IR and a Servo

The LDR/IR Robot is designed to seek out light and avoid obstacles. The robot uses the Light Dependent Resistor to seek out light and the Infrared Sensor to detect and avoid objects in its path. The following are instructions with pictures to show exactly how we built our robot. Your robot can be designed the way you desire and in your own order. At the end of this manual you will find some pictures of various robots built with this kit. Use your imagination and make your robot unique. Have fun!

Here is a video of the robot in action!

Here are the parts that our kit contains. Our kit can be purchased here:
http://egrobotics.com/store/index.php?route=product/product&path=59&product_id=50

You will find that not all parts have to be used.

Components:
1 x Arduino  
1 x Battery Holder
1 x Acrylic Chassis  
1 x  On/Off Switch
2 x Motor Gearbox and Wheel  
1 x Power Jack
3 x LDR  
1 x IR Receiver
12 x LED  
2 x IR LED
14 x Resistor 
1 x Servo
4 x NPN Transistor  
1 x Ping Pong Ball
4 x Diode  
2 x Screw
1 x Bread Board  
2 x Velcro
1 x Straw  
1 x Wire Pack

The tools used in this project are:
Hot Glue Gun
Scissors
Small Screwdriver
Needle Nose Pliers
Wire Stripper
Sharpie
Electrical Tape
Soldering Iron(can be done without)
 
Remove these adsRemove these ads by Signing Up

Step 1: Components and Tools

Here are the parts that our kit contains. Our kit can be purchased here:
http://egrobotics.com/store/index.php?route=product/product&path=59&product_id=50

You will find that not all parts have to be used.

Components:
1 x Arduino 
1 x Battery Holder
1 x Acrylic Chassis 
1 x  On/Off Switch
2 x Motor Gearbox and Wheel 
1 x Power Jack
3 x LDR 
1 x IR Receiver
12 x LED 
2 x IR LED
14 x Resistor
1 x Servo
4 x Transistor 
1 x Ping Pong Ball
4 x Diode 
2 x Screw
1 x Bread Board 
2 x Velcro
1 x Straw 
1 x Wire Pack

The tools used in this project are:
Hot Glue Gun
Scissors
Small Screwdriver
Needle Nose Pliers
Wire Stripper
Sharpie
Electrical Tape
Soldering Iron(can be done without)

Step 2: Wiring The Motors

Picture of Wiring The Motors
MotorLoop.JPG
The chassis of the robot is not predetermined, make it how you want, the only rule is to make it awesome!
Find the motor, which is already placed within the yellow gearbox, but can be taken out if you so choose for other projects. Attach a pair of the twisted wires to the motor’s metal tabs by first stripping the ends of the wires and then inserting them through the holes in the tabs. Normally this would be done using a soldering gun however it works well to insert the wire through the hole and firmly clamp the wire around it using needle nose pliers. Be careful not to twist the tab on the motor because it might break. Once it is connected firmly, place a drop of hot glue over it to keep it in place.



Step 3: Attaching Gearboxes

Picture of Attaching Gearboxes
GearboxFinish.JPG
Attach the motor-gearbox to the chassis. This can be done using a dab of hot glue on the chassis and then pressing the motor-gearbox into it. Be sure to use just a little bit of hot glue otherwise it might be hard to get off if it needs to be repositioned. Align the two wheels so that it will naturally drive in a straight line. It is alright if it veers to one side a little because it is sensor driven and will auto self-correct.

Step 4: Attaching Rear Wheel/Slider

Picture of Attaching Rear Wheel/Slider
BallFinish.JPG
Attach the Ping Pong ball to the chassis in such a way that it keeps it balanced and upright. Depending on the design you chose for the chassis, ping pong ball placement will be more or less crucial. Some designs have needed to extend the ball out farther using a popsicle stick for more of a drag racer look in order to keep from tipping over or flipping. Smash one side of the ball in for greater surface area contact and hot glue the smashed side to the chassis. Remember just a little bit of hot glue goes a long ways!

Step 5: Plan The Layout

Picture of Plan The Layout
Plan out the location of the breadboard, Arduino and battery pack. When choosing the location of the three components there are three things to keep in mind.

1. The distant the wires will have to travel in order to connect the Arduino to the breadboard which will be connected to the motors and sensors.
2. The batteries will need to be taken out at some point so make sure your design allows for this.
3. Access to the USB Port on the Arduino is important as the USB cable will need room to connect as well as the power jack into the Arduino.

Step 6: Place The Arduino

Picture of Place The Arduino
AScrew.JPG
ArduinoAttach.JPG
Put the Arduino on the chassis in its planned location. Using the sharpie mark the locations of the Arduino's tie down holes. Take the Arduino temporarily off and hot glue the provided screws heads down onto the sharpie marks. Once the glue has dried, place the Arduino onto the screws to test how well it fits. If it doesn't fit well enough, pry it off and try again until you have a snug fit.

Step 7: Placing The Breadboard

Picture of Placing The Breadboard
BBVelcro2.JPG
BBFinal.JPG
Put the breadboard down in the location you have chosen and mark it. You can do this next step one of two ways. The first way is to simply pull off the paper from the back of the breadboard and reveal the sticky side and place it down where you want it. The second way is to hot glue the two small Velcro pieces to the chassis and the other two to the back of the breadboard back removing a small portion of the paper from the back side. This way it is removable.

Step 8: Attach The Battery Pack

Picture of Attach The Battery Pack
BatteryVelcro.JPG
BatteryFinal.JPG
Attach the battery pack by hot gluing one side of the Velcro to the edge of the battery pack and the other side onto the chassis where you want it. Use plenty of hot glue to cover the entire surface to ensure that it sticks firmly. (Remember it needs to be easily accessible.)



Step 9: Wiring a Switch

Picture of Wiring a Switch
Switch2.JPG
Switch5.JPG
A) Wire the battery negative or positive lead to the On/Off switch. Attach a loose wire onto the other pin on the switch. When this is done and both wires are firmly attached to the switch, hot glue the bottom of the switch to keep the wires secured. A better way would be to solder them to the pins if that is an option for you.
B) Now take the other lead either positive or negative from the battery depending on which one you choose and wire it directly to the power jack. Unscrew the power jack's screws, fold the bare wire back onto its own wire covering, then insert the wires making sure black is connected to the negative label and red is connected to the positive label. This should create a circuit with the on/off switch enabling or disabling power.
C) Next mount the switch to the chassis somewhere accessible and easy for you to switch on or off. Use hot glue to place the switch in the desired position.
D) Test to make sure good connections were made by flipping the switch on and confirming the Arduino's power light stays constant.




Step 10: Motor Wiring

Picture of Motor Wiring
We will now create our first circuit using the Arduino board. Find the two transistors and two diodes in the clear bag without a color and place them on the breadboard. Use the following diagram to build the circuit using the jumper wires to connect from the Arduino to the breadboard. (Putting a diode across the motors is a good idea.)

TIP: If the motors seem to be going the wrong direction switch the polarity of the motor by making the positive side the negative and the negative the positive.

The motor on the left side of the robot should be connected to digital pin 8 and the motor on the right side connected to digital pin 9.






Step 11: LDR Wiring

Picture of LDR Wiring
Diagram3.jpg
Attach the long twisted wires to the Light Dependent Resistor which can be found in the green bag. Twist the end of the LDR and the end of the wire around each other. Use the needle nose pliers to get a good solid twist. If you want you may put a dab of glue to hold them together.

Wire the Light Detecting Circuit as shown below.

The LDR connected to analog pin 2 should be the right sensor and the LDR connected to analog pin 0 should be the left sensor. (If you switch these your robot will be a light fearing robot.)

Be sure the LDR's are pointed diagonally. One to the left and one to the right facing toward the level the lights are coming from.

Test the circuit using this code:
(http://www.egrobotics.com/instructions/code/testldr.ino)

void setup() {
Serial.begin(9600);
}
void loop() {
int sensor1Value = analogRead(A0);
Serial.print("Analog Pin 0 Reading = ");
Serial.println(sensor1Value);
delay(1000);
int sensor2Value = analogRead(A2);
Serial.print("Analog Pin 2 Reading = ");
Serial.println(sensor2Value);
delay(1000);
}

Open the Serial Monitor by clicking the Magnifying Glass in the top right corner of the Arduino software interface or by hitting Ctrl+Shift+M. This should bring up the serial monitor and start displaying values. Cover one sensor and you should see one of the values drop. Cover the other sensor and its value should also drop. If it doesn't, be sure you used the correct resistor, the connections are good, and you are not in a dark room.




Step 12: Test The LDR Robot

Copy the following code into the arduino sketch on your computer and upload the code by clicking .
(http://www.egrobotics.com/instructions/code/robotldr.ino)

const int RightSensor = 2;
const int LeftSensor = 0;

int SensorLeft;
int SensorRight;
int SensorDifference;


void setup() {

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(LeftSensor, INPUT);
pinMode(RightSensor, INPUT);
Serial.begin(9600);
Serial.println(" \nBeginning Light Seeking Behavior");
}


void loop() {
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
SensorRight = 1023 - analogRead(RightSensor);
delay(1);
SensorDifference = abs(SensorLeft - SensorRight);

Serial.print("Left Sensor = ");
Serial.print(SensorLeft);
Serial.print("\t");
Serial.print("Right Sensor = ");
Serial.print(SensorRight);
Serial.print("\t");

if (SensorLeft > SensorRight && SensorDifference > 75) {
Serial.println("Left");
digitalWrite(8, HIGH); 
delay(250);
digitalWrite(8, LOW);
delay(100);

}

if (SensorLeft < SensorRight && SensorDifference > 75) {
Serial.println("Right");
digitalWrite(9, HIGH);
delay(250);
digitalWrite(9, LOW);
delay(100);
}

else if (SensorDifference < 75) {
Serial.println("Forward");
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(250);

}
Serial.print("\n");
}



You have now completed the LDR part of the robot. It should now be able to seek out the brightest light source in a room. Turn it on and test it out. Also try turning off the lights in a room and leaving a door open with light coming through and see if it will travel through it. Another fun thing to do is take a flash light and try to direct its course with it!

Step 13: Adding IR LED and IR Sensor

Picture of Adding IR LED and IR Sensor
IRTape.jpg
IRFrontAttach.jpg
IRDiagram5.jpg
Wire the IR Sensor and IR LED (found in the clear bag) with the twisted wires using the same method used for the LDRs. Strip both ends of all 5 wires (2 for the IR LED and 3 for the IR Receiver), twist the wires around the connections and use a dab of hot glue to make the connections hold together. Next place the plastic straw or cover over the IR LED to make it unidirectional. You may need to wrap the IR LED in electrical or black duct tape to keep the IR light from seeping out in all directions. Be sure to wrap the back of the LED as well.

Attach the IR LED and IR Sensor(Holding off till after wiring can make it easier) to the front of your robot or wherever you want it to detect an object. It helps to have them both fixed to the chassis. If the sensors are left loose they will bounce around and make it difficult to get an accurate reading. Be sure the IR Sensor is behind the front of the IR Straw.


Wire the IR Sensor and IR LED as shown below. The resistor is a 470 Ohm Resistor


Step 14: Testing IR

Use the following code to determine if the IR detection is set up properly. Upload the code, then put your hand in front of the IR LED and the Arduino's LED should turn on. When you move your hand away the LED should turn off. (In the picture above the LED is labeled with an “L”)

Code:
(http://www.egrobotics.com/instructions/code/testir.ino)

#define IRsensorPin 11
#define IRledPin 10
#define D13ledPin 13

void IR38Write() {
  for(int i = 0; i <= 384; i++) {
    digitalWrite(IRledPin, HIGH);
    delayMicroseconds(13);
    digitalWrite(IRledPin, LOW);
    delayMicroseconds(13);
  }
}
void setup(){
  pinMode(IRledPin, OUTPUT);
  digitalWrite(IRledPin, LOW);
  pinMode(D13ledPin, OUTPUT);
  digitalWrite(D13ledPin, LOW);
}

void loop(){
  IR38Write();
  if (digitalRead(IRsensorPin)==LOW){
    digitalWrite(D13ledPin, HIGH);
  } else {
    digitalWrite(D13ledPin, LOW);
  }
  delay(100);
}


Troubleshooting:
If the LED is always on or comes on and off often then add more tape around the straw or put a divider wall between the IR LED and IR Sensor.

If the LED does not come on be sure everything is connected and plugged into the correct pins. Be sure the angles are correct. The light needs to be able to bounce off the object and return into the black part of the IR Sensor.

Part 2
If your IR LED and Receiver are working properly the next thing to do is try out the following code that will activate both the LDR and IR to have the robot seek out light, but at the same time attempt to avoid obstacles that are in the way.

Code:
(http://www.egrobotics.com/instructions/code/robotavoid.ino)

#include <Servo.h>
Servo myservo;

#define IRsensorPin 11
#define IRledPin 10

const int RightSensor = 2;
const int LeftSensor = 0;

int IR;
int SensorLeft;
int SensorRight;
int SensorDifference;

void IR38Write() {
  for(int i = 0; i <= 384; i++) {
    digitalWrite(IRledPin, HIGH);
    delayMicroseconds(13);
    digitalWrite(IRledPin, LOW);
    delayMicroseconds(13);
  }
}
void setup() {
myservo.attach(5);
pinMode(IRledPin, OUTPUT);
digitalWrite(IRledPin, LOW);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(LeftSensor, INPUT);
pinMode(RightSensor, INPUT);
Serial.begin(9600);
Serial.println(" \nBeginning Light Seeking Behavior");
}


void loop() {
IR38Write();
IR = digitalRead(IRsensorPin);
delay(50);
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
SensorRight = 1023 - analogRead(RightSensor); 
delay(1);
SensorDifference = abs(SensorLeft - SensorRight);

Serial.print("Left Sensor = ");
Serial.print(SensorLeft);
Serial.print("\t");
Serial.print("Right Sensor = ");
Serial.print(SensorRight);
Serial.print("\t");


if (SensorLeft > SensorRight && SensorDifference > 75 && IR == HIGH) {
Serial.println("Left");
digitalWrite(8, HIGH); 
delay(250);
  digitalWrite(8, LOW);
  delay(100);

}

if (IR == LOW){
digitalWrite(8, HIGH); 
delay(1500);
  digitalWrite(8, LOW);
  digitalWrite(8, LOW);
    delay(150);
 
}

if (SensorLeft < SensorRight && SensorDifference > 75 && IR == HIGH) {
digitalWrite(9, HIGH);
delay(250);
digitalWrite(9, LOW);
  delay(100);
}

else if (SensorDifference < 75 && IR == HIGH) {
Serial.println("Forward");
digitalWrite(8, HIGH);  
digitalWrite(9, HIGH);
delay(500);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
    delay(250);
}
Serial.print("\n");
}

Step 15: Adding A Servo

Picture of Adding A Servo
DSC01553.jpg
ServoDiagram.jpg
This section will depend on what you want to do with the servo. You can use it as an attack arm or use it to lift something or to help move your vehicle out of the way when it is stuck. It is up to you on how it is placed and how it is used. Find a long stiff object to attach to a servo horn or use the Popsicle sticks provided. Use a hot glue gun to attach it to the servo horn. Attach the servo horn to the servo using a small screw. You can attach the servo using hot glue to the chassis of the robot. (Be sure the servo is attached and supported well as the servo may push itself loose.)



Part 2
Wire the servo into the Arduino and Breadboard as shown below. It is easy to use the jumper wires and stick them into the end of the servo connector and then into the breadboard or arduino.

Part 3
To test if your servo is properly wired and installed check it by uploading the example code for Servo Sweep. This is easily done by clicking File > Examples > Servo > Sweep. When that opens change myservo.attach(9); this code line to be myservo.attach(5); to match your pin you have attached it to.

Once you have uploaded this code it should have the servo sweep back and forth if you have it properly configured.

Step 16:

Upload this code to have your robot seek light and attack objects in its path with the servo.

Code:
(http://www.egrobotics.com/instructions/code/robotattack.ino)

#include <Servo.h>
Servo myservo;

#define IRsensorPin 11
#define IRledPin 10

const int RightSensor = 2;
const int LeftSensor = 0;

int IR;
int SensorLeft;
int SensorRight;
int SensorDifference;

void IR38Write() {
  for(int i = 0; i <= 384; i++) {
    digitalWrite(IRledPin, HIGH);
    delayMicroseconds(13);
    digitalWrite(IRledPin, LOW);
    delayMicroseconds(13);
  }
}
void setup() {
myservo.attach(5);
pinMode(IRledPin, OUTPUT);
digitalWrite(IRledPin, LOW);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(LeftSensor, INPUT);
pinMode(RightSensor, INPUT);
Serial.begin(9600);
Serial.println(" \nBeginning Light Seeking Behavior");
}


void loop() {
IR38Write();
IR = digitalRead(IRsensorPin);
delay(50);
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
SensorRight = 1023 - analogRead(RightSensor); 
delay(1);
SensorDifference = abs(SensorLeft - SensorRight);

Serial.print("Left Sensor = ");
Serial.print(SensorLeft);
Serial.print("\t");
Serial.print("Right Sensor = ");
Serial.print(SensorRight);
Serial.print("\t");



if (SensorLeft > SensorRight && SensorDifference > 75 && IR == HIGH) {
Serial.println("Left");
digitalWrite(8, HIGH); 
delay(250);
  digitalWrite(8, LOW);
  delay(100);

}

if (IR == LOW){
  delay(500);
digitalWrite(8, HIGH); 
digitalWrite(9, HIGH);
  myservo.write(30);
  delay(200);
  myservo.write(130);
  delay(200);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
    delay(250);
}

if (SensorLeft < SensorRight && SensorDifference > 75 && IR == HIGH) {
digitalWrite(9, HIGH); 
delay(250);
digitalWrite(9, LOW);
  delay(100);
}

else if (SensorDifference < 75 && IR == HIGH) {
Serial.println("Forward");
digitalWrite(8, HIGH);  
digitalWrite(9, HIGH);
delay(500);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
    delay(250);
}
Serial.print("\n");
}



Now that you have completed the wiring and coding for your robot it is time to make the robot look unique! Use the provided materials and materials of your own to make the robot look or act the way you want it. Take a look at the Optional section that will help you with wiring extra LEDs and other additional modifications. Thanks for building this robot! We would love to see a picture and hear a story! Please email us with a picture or story at Robots@egrobotics.com
Nursultan4 months ago

Hello! Iam tryed to make the same, but is not work. How i can contact with you?

EGRobotics (author)  Nursultan4 months ago

Hi Nursultan,

You can email me with whatever problems you are having and I can take a look and get back to you! My email is ZyanM@interest-ed.org Looking forward to hearing from you!

EGRobotics

Sir,,This look pretty good!!
My question being,, is the transitor work just as an amplifier to amplify the input signal??
and if i am not wrong then here we are not controlling the speed of motor, right??
can we control its speed using pwm pins(analog o/p)??
and How can we increase the sensitivity of LDR?

i would be delighted if 'm acknowledged by your suggestion, SIR!!
EGRobotics (author)  garry_magnanimous1 year ago
Hi Garry,

The transistor's works to amplify the Arduino's output to the motors. The Arduino's digital outputs can only do a around 50 mA or something. The transistors use that tiny amount to open a gate to the Arduino's 5V which can do around 500 mA.

Yes you are correct, in that we can use analogWrite to control the speed of the motor if you wish to change it.

You can change the sensitivity of the LDR by either narrowing the the SensorDifference value or changing the 10K resistor for another value.

Goodluck! If you make one we'd love to see it!

www.EGRobotics.Com
ikochar1 year ago
which diode have u used here
EGRobotics (author)  ikochar1 year ago
Hi ikochar,

I missed your reply, sorry. In case you are still wondering we just used what we had around I believe they are 1N4007. Hope you figured something out by now though.

EG Robotics
topek_251 year ago
for step 10, where do i put the 2 diodes?
EGRobotics (author)  topek_251 year ago
Hi Topek_25,

Sorry for the late reply. Here is a quickly made picture of where the diodes go. The diodes go between the motor leads. This keeps any voltage created by the motor spinning from momentum or any other force from being a nuisance. Good Luck!

EG Robotics
diodes.jpg
ikochar1 year ago
it is all fine but which transistor have you used here?
EGRobotics (author)  ikochar1 year ago
We use 2N2222 transistors. Their only rated at around 600 - 800mA so be sure to use them through the Arduino so that it bogs down the voltage regulator instead of destroying the transistors. Have Fun!
curious061 year ago
hey i connected everything the way you described and the left wheel rotates while the right wheel doesn't..they have exactly the same kind of set up and connected to the right pins..but if i switched the left motor pin in to the right transistor the left one work, any suggestion?
EGRobotics (author)  curious061 year ago
Hi Curious

Do you have the IR and IR Sensor attached? Or just the light seeking part? If you have the IR sensor attached what code are you using and did it work before attaching the IR Sensor?

The Right transistor works because you tested plugging the left motor into it. Is that what you are saying? Did you plug the Right motor directly into the power source to ensure connections between wires and motor? If so did the motor work? if it didn't check connections at the motor especially if you used the hot glue method. Double check your diode set up on that motor as well.

Let me know if you have any more questions! also post the solution to help others in the future! Also did you buy the kit from us? if not are you using the exact same motors? You can always email us from our sites email for additional help as well!

Thanks,
EG Robotics
This is great! It's like you read my mind are are a few weeks ahead of me. I have planned this same project almost exactly and just received all my parts. Even the ping ping ball caster, LDR, and IR. Your instructions have cleared up some details I was working out. I was building my son a robot for Christmas. Thank you so much.
EGRobotics (author)  matt.e.jenkins1 year ago
Thats great! Have fun with it!
great instructable! I like how you put code in each step of the process. I like your simple design, do you think it's possible to do this with out a microcontroller? have the output from the resistors drive the motors directly?
EGRobotics (author)  amandaghassaei1 year ago
To have the output from the Light Dependent Resistors Drive the motor directly would not be possible because of the high resistance of the LDR.

My battery Box is 6 rechargeable AAA's so thats 1.2V * 6 = 7.2 Volts.
My LDR is putting out 1500 Ohms in Normal Light and the motor is 5 ohms. To find the amps add up the ohms = 1505.

Amps = Volts/Resistance Amps = 7.2/1505 = .0047 A which isn't enough to move our motors.
Also the voltage usage of each Volts = Amps * Resistance
Volts = .0047 * 5 = .023 Volts which also isn't enough volts.

However if there was such thing as a LDR that was like 5 or 6 Ohms when it was bright and 4 or 3 ohms when it was dark that would probably work. Instead of using a full blown micro-controller you can do like Higgs Boson did in this Instructable and use a 555 IC. Which you made me want to do now! Maybe I'll add how I did it to mine later! Thanks!
what if you just use a transistor to step up the current but control the voltage with the LDR?
EGRobotics (author)  amandaghassaei1 year ago
So I set up a circuit like the one below and it worked in a brighter setting. If I had some potentiometers lying around could have played with it a bit more. Lower resistance LDRs would have worked better as well. However I am interested in your opinion, how would you set it up?
Thanks!
LDRMotor.jpg
yes, this is exactly what I was thinking! And yes, the high resistance of the LDRs will make the range of the voltage divider somewhat limited. There is a way to get around this: set up another transistor as an amplifier.

Start with a circuit like this one.  Set up a pot and the LDR as R1 and R2.  (You can forget about the capacitor since we are not working with an AC signal).  Then use another pot for RC.  You will need to mess around with the pots a little to figure out what gives you a signal centered around 2.5V with a good amplitude (ideally extending from 0 and 5V) from Vout.  Eventually, you should replace the pot in series with the LDR with a resistor, and you can use the RC pot to adjust sensitivity.  Then send Vout to a second transistor set up as an emitter-follower (just like what you have in the image above).  Simply connect Vout to the base of this transistor and drive the motor from the emitter.

let me know how that works!