Hey there! In this tutorial, we're going to build a cool light-chasing robot that moves towards areas of bright light. To do this, we will be using photo resistors (also called photocells), which are light-dependent resistors that respond to the amount of light detected. This project uses an Arduino, an LDR module, and an L298N motor driver module. If you've built our line-following robot, surveillance robot car, or obstacle-avoiding robot before, this project will be right up your alley. So grab your components and let's get started!
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
What is LDR sensor?
An LDR sensor is a component that changes its resistance it depend how much light fall in it. They are commonly used in light meters, cameras, and automatic lighting systems. LDRs are inexpensive and easy to integrate into electronic circuits. They are also popular in robotics projects, where they are used to detect the brightness of the surrounding environment and trigger a response.
L298N Pinout
The L298N motor driver module has 15 pins in total. Here is a brief overview of each pin and its function:
- ENA: Enables Motor A
- IN1: Input 1 for Motor A
- IN2: Input 2 for Motor A
- GND: Ground
- +5V: Power supply for logic circuitry
- +12V: Power supply for the motors
- OUT1: Output 1 for Motor A
- OUT2: Output 2 for Motor A
- OUT3: Output 1 for Motor B
- OUT4: Output 2 for Motor B
- IN3: Input 1 for Motor B
- IN4: Input 2 for Motor B
- ENB: Enables Motor B
- +V SENSE: Voltage sensing pin for the power supply
- GND: Ground
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 |
CIRCUIT DIAGRAM
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.
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 |
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);
}