Arduino UNO-Based Autonomous Obstacle-Avoiding Robot with Ultrasonic and IR Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a circuit that includes an Arduino UNO microcontroller, various sensors, motor drivers, motors, LEDs, a pushbutton, and a wireless relay. The circuit is designed to control the movement of a robot based on sensor inputs and to provide visual feedback using LEDs.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
L298N DC Motor Driver
- Description: A dual H-Bridge motor driver module.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
DC Motor
- Description: A simple DC motor.
- Pins: pin 1, pin 2
LM2596
- Description: A step-down voltage regulator.
- Pins: IN, OUT
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic distance sensor.
- Pins: VCC, TRIG, ECHO, GND
IR Sensor
- Description: An infrared sensor.
- Pins: out, gnd, vcc
12V 200Ah Battery
- Description: A high-capacity battery.
- Pins: GND, 12V
Pushbutton
- Description: A simple pushbutton switch.
- Pins: Pin 2, Pin 1, Pin 3, Pin 4
LED: Two Pin (red)
- Description: A red LED.
- Pins: cathode, anode
12V Wireless Relay
- Description: A wireless relay module.
- Pins: NEGATIF, POSITIF, NO, C, NC
Wiring Details
Arduino UNO
5V connected to:
- HC-SR04 Ultrasonic Sensor (VCC)
- IR Sensor (vcc)
- IR Sensor (vcc)
GND connected to:
- HC-SR04 Ultrasonic Sensor (GND)
- IR Sensor (out)
- IR Sensor (out)
- LED: Two Pin (red) (cathode)
- LED: Two Pin (red) (cathode)
- 12V Wireless Relay (NEGATIF)
- LM2596 (OUT)
Vin connected to:
- LM2596 (OUT)
D11 connected to:
- L298N DC Motor Driver (IN4)
D10 connected to:
- L298N DC Motor Driver (IN3)
D9 connected to:
- L298N DC Motor Driver (IN2)
D8 connected to:
- L298N DC Motor Driver (IN1)
D7 connected to:
- IR Sensor (gnd)
D6 connected to:
- IR Sensor (gnd)
D5 connected to:
- HC-SR04 Ultrasonic Sensor (TRIG)
D4 connected to:
- HC-SR04 Ultrasonic Sensor (ECHO)
D3 connected to:
- LED: Two Pin (red) (anode)
D2 connected to:
- LED: Two Pin (red) (anode)
L298N DC Motor Driver
OUT1 connected to:
- DC Motor (pin 2)
OUT2 connected to:
- DC Motor (pin 1)
12V connected to:
- 12V Wireless Relay (C)
GND connected to:
- 12V 200Ah Battery (GND)
- LM2596 (IN)
OUT3 connected to:
- DC Motor (pin 2)
OUT4 connected to:
- DC Motor (pin 1)
12V Wireless Relay
NEGATIF connected to:
- Arduino UNO (GND)
- LM2596 (OUT)
POSITIF connected to:
- 12V Wireless Relay (NO)
- LM2596 (IN)
- Pushbutton (Pin 1)
12V 200Ah Battery
GND connected to:
- LM2596 (IN)
- L298N DC Motor Driver (GND)
12V connected to:
- Pushbutton (Pin 3)
Pushbutton
Pin 1 connected to:
- 12V Wireless Relay (POSITIF)
Pin 3 connected to:
- 12V 200Ah Battery (12V)
Code Documentation
#define LEFT 6
#define RIGHT 7
#define trigPin 5
#define echoPin 4
#define LedLeft 2
#define LedRight 3
const int LeftMotorForward = 9;
const int LeftMotorBackward = 8;
const int RightMotorForward = 10;
const int RightMotorBackward = 11;
const int minDistance = 10;
const int maxDistance = 300;
void setup() {
Serial.begin(9600);
pinMode(RIGHT, INPUT_PULLUP); // Try with pull-up resistor
pinMode(LEFT, INPUT_PULLUP);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(RightMotorForward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(LedLeft, OUTPUT);
pinMode(LedRight, OUTPUT);
}
void loop() {
long distance = read_cm();
int Right_Value = digitalRead(RIGHT);
int Left_Value = digitalRead(LEFT);
Serial.print("Right Sensor = ");
Serial.print(Right_Value);
Serial.print(" | Left Sensor = ");
Serial.print(Left_Value);
Serial.print(" | Distance = ");
Serial.print(distance);
Serial.println(" cm");
if ((Right_Value == 0) && (distance >= minDistance && distance <= maxDistance) && (Left_Value == 0)) {
Serial.println("Moving Forward");
forward();
} else if ((Right_Value == 0) && (Left_Value == 1)) {
Serial.println("Turning Right");
turnRight();
} else if ((Right_Value == 1) && (Left_Value == 0)) {
Serial.println("Turning Left");
turnLeft();
} else if ((Right_Value == 0) && (Left_Value == 0)) {
Serial.println("Stopping");
stop();
} else if (distance < minDistance) {
Serial.println("Moving Backward");
backward();
}
delay(50);
}
long read_cm() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long time = pulseIn(echoPin, HIGH, 30000); // 30 ms timeout
if (time == 0) {
return maxDistance + 1;
}
return time / 29 / 2;
}
void forward() {
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
LEDForwardSignal();
}
void backward() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
LEDBackSignal();
}
void turnRight() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(LedLeft, LOW);
digitalWrite(LedRight, HIGH);
}
void turnLeft() {
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(LedLeft, HIGH);
digitalWrite(LedRight, LOW);
}
void stop() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(LedLeft, LOW);
digitalWrite(LedRight, LOW);
}
void LED