Creating a simple robot using motors and sensors is a great way to explore robotics and learn about the basics of control systems. In this example, we will build a basic obstacle-avoidance robot using two DC motors and an ultrasonic distance sensor. This robot will be programmed to move forward until it detects an obstacle in front of it, at which point it will stop and change direction to avoid the obstacle. Here’s how to create it:
Components you’ll need:
Arduino board (e.g., Arduino Uno)
Two DC motors with wheels
Motor driver module (e.g., L298N or L293D)
Ultrasonic distance sensor (HC-SR04)
Breadboard and jumper wires
Battery pack or power supply for the motors
Chassis or platform to build the robot
Screwdriver and mounting hardware
Circuit Connections:
Connect the two DC motors to the motor driver module. Typically, you’ll have two terminals for each motor (M1 and M2). Connect the motors as follows:
Motor 1: Connect one terminal to OUT1, and the other terminal to OUT2.
Motor 2: Connect one terminal to OUT3, and the other terminal to OUT4.
Connect the motor driver module to the Arduino:
Connect the module’s “Input 1” and “Input 2” pins to two digital pins on the Arduino (e.g., 5 and 6 for Motor 1).
Connect the module’s “Input 3” and “Input 4” pins to two other digital pins on the Arduino (e.g., 9 and 10 for Motor 2).
Connect the module’s “ENA” and “ENB” pins to two PWM-capable digital pins on the Arduino (e.g., 3 and 11).
Connect the ultrasonic distance sensor to the Arduino:
Connect the sensor’s “Trigger” pin to a digital pin on the Arduino (e.g., 7).
Connect the sensor’s “Echo” pin to another digital pin on the Arduino (e.g., 8).
Connect the sensor’s VCC and GND pins to 5V and GND on the Arduino, respectively.
Connect the power supply for the motors to the motor driver module.
Arduino Code:
Upload the following Arduino code to your Arduino board using the Arduino IDE:
#include
#define TRIGGER_PIN 7
#define ECHO_PIN 8
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int motor1Pin1 = 5;
int motor1Pin2 = 6;
int motor2Pin1 = 9;
int motor2Pin2 = 10;
int enablePin1 = 3;
int enablePin2 = 11;
void setup() {
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enablePin1, OUTPUT);
pinMode(enablePin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int distance = sonar.ping_cm();
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm”);
if (distance > 10) {
// Forward motion
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(enablePin1, 255); // Full speed
analogWrite(enablePin2, 255);
} else {
// Stop and turn right
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(enablePin1, 0); // Stop
analogWrite(enablePin2, 255); // Full speed
delay(1000); // Turn for 1 second
}
}
Assembly:
Assemble your robot chassis or platform, and attach the motors and wheels to it.
Mount the motor driver module, Arduino board, and ultrasonic sensor securely on the chassis.
Connect the motors to the wheels and power supply.
Make sure the ultrasonic sensor is facing forward and unobstructed.
Upload the provided Arduino code to your Arduino board.
Usage:
When you power on your robot, it will move forward until it detects an obstacle closer than 10 cm using the ultrasonic sensor. At that point, it will stop, turn right for one second, and then continue moving forward. This cycle will repeat as the robot encounters obstacles in its path.
This basic obstacle-avoidance robot is a starting point for more advanced robotics projects. You can further enhance it by adding more sensors (e.g., IR sensors for line following), improving the control algorithm, or implementing remote control capabilities.