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