Introduction: Easy Arduino Light Following Robot

This is a tutorial on how to make a light following robot using Arduino, it has been simplified so that beginners can attempt this project too. This project should only take you at the most of an hour. I hope you enjoy.

Step 1: Materials

Arduino Uno: http://www.freetronics.com.au/products/eleven#.VZn...

H-Bridge shield: http://www.freetronics.com.au/products/hbridge-dua...

Chassis: http://www.hobbyking.com/hobbyking/store/__26249__...

2X LDR/Light Dependant Resistors

2X 100k Ω Resistors

Jumper leads

3X 9v

Step 2: Wiring

With the wires on the bread board that go to the side, 2 go to 5v on arduino and the other one goes to GND

It is easier to follow the pictures then me explain.

First, connect the H-Bridge shield to the Arduino Uno

Then, connect the LDR's, like in the diagram

After, insert the positive and negative of the motors in to the shield

*Now, connect 2 9v batteries in a series and then positive and negative to the spots on the shield

*Finally plug a 9v battery into the Arduino

(*Last 2 are for when you have uploaded the code)

Step 3: Put It on the Chassis

Put the creation on your chassis how ever you please, make it look how ever you want as well. There is much I can say about this step though.

Step 4: Code

Please respect that it took a while to figure out and write this code:

//Code by Jason McLaughlin

//2015

const int channel_a_enable = 6; //start motor a /left

const int channel_a_input_1 = 4; //positive/negative 1

const int channel_a_input_2 = 7; //positive/negative 2

const int channel_b_enable = 5; //start motor b /right

const int channel_b_input_3 = 3; //positive/negative 1

const int channel_b_input_4 = 2; //positive/negative 2

const int RightSensor = A1; // Read the right sensor

const int LeftSensor = A2; // Read 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

void setup() {

pinMode( channel_a_enable, OUTPUT ); // Channel A enable

pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1

pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2

pinMode( channel_b_enable, OUTPUT ); // Channel B enable

pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3

pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4

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 LDR

digitalWrite(A2, HIGH); // Enables LDR

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() }

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:

analogWrite( channel_a_enable, 255);

digitalWrite( channel_a_input_1, LOW);

digitalWrite( channel_a_input_2, HIGH);

analogWrite( channel_b_enable, 255);

digitalWrite( channel_b_input_3, HIGH);

digitalWrite( channel_b_input_4, LOW);

Serial.println("Left"); // This prints Left when the robot would actually turn Left.

delay(50);

}

if (SensorLeft < SensorRight && SensorDifference > 75) { // This is interpreted as if the Left sensor reads less light than the Right Sensor, Do this:

analogWrite( channel_a_enable, 255);

digitalWrite( channel_a_input_1, HIGH);

digitalWrite( channel_a_input_2, LOW);

analogWrite( channel_b_enable, 255);

digitalWrite( channel_b_input_3, LOW);

digitalWrite( channel_b_input_4, HIGH);

Serial.println("Right"); // This prints Right when the robot would actually turn Right.

delay(50);

}

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:

analogWrite( channel_a_enable, 255);

digitalWrite( channel_a_input_1, HIGH);

digitalWrite( channel_a_input_2, LOW);

analogWrite( channel_b_enable, 255);

digitalWrite( channel_b_input_3, HIGH);

digitalWrite( channel_b_input_4, LOW);

Serial.println("Forward"); // This prints Forward when the robot would actually go forward.

delay(50);

}

Serial.print("\n");

}

Comments

author
RishabhM42 (author)2016-12-03

Any circuit diagram for this?

author
AkashK104 (author)2016-11-05

can we use l298n instead of that ardumoto

author
Saeki Hirai (author)2015-08-12

may I ask how much is the cost of this project? thanks! ?

author

It really depends on what you are making it out of. This is a flexible diy, meaning you don't have to do everything EXACTLY as the author/creator did.You will have to look into your available materials and make a couple of trips to some stores (also use internet) and see what you can get for how much money you have available. It shouldn't be that hard and I find that a lot of cheap items can be used for this project. For instance, the chassis can be made of cardboard (easy to find around the house) or some old container (plastic works well). For wheels, just connect some lego tires to the motors. There are a lot of cheap alternatives to expensive project, you just have to know where to find them.

author
ShioriToshiko (author)2015-10-11

Is there any way I could make this with infrared sensors and a hand-held infrared-light-emiting "flashlight"? I want to make something like this but the robot will be a "minion" that follows wherever my hand is pointing (general location) as if it is obeying my commands (halloween costume ideas). I don't want to make things obvious by using a regular flashlight, so I want to use "invisible" light. Any help is appreciated, thanks.

author
Computothought (author)2015-07-06

Your code would not compile for me...

author

It should be all right now but just in case here it is:

//code by Jason Mclaughlin

//2015

const int channel_a_enable = 6; //start motor a /left

const int channel_a_input_1 = 4; //positive/negative 1

const int channel_a_input_2 = 7; //positive/negative 2

const int channel_b_enable = 5; //start motor b /right

const int channel_b_input_3 = 3; //positive/negative 1

const int channel_b_input_4 = 2; //positive/negative 2

const int RightSensor = A1; // Read the right sensor

const int LeftSensor = A2; // Read 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

void setup() {

pinMode( channel_a_enable, OUTPUT ); // Channel A enable

pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1

pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2

pinMode( channel_b_enable, OUTPUT ); // Channel B enable

pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3

pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4

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:

analogWrite( channel_a_enable, 255);

digitalWrite( channel_a_input_1, LOW);

digitalWrite( channel_a_input_2, HIGH);

analogWrite( channel_b_enable, 255);

digitalWrite( channel_b_input_3, HIGH);

digitalWrite( channel_b_input_4, LOW);

Serial.println("Left"); // This prints Left when the robot would actually turn Left.

delay(50);

}

if (SensorLeft < SensorRight && SensorDifference > 75) { // This is interpreted as if the Left sensor reads less light than the Right Sensor, Do this:

analogWrite( channel_a_enable, 255);

digitalWrite( channel_a_input_1, HIGH);

digitalWrite( channel_a_input_2, LOW);

analogWrite( channel_b_enable, 255);

digitalWrite( channel_b_input_3, LOW);

digitalWrite( channel_b_input_4, HIGH);

Serial.println("Right"); // This prints Right when the robot would actually turn Right.

delay(50);

}

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:

analogWrite( channel_a_enable, 255);

digitalWrite( channel_a_input_1, HIGH);

digitalWrite( channel_a_input_2, LOW);

analogWrite( channel_b_enable, 255);

digitalWrite( channel_b_input_3, HIGH);

digitalWrite( channel_b_input_4, LOW);

Serial.println("Forward"); // This prints Forward when the robot would actually go forward.

delay(50);

}

Serial.print("\n");

}

author

It seems to compile now. will save for when I I get the hardware.

author

ok let me check it for you

About This Instructable

4,688views

18favorites

License:

Bio: I love the song Come Get Your Love
More by JaceDuino :How to control motors with Arduino and RC receiver in 10 minutesHow to control Arduino with RC receiver in 10 minutesEasy Arduino Light following Robot
Add instructable to: