Introduction: LDR Robot
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!
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)
Step 1: Wiring the Motors
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 2: Attaching Gearboxes
Step 3: Attaching Rear Wheel/Slider
Step 4: 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 5: Place the Arduino
Step 6: Placing the Breadboard
Step 7: Attach the Battery Pack
Step 8: Wiring a Switch
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 9: 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 10: LDR Wiring
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 11: 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 12: Adding IR LED and IR Sensor
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 13: 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 14: Adding a Servo
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 15:
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
24 Comments
5 years ago
Hi,
How would you create a robot that works in the same way –
moves towards light and stops when it comes across objects – but that is
upright with a base that doesn’t move rather extends an arm towards the light
source with the LDR at the end of this arm?
Perhaps there is another instructables tutorial that I could
combine with the LDR and IR LED/sensor part of this one, if you do could you
tell me how you would combine the two?
Even if you find you can't find a solution, a point in the right direction would really help.
Thanks,
Ruby
6 years ago
Hello Sir,
I'm Aman Kushwaha, and i was trying to make a good project with Arduino.. So i decided to make this awesome LDR Robot with your awesome instructables.. And i sucessfully made it!
But i need one help regading to this,
As i am a absolute beginner with arduino, i need help in sketch, i need to add a 16x2 LCD on LDR Robot, which shows values (and robot direction) by LDR and IR sensors same as what is seen on serial monitor (by clicking Ctrl+shift+M).
I need help(sketch and how to add lcd) as soon as possible.
Any reply from you will help me a lot in my project. Thanks.
Reply 6 years ago
Hi Aman Kushwaha623,
If you have your LCD hooked up then you need to add the library for it in the Arduino IDE. The ones we use we use this code and library at the beginning of the code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
In void setup we add:
lcd.init();
lcd.backlight();
Then every time it has a Serial.println change it to lcd.print
I hope this helps!
8 years ago on Introduction
hey... how do u run the motors . what is the code for running it ? pls answer asap kindly..plsssss
Reply 8 years ago on Introduction
Hi Tasfia,
The motor are connected to transistors which are switched by pins 8 and 9 on the Arduino. This can be seen in Step:10
After you have declared pins 8 and 9 as OUTPUT in void setup()
eg. pinMode(8, OUTPUT);
You can turn the motor on in void loop by digitalWrite(9, HIGH);
You can turn the motor off by digitalWrite(9, LOW);
If you are still having trouble after this I would suggest going through:
https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/
Goodluck!
EG Robotics
9 years ago on Introduction
Hello! Iam tryed to make the same, but is not work. How i can contact with you?
Reply 9 years ago on Introduction
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
10 years ago on Introduction
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!!
Reply 10 years ago on Introduction
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
10 years ago on Introduction
which diode have u used here
Reply 10 years ago on Introduction
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
10 years ago on Introduction
for step 10, where do i put the 2 diodes?
Reply 10 years ago on Introduction
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
10 years ago on Introduction
it is all fine but which transistor have you used here?
Reply 10 years ago on Introduction
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!
10 years ago on Introduction
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?
Reply 10 years ago on Introduction
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
10 years ago on Introduction
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.
Reply 10 years ago on Introduction
Thats great! Have fun with it!
10 years ago on Introduction
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?