Basic Arduino Robot, Light Seeker!

Powered by Instructables.
<

Step 1: (w/ Video) Basic Arduino Robot, Light Seeker!

Many times I have Googled for "Arduino Robot" in hopes of finding a robot to build. I end up finding many versions of a obstacle avoiding robot that uses either an infrared or a sonar range finder sensor mounted on a servo to detect objects in its way while randomly roaming the area around it. The problem with this robot is it is expensive and very advanced in some aspects for a "first robot" to make with an Arduino board. The cost of a servo is about $10, and a range finder sensor runs from about $10 - $30.

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.)




If you have a robot base, with and Arduino and full motor controller, you can use it to control your robot with a computer:


 







Step 2: Materials

The materials you need are:
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 3: Robot Base

There isn't much to say here. A lot of different robotic bases have been shown on Instructables and on various other websites. The main parts of a robotic base is 2 motors, corresponding gearboxes, supply battery pack, a base platform, and an H bridge* 

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 4: Incorporating Light sensors

The light sensors are CdS cells which are used to detect light brightness. The two leads change resistance based on the amount of light received. Using the sensor requires what's called a voltage divider. One pin of the LDR is connected to ground. The other is connected to the analog input pin and a pullup resistor is connected to the same analog input pin and +5v. Luckily, Arduino has a pullup resistor built in to every pin. To use the pullup resistor, you must first define the pin as an input, then use digitalWrite() to set the pin HIGH like so:

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 5: Incorporating Light sensors Part 2

After waving your hands in front of the light sensors and watching them change in value, you'll notice when the light sensor detects less light, the Sensor value will be rise, and when it detects more light, the Sensor value will drop. This is because the Sensor decreases in resistance when it detects a bright light. When there is less resistance to across the LDR's two leads, the Analog input pin will start to read a ground signal, or something close to it. When the the LDR is receiving less light, the resistance to ground will increase, and more electricity will flow to the Analog pin from the pullup resistor which is connected to +5v.

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 6: Interpreting Data

At this point, we have data from our light sensors and we want to be able to execute functions based on these values.

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 7: Finally! Motor Control!

At last, We can now control our motors accordingly to the algorithm in the previous step.
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 8: 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");



Step 9: Now its your turn!

Make your own variants of this robot and post pictures of them below in the comments. I posted pictures of an earlier version of this robot. I'll post pictures of your robot on this page along with my pictures.





License: Attribution-NonCommercial-ShareAlike.

Tools and Space
Age Level
Category
Time Per Project
1div id="in-node-search">

Search

C