In this tutorial, we will learn how to make an LED Arduino obstacle-avoiding robot car. The main component here is the Arduino board. This is a four-wheel robot. HC-SR04 Sensor, L298N H-bridge motor drive, and SG90 Servo are used for this.
You can watch the following video or read the written tutorial below.
Overview
In this robot car, the task is to detect obstacles in front and avoid them. This process occurs continuously. An ultrasonic sensor (HC-SR04) is used to detect obstacles in front. If the robot car encounters an obstacle while moving forward, it stops and utilizes the ultrasonic sensor attached to the servo motor to monitor both sides. It then proceeds along the path with fewer obstacles.
The specialty of this robot lies in the use of LEDs that change according to the movement of the car. The white LED lights up when moving forward. The red LED lights up when moving backward. When its ultrasonic sensor turns to the left, the green LED lights up. When the ultrasonic sensor turns to the right, the blue LED lights up.
Components Needed
Before we begin, make sure you have gathered all the necessary components. The purchase links are in the description of my YouTube video.
- Arduino Uno or compatible board
- Four Plastic Wheels with four TT Gear Motors
- ECO Board
- L298N motor driver module
- Ultrasonic sensor (HC-SR04)
- SG90 servo motor
- Jumper wires
- Two 18650 Rechargeable Battery 3.7V
- 18650 Li-ion Battery Holder 2-Way
- Toggle Switch
- 1W Six LED ( two white, two red, one blue, one green )
- USB cable for programming the Arduino
- Screwdriver and assorted screws
- Glue Gun
I use the HC-SR04 Ultrasonic sensor for this.
Assemble the Chassis
Glue the four-gear motors to the ECO Board using the glue gun. Glue the battery holder to the ECO board using the glue gun. If this is difficult to do, watch a tutorial video.
LED Obstacle Avoiding Robot Circuit Diagram
The Obstacle Avoiding Robot Circuit Diagram is not complicated. First, you download the circuit diagram from the download button. It’s more obvious. Small jumpers should be connected to ENA and ENB on the L298N motor driver you are using. It is connected by default when you buy it. Never apply power by switching the VCC and GND of the ultrasonic sensor. If so, it may be damaged. Use two 3.7V 18659 li-ion batteries to provide power. Do not use more than two batteries. It cannot use 9V battery. If a 9V battery is used, only the ultrasonic sensor and servo motor will work. Gear motor not working. The reason for that is that the 9V battery does not have enough amperage.
Connect the Battery Power Connection
First solder the switch to the battery holder. Solder the red wire of the battery holder to one end of the switch and solder a red wire to the other end of the switch. Stik the switch to the ECO Board with the glue gun.
Use two 3.7V 18659 li-ion batteries to provide power. Do not use more than two batteries. It cannot use 9V battery. If a 9V battery is used, only the ultrasonic sensor and servo motor will work. Gear motor not working. The reason for that is that the 9V battery does not have enough amperage.
- Connect the Red wire (+) of the battery holder to the 12V In of the L298N Motor Driver and to the VIN Pin of the Arduino board.
- Connect the Black wire (-) of the battery holder to the GND In of the L298N Motor Driver and to the GND Pin of the Arduino board.
Connecting the four inputs of the L298N Motor Driver to the Arduino UNO board.
The control signals for each motor come from the Arduino or microcontroller. There are two input pins for each motor (IN1/IN2 for one motor and IN3/IN4 for the other). By applying different combinations of high and low signals to these pins, the Arduino controls the direction and speed of each motor.
The L298N has two enable pins (ENA and ENB) that allow the user to enable or disable the corresponding H-bridge. PWM (Pulse Width Modulation) signals can be applied to these pins to control the speed of the motors. Here I am not using ENA and ENB. It has a mini jumper connected to 5V.
In this way, connect the digital pins of the Arduino board and the inputs of the L298N motor driver by jumper wires.
- Connect the IN1 pin of the motor driver to digital pin D4 on the Arduino.
- Connect the IN2 pin of the motor driver to digital pin D5 on the Arduino.
- Connect the IN3 pin of the motor driver to digital pin D6 on the Arduino.
- Connect the IN4 pin of the motor driver to digital pin D7 on the Arduino.
Connecting the four-gear motors to the l298N Motor Driver.
In this way, connect the wires of the gear motors to the motor power connectors of the l298N motor driver.
- Connect the two left motor Red wires to the “OUT1” terminals on the motor driver.
- Connect the two left motor Black wires to the “OUT2” terminals on the motor driver.
- Connect the two right motor Black wires to the “OUT3” terminals on the motor driver.
- Connect the two right motor Red wires to the “OUT4” terminals on the L298N motor driver.
Attaching the Sg90 Servo Motor to the chassis and connecting it to the Arduino board.
The SG90 is a popular and widely used micro servo motor, often employed in robotics and other electronic projects. The SG90 servo motor consists of a small DC motor, a set of gears, a control circuit, and a potentiometer.
Attach the servo motor to the ECO board using a screws as shown in the image below.
In this way, connect the Arduino board and the SG90 Servo motor by jumper wires.
- Connect the Signal (Orange Wire) from the servo motor to a digital pin D10 on the Arduino.
- Connect the VCC (Red Wire) from the servo motor to the 5V pin on the Arduino.
- Connect the GND (Brown Wire) from the servo motor to the GND pin on the Arduino.
Connect the Front and Back LEDs
Connects two LEDs each to the front and back. Connect two white LEDs in parallel to the front and two red LEDs in parallel to the back. I use 1W LED bulbs for the front and rear lights. No resistors are needed to connect these.
In this way, connect the Arduino board and the Front and Back LEDs by jumper wires.
- Connect the (+) wires of the front lights to digital pin 8 of the Arduino board.
- Connect the (-) wires of the front lights to GND of the Arduino board.
- Connect the (+) wires of the backlights to digital pin 9 of the Arduino board.
- Connect the (-) wires of the back lights to GND of the Arduino board.
Connect the LEDs to the ultrasonic sensor
Install the two LEDs and connect the wires as shown in the figure below.
Solder the (-) of the two LEDs to the GND of the ultrasonic sensor.
Connect the LED and the ultrasonic sensor to the Arduino board
In this way, connect the Arduino board and the Green and Blue LEDs by jumper wires.
- Connect the (+) wire of the Green LED to digital pin 13 of the Arduino board.
- Connect the (-) wire of the Green LED to GND pin of the ultrasonic sensor.
- Connect the (+) wire of the Blue LED to digital pin 3 of the Arduino board.
- Connect the (-) wire of the Blue LED to GND pin of the ultrasonic sensor.
In this way connect the ultrasonic sensor (HC-SR04) with the Arduino board,
- Connect the VCC (power) pin of the ultrasonic sensor to the 5V pin on the Arduino.
- Connect the GND (ground) pin of the sensor to the GND pin on the Arduino.
- Connect the TRIG (trigger) pin of the ultrasonic sensor to Analog pin A1 on the Arduino.
- Connect the ECHO pin of the sensor to another Analog pin A2 on the Arduino.
Upload the Arduino Sketch
Before uploading the code, download the New Ping Library and add it to the Arduino IDE. The Newping Library should be added to the Library of the Arduino IDE. Otherwise the code cannot be uploaded. Download from below.
The file type of the Newping Library you downloaded is a zip file. Follow the steps below and add the Zip file to the Arduino IDE.
In the Arduino IDE, navigate to Sketch > Include Library > Add . ZIP Library. At the top of the drop-down list, select the option to “Add . ZIP Library”.
After that, copy the following Arduino code and paste it into the new sketch in the Arduino IDE. Select the board and port and upload the code. If this is difficult to do, watch a tutorial video.
You can download the Arduino code and open it directly through the Arduino IDE. Click the Download button below to download the Arduino code
#include <Servo.h> //Servo motor library. This is standard library //Robot Lk
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library //Robot Lk
//Robot Lk YouTube Channel-https://www.youtube.com/c/RobotLk
//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
//LED Robot Lk
const int ForwardLED = 8;
const int BackwardLED = 9;
const int LeftLED = 11;
const int RightLED = 12;
const int LeftSensorLED = 13;
const int RightSensorLED = 3;
//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
//Robot Lk
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(ForwardLED, OUTPUT);//Robot Lk
pinMode(BackwardLED, OUTPUT);//Robot Lk
pinMode(LeftLED, OUTPUT);//Robot Lk
pinMode( RightLED, OUTPUT);
pinMode(LeftSensorLED, OUTPUT);
pinMode( RightSensorLED, OUTPUT);
servo_motor.attach(10); //our servo pin
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 45){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft){
turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
digitalWrite(RightSensorLED, HIGH);
delay(200);
digitalWrite(RightSensorLED, LOW);
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
digitalWrite(LeftSensorLED, HIGH);//Robot Lk
delay(500);
digitalWrite(LeftSensorLED, LOW);//Robot Lk
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
int readPing(){
delay(70);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(ForwardLED, LOW);
digitalWrite(BackwardLED, LOW);
digitalWrite(LeftLED, LOW);
digitalWrite(RightLED, LOW);
digitalWrite(LeftSensorLED, LOW);
digitalWrite(RightSensorLED, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(ForwardLED, HIGH);//Robot Lk//Robot Lk
}
}
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
digitalWrite(BackwardLED, HIGH);//Robot Lk
}
void turnRight(){
digitalWrite(RightLED, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
digitalWrite(LeftLED, HIGH);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
Code language: PHP (php)
This code uses a servo motor to scan for obstacles. When an obstacle is detected within 45 cm, the robot stops, rotates the servo to find a clear path, and then moves forward. The specialty of this robot lies in the use of LEDs that change according to the movement of the car. The white LED lights up when moving forward. The red LED lights up when moving backward. When its ultrasonic sensor turns to the left, the green LED lights up. When the ultrasonic sensor turns to the right, the blue LED lights up.