In this tutorial, we are going to make a light-chasing robot that moves toward areas of bright light. We will be using photoresistors to determine the brightness of the light. For a better understanding, have a look at the line following robot, surveillance robot car, obstacle-avoiding robot that we previously created.
A photoresistor, or photocell, is a light-dependent resistor (LDR). It acts just like regular resistors, but it responds to the amount of light detected. Using this property of the LDR we are going to make our robot using Arduino, the LDR module, and the L298N motor driver module. So let’s get started and built this robot.
What is LDR?
LDR stands for Light Dependent Resistor. LDRs are tiny light-sensing device which is also known as Photoresistors. It has two terminals. LDR is a resistor whose resistance varies as the amount of light falling on it varies. The resistance of these LDR decreases with an increase in intensity of light and vice-versa. This property allows us to use them for making light sensing circuits and many other applications.
LDR module Pinout
The LDR sensor module is a digital sensor as well as an analog sensor module, which is capable to detect light intensity. This sensor module also is known as the Photoresistor sensor module. This module has an onboard LDR(Light Dependent Resistor), that helps it to detect light. This sensor module has 4 terminals. The “DO” pin is a digital output pin and the “AO” pin is an analog output pin. The output of the module goes high in the absence of light and becomes low in the presence of light. The sensitivity of the sensor can be adjusted using the potentiometer provided on the board.
L298N Pinout
Datasheet
Motor controller |
L298N, drives 2 DC motors |
Operating Voltage |
5- 35V |
Logic voltage |
4.5 – 7 V |
Max current |
2A per channel |
Voltage Regulator |
78M05 |
Module dimensions |
43 x 43 x 28 mm |
Junction operating temperature |
-25 to 130 o Celsius |
Components required
- Arduino Board (UNO)
- USB –A to micro-USB cable
- Car chassis
- L298 motor driver module
- LDR sensor module
- 12V Battery
- On-Off- Switch
- DC Female Connector Jack
- Connecting wires
- Soldering iron
- Solder wire
- Hot Melt Glue Gun
Software Required
- Arduino IDE
Circuit Diagram
Follow this table in order to make your connections.
L298N driver Pins |
Arduino Pins |
ENA |
10 |
IN1 |
2 |
IN2 |
3 |
IN3 |
4 |
IN4 |
5 |
ENB |
11 |
LDR module pins |
Arduino pins |
VCC |
5v |
GND |
GND |
DO(right LDR) |
7 |
DO(left LDR) |
8 |
For L298N motor driver module, motors are connected to the motor terminals of the motor driver module. The motor driver has another 3 terminals, in which one is 12Volt connected to the battery. The GND terminal is connected to the battery’s negative terminal and it is also connected to the Arduino board GND pin and 5v is connected to 5v of the Arduino board.
Arduino Code
int RMotor_1 = 2;
int RMotor_2 = 3;
int LMotor_1 = 4;
int LMotor_2 = 5;
int REnable = 10;
int LEnable = 11;
int motor_speed = 60;
void setup() {
Serial.begin(9600);
Serial.println("GPIO test!");
pinMode(RMotor_1, OUTPUT);
pinMode(RMotor_2, OUTPUT);
pinMode(LMotor_1, OUTPUT);
pinMode(LMotor_2, OUTPUT);
pinMode(REnable, OUTPUT);
pinMode(LEnable, OUTPUT);
analogWrite(10, motor_speed);
analogWrite(11, motor_speed);
}
void loop() {
int ldrright = digitalRead(7);
int ldrleft = digitalRead(8);
if (ldrright == 0 && ldrleft == 0) {
Serial.println("F");
move_forward();
}
if (ldrright == 0 && ldrleft == 1) {
Serial.println("R");
turn_right();
}
if (ldrright == 1 && ldrleft == 0) {
Serial.println("L");
turn_left();
}
if (ldrright == 1 && ldrleft == 1) {
Serial.println("S");
move_stop();
}
delay(100);
}
void move_forward() {
digitalWrite(RMotor_1, HIGH);
digitalWrite(RMotor_2, LOW);
digitalWrite(LMotor_1, LOW);
digitalWrite(LMotor_2, HIGH);
}
//NOT USING BACKWARD
void move_backward() {
digitalWrite(RMotor_1, HIGH);
digitalWrite(RMotor_2, LOW);
digitalWrite(LMotor_1, LOW);
digitalWrite(LMotor_2, HIGH);
}
void turn_right() {
digitalWrite(RMotor_1, LOW);
digitalWrite(RMotor_2, HIGH);
digitalWrite(LMotor_1, LOW);
digitalWrite(LMotor_2, HIGH);
}
void turn_left() {
digitalWrite(RMotor_1, HIGH);
digitalWrite(RMotor_2, LOW);
digitalWrite(LMotor_1, HIGH);
digitalWrite(LMotor_2, LOW);
}
void move_stop() {
digitalWrite(RMotor_1, LOW);
digitalWrite(RMotor_2, LOW);
digitalWrite(LMotor_1, LOW);
digitalWrite(LMotor_2, LOW);
}
Troubleshooting
Robot is moving too fast?
For this change value of motor speed in the code. Note that 1 is the minimum value and 255 is the maximum speed.
No Power supply?
Try changing your batteries. Ensure that you must supply 12v to the L298 Motor Driver module. A Voltage rating less than this will not work.
Project not working?
Ensure that the circuit you have made is according to the circuit diagram. Check for loose connections. Cross-check for motor connections.
Motors are rotating in opposite direction?
For this, reverse the terminals of the motor connecting the Motor driver module.
LDR is not responding?
Try adjusting POT provided on the LDR sensor module. Keep adjusting until you get the desired