Arduino UNO-Based Battery-Powered Robotic Vehicle with IR and Ultrasonic Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control two DC motors using an Arduino UNO and an L298N motor driver. It also includes two IR sensors and an ultrasonic distance sensor for obstacle detection. The circuit is powered by two 3.7V batteries connected in series through a rocker switch.
Component List
Arduino UNO
- Description: 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: Dual H-Bridge motor driver.
- 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
Motor amarillo motorreductor hobby (2 units)
- Description: Hobby DC motor with gearbox.
- Pins: vcc, GND
3.7V Battery (2 units)
- Description: Rechargeable lithium-ion battery.
- Pins: +, -
IR Sensor (2 units)
- Description: Infrared sensor for obstacle detection.
- Pins: out, gnd, vcc
HC-SR04 Ultrasonic Distance Sensor
- Description: Ultrasonic sensor for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
Rocker Switch
- Description: On/Off switch.
- Pins: output, input
Wiring Details
Arduino UNO
5V connected to:
- VCC of HC-SR04 Ultrasonic Distance Sensor
- vcc of IR Sensor 1
- vcc of IR Sensor 2
GND connected to:
- GND of 3.7V Battery 1
- GND of L298N DC Motor Driver
- GND of HC-SR04 Ultrasonic Distance Sensor
- gnd of IR Sensor 1
- gnd of IR Sensor 2
Vin connected to:
- 5V of L298N DC Motor Driver
D12 connected to:
- TRIG of HC-SR04 Ultrasonic Distance Sensor
D11 connected to:
- ECHO of HC-SR04 Ultrasonic Distance Sensor
D10 connected to:
- ENA of L298N DC Motor Driver
D9 connected to:
- IN1 of L298N DC Motor Driver
D8 connected to:
- IN2 of L298N DC Motor Driver
D7 connected to:
- IN3 of L298N DC Motor Driver
D6 connected to:
- IN4 of L298N DC Motor Driver
D5 connected to:
- ENB of L298N DC Motor Driver
D3 connected to:
- out of IR Sensor 1
D2 connected to:
- out of IR Sensor 2
L298N DC Motor Driver
OUT1 connected to:
- GND of Motor 2
OUT2 connected to:
- vcc of Motor 2
OUT3 connected to:
- GND of Motor 1
OUT4 connected to:
- vcc of Motor 1
12V connected to:
- output of Rocker Switch
Motor amarillo motorreductor hobby
Motor 1
- vcc connected to OUT4 of L298N DC Motor Driver
- GND connected to OUT3 of L298N DC Motor Driver
Motor 2
- vcc connected to OUT2 of L298N DC Motor Driver
- GND connected to OUT1 of L298N DC Motor Driver
3.7V Battery
Battery 1
-
- connected to - of Battery 2
-
- connected to GND of Arduino UNO
-
Battery 2
-
- connected to input of Rocker Switch
-
IR Sensor
IR Sensor 1
- vcc connected to 5V of Arduino UNO
- gnd connected to GND of Arduino UNO
- out connected to D3 of Arduino UNO
IR Sensor 2
- vcc connected to 5V of Arduino UNO
- gnd connected to GND of Arduino UNO
- out connected to D2 of Arduino UNO
HC-SR04 Ultrasonic Distance Sensor
- VCC connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- TRIG connected to D12 of Arduino UNO
- ECHO connected to D11 of Arduino UNO
Rocker Switch
- input connected to + of Battery 2
- output connected to 12V of L298N DC Motor Driver
Documented Code
// Définition des broches
const int irSensor1Pin = A0; // Capteur infrarouge 1
const int irSensor2Pin = A1; // Capteur infrarouge 2
const int ultrasonicTrigPin = A3; // Broche Trigger du capteur ultrason
const int ultrasonicEchoPin = A2; // Broche Echo du capteur ultrason
// Broches pour le driver de moteur
const int motor1EnablePin = 5; // Broche pour activer le moteur 1
const int motor1DirectionPin = 8; // Broche pour la direction du moteur 1
const int motor2EnablePin = 10; // Broche pour activer le moteur 2
const int motor2DirectionPin = 7; // Broche pour la direction du moteur 2
void setup() {
Serial.begin(9600); // Initialisation des broches
pinMode(motor1EnablePin, OUTPUT);
pinMode(motor1DirectionPin, OUTPUT);
pinMode(motor2EnablePin, OUTPUT);
pinMode(motor2DirectionPin, OUTPUT);
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
}
void loop() {
// Lire les capteurs infrarouges
int irValue1 = analogRead(irSensor1Pin);
int irValue2 = analogRead(irSensor2Pin);
// Afficher les valeurs des capteurs infrarouges
Serial.print("IR Sensor 1: ");
Serial.println(irValue1);
Serial.print("IR Sensor 2: ");
Serial.println(irValue2);
// Mesurer la distance avec le capteur ultrason
long duration, distance;
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
duration = pulseIn(ultrasonicEchoPin, HIGH);
distance = (duration * 0.034) / 2; // Calcul de la distance en cm
// Afficher la distance mesurée
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Contrôler les moteurs en fonction de l'interrupteur
digitalWrite(motor1EnablePin, HIGH); // Activer le moteur 1
digitalWrite(motor1DirectionPin, HIGH); // Définir la direction du moteur 1
digitalWrite(motor2EnablePin, HIGH); // Activer le moteur 2
digitalWrite(motor2DirectionPin, HIGH); // Définir la direction du moteur 2
digitalWrite(motor1EnablePin, LOW); // Désactiver le moteur 1
digitalWrite(motor2EnablePin, LOW); // Désactiver le moteur 2
delay(500); // Attendre un peu avant la prochaine lecture
}
This code initializes the pins for the IR sensors, ultrasonic sensor, and motor driver. It reads the values from the IR sensors and the ultrasonic sensor, prints them to the serial monitor, and controls the motors based on the readings.