Arduino Mega 2560-Based Obstacle Avoidance Robot with Multiple HC-SR04 Sensors and Motor Controllers
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control multiple HC-SR04 ultrasonic sensors, motor controllers, and relay modules using an Arduino Mega 2560. The system reads distances from the sensors and implements simple obstacle avoidance logic to control the motors. Each sensor is connected to a dedicated pin on the Arduino Mega 2560.
Component List
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic distance sensor
- Pins: VCC, TRIG, ECHO, GND
Arduino Mega 2560
- Description: Microcontroller board based on the ATmega2560
- Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL
1 Channel 5V Relay Module
- Description: Relay module for switching high voltage devices
- Pins: VCC+, VCC- (GND), IN, N.O., COM, N.C.
ZS-X11H v2 350w Motor Controller
- Description: Motor controller for controlling DC motors
- Pins: 5v, 0-5v, GND, DIR, BRAKE, Stop, A, B, C, VCC
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V (common net)
- GND: Connected to GND (common net)
- TRIG:
- Sensor 1: Connected to Arduino Mega 2560 D8 PWM
- Sensor 2: Connected to Arduino Mega 2560 D10 PWM
- Sensor 3: Connected to Arduino Mega 2560 D6 PWM
- Sensor 4: Connected to Arduino Mega 2560 D4 PWM
- Sensor 5: Connected to Arduino Mega 2560 D12 PWM
- Sensor 6: Connected to Arduino Mega 2560 D2 PWM
- ECHO:
- Sensor 1: Connected to Arduino Mega 2560 D9 PWM
- Sensor 2: Connected to Arduino Mega 2560 D11 PWM
- Sensor 3: Connected to Arduino Mega 2560 D7 PWM
- Sensor 4: Connected to Arduino Mega 2560 D5 PWM
- Sensor 5: Connected to Arduino Mega 2560 D13 PWM
- Sensor 6: Connected to Arduino Mega 2560 D3 PWM
Arduino Mega 2560
- 5V: Connected to ZS-X11H v2 350w Motor Controller 5v (common net)
- GND: Connected to GND (common net)
- A0: Connected to 1 Channel 5V Relay Module IN
- A1: Connected to 1 Channel 5V Relay Module IN
- D22: Connected to ZS-X11H v2 350w Motor Controller 0-5v
- D23: Connected to ZS-X11H v2 350w Motor Controller 0-5v
- D24: Connected to ZS-X11H v2 350w Motor Controller BRAKE
- D25: Connected to ZS-X11H v2 350w Motor Controller BRAKE
1 Channel 5V Relay Module
- VCC+: Connected to 5V (common net)
- VCC- (GND): Connected to GND (common net)
- IN:
- Relay 1: Connected to Arduino Mega 2560 A0
- Relay 2: Connected to Arduino Mega 2560 A1
- N.O.:
- Relay 1: Connected to ZS-X11H v2 350w Motor Controller DIR
- Relay 2: Connected to ZS-X11H v2 350w Motor Controller DIR
- COM: Connected to GND (common net)
ZS-X11H v2 350w Motor Controller
- 5v: Connected to 5V (common net)
- GND: Connected to GND (common net)
- 0-5v:
- Motor Controller 1: Connected to Arduino Mega 2560 D22
- Motor Controller 2: Connected to Arduino Mega 2560 D23
- DIR:
- Motor Controller 1: Connected to 1 Channel 5V Relay Module N.O.
- Motor Controller 2: Connected to 1 Channel 5V Relay Module N.O.
- BRAKE:
- Motor Controller 1: Connected to Arduino Mega 2560 D24
- Motor Controller 2: Connected to Arduino Mega 2560 D25
Code Documentation
/*
* Arduino Sketch for controlling multiple HC-SR04 sensors, motor controllers,
* and relay modules. The code reads distances from the sensors and implements
* simple obstacle avoidance logic to control the motors. Each sensor is
* connected to a dedicated pin on the Arduino Mega 2560.
*/
// Pin definitions for HC-SR04 sensors
const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 4;
const int echoPin2 = 5;
const int trigPin3 = 6;
const int echoPin3 = 7;
const int trigPin4 = 8;
const int echoPin4 = 9;
const int trigPin5 = 10;
const int echoPin5 = 11;
const int trigPin6 = 12;
const int echoPin6 = 13;
// Pin definitions for motor controllers
const int motorDirPin1 = 22;
const int motorDirPin2 = 23;
const int motorBrakePin1 = 24;
const int motorBrakePin2 = 25;
const int motorSpeedPin1 = 26;
const int motorSpeedPin2 = 27;
// Pin definitions for relay modules
const int relayPin1 = A0;
const int relayPin2 = A1;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize sensor pins
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
pinMode(trigPin5, OUTPUT);
pinMode(echoPin5, INPUT);
pinMode(trigPin6, OUTPUT);
pinMode(echoPin6, INPUT);
// Initialize motor controller pins
pinMode(motorDirPin1, OUTPUT);
pinMode(motorDirPin2, OUTPUT);
pinMode(motorBrakePin1, OUTPUT);
pinMode(motorBrakePin2, OUTPUT);
pinMode(motorSpeedPin1, OUTPUT);
pinMode(motorSpeedPin2, OUTPUT);
// Initialize relay pins
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
// Read distances from all sensors
int distances[6];
distances[0] = readDistance(trigPin1, echoPin1);
distances[1] = readDistance(trigPin2, echoPin2);
distances[2] = readDistance(trigPin3, echoPin3);
distances[3] = readDistance(trigPin4, echoPin4);
distances[4] = readDistance(trigPin5, echoPin5);
distances[5] = readDistance(trigPin6, echoPin6);
// Simple obstacle avoidance logic
if (distances[0] < 20 || distances[1] < 20 || distances[2] < 20 ||
distances[3] < 20 || distances[4] < 20 || distances[5] < 20) {
// Obstacle detected, turn left or right
if (distances[0] < 20 || distances[1] < 20 || distances[2] < 20) {
turnRight();
} else {
turnLeft();
}
} else {
// No obstacle, move forward
moveForward();
}
delay(100); // Delay for stability
}
int readDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
void stopMotors() {
digitalWrite(motorBrakePin1, HIGH);
digitalWrite(motorBrakePin2, HIGH);
analogWrite(motorSpeedPin1, 0);
analogWrite(motorSpeedPin2, 0);
}
void moveForward() {
digitalWrite(motorDirPin1, HIGH);
digitalWrite(motorDirPin2, HIGH);
digitalWrite(motorBrakePin1, LOW);
digitalWrite(motorBrakePin2, LOW);
analogWrite(motor