If you are building an obstacle-avoiding robot using four DC motors, an ultrasonic sensor, and a servo motor for directional scanning. Below are the complete details including components, wiring, and setup.
β Components List
- Arduino Uno
- Motor Driver Shield HW-130
- DC Motors
- Ultrasonic Sensor (HC-SR04)
- Servo Motor
- Robot Chassis with Wheels
- Power Supply (Battery Pack or Li-ion Battery)
- Jumper Wires
π Wiring Connections
π¦ Motor Connections (Motor Shield)
- DC Motor 1 β M1 terminals
- DC Motor 2 β M2 terminals
- DC Motor 3 β M3 terminals
- DC Motor 4 β M4 terminals
β οΈ Ensure correct polarity for consistent direction of movement.
π© Ultrasonic Sensor (HC-SR04)
HC-SR04 Pin | Connects To | Notes |
---|---|---|
VCC | 5V on Arduino (via shield) | |
GND | GND | |
Trig | A0 | As per #define trigPin A0 |
Echo | A1 | As per #define echoPin A1 |
Please note – Connect servo motor to pins given motor driver shield directly. Based on code signal pin is connected to pin 9.
π¨ Servo Motor
Servo Wire | Connects To | Notes |
---|---|---|
Red (VCC) | 5V | Can draw from Arduinoβs 5V if low-torque servo |
Brown/Black (GND) | GND | |
Orange/Yellow (Signal) | Pin 9 | As per #define servoPin 9 |
π οΈ Assembly Steps
- Mount motors onto the chassis and connect them to the Motor Shield terminals.
- Stack the Motor Shield on top of the Arduino Uno.
- Mount the servo to the front-top part of the chassis.
- Attach the ultrasonic sensor to the servo horn or bracket.
- Wire the ultrasonic sensor to pins A0 (Trig) and A1 (Echo).
- Connect the servo signal wire to Servo Pins given on Motor Driver.
- Upload the code to Arduino using the Arduino IDE.
- Power the system
π¦ Arduino Code
#include <AFMotor.h>
#include <Servo.h>
// Motors: M1βM4
AF_DCMotor motor1(1); // M1
AF_DCMotor motor2(2); // M2
AF_DCMotor motor3(3); // M3
AF_DCMotor motor4(4); // M4
// Ultrasonic pins
#define trigPin A0
#define echoPin A1
// Servo motor
Servo myServo;
#define servoPin 9 // Servo connected to D9
void setup() {
Serial.begin(9600);
// Set motor speeds (0β255)
motor1.setSpeed(150); // Increased from 100
motor2.setSpeed(150);
motor3.setSpeed(150);
motor4.setSpeed(150);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Attach and center the servo
myServo.attach(servoPin);
myServo.write(90);
delay(500);
Serial.println("Setup complete. Servo centered.");
}
void loop() {
int distance = getDistance();
Serial.print("Distance: ");
Serial.println(distance);
if (distance < 20) {
stopMotors();
delay(300);
Serial.println("Obstacle detected. Reversing...");
moveBackward();
delay(500);
stopMotors();
delay(300);
// Scan left
myServo.write(0);
delay(700);
int leftDist = getDistance();
Serial.print("Left distance: ");
Serial.println(leftDist);
// Scan right
myServo.write(180);
delay(700);
int rightDist = getDistance();
Serial.print("Right distance: ");
Serial.println(rightDist);
// Return to center
myServo.write(90);
delay(500);
// Decide direction to turn
if (leftDist > rightDist) {
Serial.println("Turning left...");
turnLeft();
} else {
Serial.println("Turning right...");
turnRight();
}
} else {
moveForward();
}
delay(100); // Main loop delay
}
// Get distance from ultrasonic sensor
int getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
// Move forward
void moveForward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
// Move backward
void moveBackward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
// Turn left with increased speed and duration
void turnLeft() {
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(1000); // Turn duration
stopMotors();
delay(200);
// Reset speed
motor1.setSpeed(150);
motor2.setSpeed(150);
motor3.setSpeed(150);
motor4.setSpeed(150);
}
// Turn right with increased speed and duration
void turnRight() {
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(1000); // Turn duration
stopMotors();
delay(200);
// Reset speed
motor1.setSpeed(150);
motor2.setSpeed(150);
motor3.setSpeed(150);
motor4.setSpeed(150);
}
// Stop all motors
void stopMotors() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
π οΈ Tips & Troubleshooting
- Use Serial Monitor at 9600 baud to debug distances.
- Servo jitter? Power it separately.
- Motors weak? Check battery level or increase PWM speed.
- Calibrate sensor: Sometimes adding delays/stabilization helps.