(w/ Video) Basic Arduino Robot, Light Seeker! using arduino

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.
Basic Arduino Robot
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

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 2: 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 3: 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 4: 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() {
Basic Arduino Robot
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.
}

 

For more detail: (w/ Video) Basic Arduino Robot, Light Seeker!

 


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top