This instructable will cover how to make a light seeking robot (Photovore), and some robotics and motor control basics. This robot can be built with parts found at the nearest Radio Shack, if you already have an Arduino board and a robotic base. The cost for a five pack of CdS cells (Light dependent resistors) cost $2.99 at Radio Shack. and a 15 pack of NPN transistors (very useful later on) are also $2.99. Of all these parts, we will only need two NPN transistors and two CdS cells.
Please remember, this is my first instructable.
I really appreciate criticism, comments, questions, and suggestions.
Your feedback will help me improve this instructable and future instructables created by me.
Check out our website
Note: by posting any pictures in the comments, you are giving me full rights to the posted image (disclaimer to post robot pictures on the last step.)
Step 1: Materials
2 CdS cells
2 NPN transistors
2 current limiting resistors for the transistors (I used 1k ohm (brn - blk - red) )
2 flyback diodes. (1n4001 - 1n4003). You can scavenge these out of AC to DC converters. (shown in the third picture)
1 Arduino development board (I used a duemilinove, but an Uno, diecimila, NG, etc. will work.)
1 robot base. (Go to step 2 for more info if you don't already have one)
1 Arduino supply battery pack (usually 9 volt batteries.)
1 Motor supply battery (2-4 AA batteries work well.)
Step 2: Robot Base
I used the Tamiya tracked vehicle set, as it was very common, and included many of these things. The tracked vehicle set included a rectangular wooden base, wheels and drive sprockets, tank treads, and a single gearbox (useless for this I'ble, but useful later on). Separately, I had to buy a battery pack and a dual gearbox which included two DC motors.
I made a second layer on top of the Robot Base to hold the Arduino board away from any EMI produced by the motors and possibly interfering with the code running on the Arduino. This is also the reason for having two seperate power supplies for the Arduino and motors.
*Note the H-bridge isn't used in this I'ble. You can find plenty more info on Instructables or Google.
Step 3: Incorporating Light sensors
void setup() {
pinMode("analog pin", INPUT);
digitalWrite("analog pin", HIGH);
}
This way, all you need to do is connect one pin of the LDR to the analog input pin, and the other pin to ground.
Using this code, you can read the values of the two light sensors and then display them through the serial monitor. This is the first part of the robot. Later on, we will use the values of the light sensor to control motors accordingly.
The physical setup is simple. Connect one pin of both light sensors to ground, The other two pins two analog input 1 and 2.
Analog pin 1 is for the Right sensor, Analog input 2 is for the Left sensor.
// Copy code from this point on:
/****************************************************************************************************************
* Program for reading two LDRs
* created by: Anuj
* (c)copyright
*
*
* Physical setup:
* Two LDRs connected to ground and Analog pin 1(Right) and Analog pin 2(left)
*
*
******************************************************************************************************************/
// Using Arduino Duemilinove
// Pin definitions - attaches a variable to a pin.
const int RightMotor = 2; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotor = 3; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int RightSensor = 1; // This pin is used to read the value of the Right Sensor.
const int LeftSensor = 2; // This pin is used to read the value of the Left Sensor.
// Variable definitions
int SensorLeft; // This stores the value of the Left Sensor pin to use later on in the sketch
int SensorRight; // This stores the value of the Right Sensor pin to use later on in the sketch
int SensorDifference; // This value is used to determine the difference between the Left and Right
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup() {
pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
digitalWrite(A1, HIGH); // Enables an internal pullup resistor
digitalWrite(A2, HIGH); // Enables an internal pullup resistor
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nReading Light sensors."); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop() {
SensorLeft = analogRead(LeftSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
SensorRight = analogRead(RightSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.
// This is known as debugging.
Serial.print("Left Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorLeft); // Prints the value of the Left Sensor.
Serial.print("\t"); // Prints a tab (space).
Serial.print("Right Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorRight); // Prints the value of the Right Sensor.
Serial.print("\n"); // Prints a new line after all the necessary data is displayed.
}
Step 4: Incorporating Light sensors Part 2
By personal preference, I like to have the analogRead() function return 0(min) if the light sensor is dark, and 1023(max) if it detects bright light. We can solve this problem using the software. There are no changes in the circuit.
At this section of code:
SensorLeft = analogRead(LeftSensor);
delay(1);
We can add :
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
This will subtract whatever value the analogRead() function returns by 1023(which is the max value it can return.)
This way, it will invert what ever value the analogRead() returns.
For example, if the analogRead() value is x
x = 0
Sensor value = 1023 - x
Sensor value = 1023 - 0
Sensor value = 1023
x = 1023
Sensor value = 1023 - x
Sensor value = 1023 - 1023
Sensor value = 0
This method can be used to invert any value, as long as you know the maximum value it will return.
For example, if a sensor returns a maximum of 255:
val = 255 - sensor value
Upload this code to the Arduino board and test it out.
// Copy code from this point on:
/****************************************************************************************************************
* Program for reading two LDRs
* created by: Anuj
* (c)copyright
*
*
* Physical setup:
* Two LDRs connected to ground and Analog pin 1(Right) and Analog pin 2(left)
*
*
******************************************************************************************************************/
// Using Arduino Duemilinove
// Pin definitions - attaches a variable to a pin.
const int RightMotor = 2; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotor = 3; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int RightSensor = 1; // This pin is used to read the value of the Right Sensor.
const int LeftSensor = 2; // This pin is used to read the value of the Left Sensor.
// Variable definitions
int SensorLeft; // This stores the value of the Left Sensor pin to use later on in the sketch
int SensorRight; // This stores the value of the Right Sensor pin to use later on in the sketch
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup() {
pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
digitalWrite(A1, HIGH); // Enables an internal pullup resistor
digitalWrite(A2, HIGH); // Enables an internal pullup resistor
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nReading Light sensors."); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop() {
SensorLeft = analogRead(LeftSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
SensorRight = analogRead(RightSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.
// This is known as debugging.
Serial.print("Left Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorLeft); // Prints the value of the Left Sensor.
Serial.print("\t"); // Prints a tab (space).
Serial.print("Right Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorRight); // Prints the value of the Right Sensor.
Serial.print("\n"); // Prints a new line after all the necessary data is displayed.
}
//Do not copy anything beyond this point.
Now we have to sensors to use with our robot, but we're not quite done yet. We need a way to compare these values. Using only these values, we can determine whether one sensor is reading a greater or lesser value then the other sensor, or if they are exactly the same.
Now, because of many other factors such as minor discrepancies in LDR manufacturing, jitter, EMI, etc, the values for the light sensor will seldom be exactly the same. So instead, we can use another integer to determine if they are approximately the same. We can achieve this by using calculating the difference between the two light sensors. We can subtract the value of one sensor by the value of the other sensor and then finding the absolute value of the sensor. This will return the difference of the two sensors and we can either use that immediately, or save it to another integer, in case we need to use it more than once. The final value can be used to determine if the sensors are reading two distinct values, or if they are somewhat similar. The higher the value, the higher the difference. Using an if statement, I can say, if the sensors values are within 100 increments of each other, do this.
if(SensorDifference <= 100) {
// Execute this.
}
Piggybacking off our previous code that should be still be running on the Arduino board,
we can use the two integers that contain the value of the Sensors to calculcate the difference and save it to a new integer.
The new code looks like this. The changes are in bold text.
/****************************************************************************************************************
* Program for reading two LDRs
* created by: Anuj
* (c)copyright
*
*
* Physical setup:
* Two LDRs connected to ground and Analog pin 1(Right) and Analog pin 2(left)
*
*
******************************************************************************************************************/
// Using Arduino Duemilinove
// Pin definitions - attaches a variable to a pin.
const int RightMotor = 2; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotor = 3; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int RightSensor = 1; // This pin is used to read the value of the Right Sensor.
const int LeftSensor = 2; // This pin is used to read the value of the Left Sensor.
// Variable definitions
int SensorLeft; // This stores the value of the Left Sensor pin to use later on in the sketch
int SensorRight; // This stores the value of the Right Sensor pin to use later on in the sketch
int SensorDifference; // This value is used to determine the difference between the Left and Right
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup() {
pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
digitalWrite(A1, HIGH); // Enables an internal pullup resistor
digitalWrite(A2, HIGH); // Enables an internal pullup resistor
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nReading Light sensors."); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop() {
SensorLeft = analogRead(LeftSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
SensorRight = analogRead(RightSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
SensorDifference = abs(SensorRight = SensorLeft);
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.
// This is known as debugging.
Serial.print("Left Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorLeft); // Prints the value of the Left Sensor.
Serial.print("\t"); // Prints a tab (space).
Serial.print("Right Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorRight); // Prints the value of the Right Sensor.
Serial.print("\t"); // Prints a tab (space).
Serial.print("Sensor Difference = "); // Prints the text inside the quotes.
Serial.print(SensorDifference);
Serial.print("\n"); // Prints a new line after all the necessary data is displayed.
}
Step 5: Interpreting Data
We have three different data values, Left Sensor, Right Sensor, and Sensor Difference.
Our robot will have the ability to go forward, left, or right based on this data. Lets break it down:
If the difference of the Left Sensor and the Right sensor is under a certain threshold, go straight. This threshold value will be customizable. For this we will add this code within the void loop of the LDR implementation code, after the code has reported the values of the Sensors.
if(SensorDifference <= 75) {
// Go straight
}
We will add the motor function to go straight in the next step. This step is purely for algorithm. This also applies to the next two sections for going left and right.
If the Right sensor detects more light than the Left, turn Right. Note, that while one sensor reads more than the other sensor, they might still be very close to each other. In this case both functions will be executed. To solve this problem, we can use an else if statement instead. This will only execute only if any if statements before this statement is not executed.
else if (RightSensor > Left sensor) {
// Turn Right
}
Similarly for the Left command
else if (LeftSensor > RightSensor) {
// Turn Left
}
In each of these three commands, we will add the actual motor control, which will be done by writing either HIGH or LOW to the digital pins 2 and 3. We will also add a debug line which will state which direction the robot wants to move. This is helpful for determining if the code is working properly, without having a robot freak out and possibly breaking itself in case the code is not working properly. I recomend running your code on your robot without the motors attached and checking the Serial Monitor to check if things are working properly. If they are, you can proceed to run the code with the motors.
Step 6: Finally! Motor Control!
Using digitalWrite(), we can switch the transistors on and off by turning the corresponding pin HIGH or LOW.
This will only switch the motor on and off in one direction. We will need an H-bridge to have full control of a motor.
We won't be using an H-Bridge in this Insctructable.
Connect one end of a 1k ohm resistor to pin 2 (right motor) . Connect the other end to an empty row on the breadboard. To add the transistor, align the base terminal of the transistor with the end of the resistor and insert it in the same row of the resistor. Connect the collector to one pin of the right motor. Connect the emitter (remaining pin) to ground. Connect the other pin of the motor to the positive terminal of the battery. Repeat for the Left motor on pin 3. Also, connect the negative terminal of the battery pack to the ground pin on the Arduino.
Here are the functions to get your robot moving.
Straight:
digitalWrite(RightMotor, HIGH);
digitalWrite(LeftMotor, HIGH);
Left:
digitalWrite(RightMotor, HIGH);
digitalWrite(LeftMotor, LOW);
Right:
digitalWrite(RightMotor, LOW);
digitalWrite(LeftMotor, HIGH);
Notice to turn, you must turn the opposite motor on.
Step 7: Final Code
* Program for a simple light Seeking Robot.
* created by: Anuj
* (c)copyright
*
* Robotic base should include differential drive.
*
* Physical setup:
* Two LDRs connected to ground and Analog pin 1(Right) and Analog pin 2(left)
* base of NPN Transistors on pins 11(right) and 12(left).
*
*
******************************************************************************************************************/
// Using Arduino Duemilinove
// Pin definitions - attaches a variable to a pin.
const int RightMotor = 2; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotor = 3; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int RightSensor = 1; // This pin is used to read the value of the Right Sensor.
const int LeftSensor = 2; // This pin is used to read the value of the Left Sensor.
// Variable definitions
int SensorLeft; // This stores the value of the Left Sensor pin to use later on in the sketch
int SensorRight; // This stores the value of the Right Sensor pin to use later on in the sketch
int SensorDifference; // This value is used to determine the difference between the Left and Right
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup() {
pinMode(LeftMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
digitalWrite(A1, HIGH); // Enables an internal pullup resistor
digitalWrite(A2, HIGH); // Enables an internal pullup resistor
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop() {
SensorLeft = 1023 - analogRead(LeftSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1);
SensorRight = 1023 - analogRead(RightSensor); // This reads the value of the sensor, then saves it to the corresponding integer.
delay(1);
SensorDifference = abs(SensorLeft - SensorRight); // This calculates the difference between the two sensors and then saves it to an integer.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.
Serial.print("Left Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorLeft); // Prints the value of the Left Sensor.
Serial.print("\t"); // Prints a tab (space).
Serial.print("Right Sensor = "); // Prints the text inside the quotes.
Serial.print(SensorRight); // Prints the value of the Right Sensor.
Serial.print("\t"); // Prints a tab (space).
// This section of the sketch is what actually interperets the data and then runs the motors accordingly.
if (SensorLeft > SensorRight && SensorDifference > 75) { // This is interpreted as if the Left sensor reads more light than the Right Sensor, Do this:
digitalWrite(RightMotor, HIGH); // This is used to turn Left. Notice the
digitalWrite(LeftMotor, LOW); // opposite motor runs to turn Left.
Serial.println("Left"); // This prints Left when the robot would actually turn Left.
}
if (SensorLeft < SensorRight && SensorDifference > 75) { // This is interpreted as if the Left sensor reads less light than the Right Sensor, Do this:
digitalWrite(RightMotor, LOW); // This is used to turn Right. Notice the
digitalWrite(LeftMotor, HIGH); // opposite motor runs to turn Right.
Serial.println("Right"); // This prints Right when the robot would actually turn Right.
}
else if (SensorDifference < 75) { // This is interpreted as if the difference between the two sensors is under 125 (Experiment to suit our sensors), Do this:
digitalWrite(RightMotor, HIGH); // This is used to go straight. Notice
digitalWrite(LeftMotor, HIGH); // both motors are enabled to go forward.
Serial.println("Forward"); // This prints Forward when the robot would actually go forward.
}
Serial.print("\n");
}





















































Visit Our Store »
Go Pro Today »




I absolutely adore the Tamiya base. I remember seeing it at Fry's one day and buying it with the express intent to build a robot :)
Never did finish the sensor package I wanted for mine so it just took a range reading on objects in front of it. Then took the last bid from that reading to decide on turning right or left. Always meant to go back and finish it but alas my cat had other plans ...... like puking on it ~.~
And I have a problem i cant get the same readings from both sensors they are very different i tried using 2 5k pots the change the values but they dont work well at all. I was thinking about constraining the values and mapping them to 0 - 255 but i cant figure out a code for that. I am using the LDRs for a Line Follower.
The transistors are being used to drive the motors. The Arduino cant source enough power on its own, the transistors are acting essentially like a light switch that the Arduino can flip on and off that connects directly to the power supply.
I'm rusty but the resistors are either acting for biasing or to limit the current, maybe both.
A lot of times you will see both a capacitor and a diode hooked up to a DC motor, the caps help with the initial power draw that the motor can place on your supply.
The diodes are there to prevent reverse power flow from the motors. Motors are act a lot like inductors. primarily because both are essentially wire windings. When you power an inductor it induces a magnetic field. When you stop the power flow to the inductor that field collapses and you are going to get a negative voltage spike.
As for the sensors the question is how different are the readings and are you taking readings under the same conditions? Just to decide if they are working properly you should probably take a multimeter and get a resistance reading. First by covering the sensor then by saturating it with as much light as you can. As long as those readings are similar and within spec for the part then you are fine.
The rest of the time the readings should be different. That is how the robot decides which direction to go.
it is an rc car and is using the arduino motor shield which has an H-bridge.
over serial it appears to work fine, but when driving it's steering wheels keep turning back and forth rapidly and randomly, sometimes stopping a bit in the left turn position.
i don't really know the problem, the left and right turning codes have worled otherwise in non-sensor tests.
i really want this to work, it is my first arduino robot, so i am kinda a noob.
i chose the rc car as a chassis because it was easier to obtain than the tamiya, although i might get that when i have more money.
also i was using the makershield, and now a full sized breadboard to maximize the space between the two sensors but has not helped,
anybody know the problem or how to fix it,
my rc car was also modified to be switched from rc or arduino control, so it still has a rc board within its body, but the motors are connected to both the arduino and rc board. the power is from the on-board battery holder with 4 AA batteries. the power connections are to the Vin and ground screw holes on the arduino motor shield.
any help will be greatly appreciated
How were you able to connect the Protoboard to the motors?
I am currently following this tutorial (in my spare time), and right now I have completed the base, platform, and building the protoboard. What is the next step?
I appreciate any help I can get
Thank you.
http://www.ladyada.net/make/mshield/index.html here it is.
SensorDifference = abs(SensorRight = SensorLeft);
should have - instead of =, like this:
SensorDifference = abs(SensorRight - SensorLeft);
it was wonderful to c ur robot
can u give me more details about how to connect the components in bread board
my id is kaifarun@gmail.com
help me friend