0% found this document useful (0 votes)
4 views13 pages

Robot Notes

This document provides a guide to building a smart robot using an Arduino Uno, ultrasonic sensor, and other components. The robot is designed to detect obstacles and navigate around them using specific coding instructions. It also includes wiring diagrams, code snippets, and tips for troubleshooting and enhancing the robot's functionality with a servo motor for better obstacle detection.

Uploaded by

wadzanai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Robot Notes

This document provides a guide to building a smart robot using an Arduino Uno, ultrasonic sensor, and other components. The robot is designed to detect obstacles and navigate around them using specific coding instructions. It also includes wiring diagrams, code snippets, and tips for troubleshooting and enhancing the robot's functionality with a servo motor for better obstacle detection.

Uploaded by

wadzanai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Let‛s Build a Smart Robot!

What You‛ll Need:

1. 1 x Arduino Uno
2. 1 x Ultrasonic Sensor
3. 1 x Servo Motor (Optional)
4. 2WD Robot Chassis
5. Jumper Wires
6. 2 X Li-ion 3.7V battery pack

What the Robot Will Do:

1. This robot will:


2. Drive forward like a champ
3. Detect obstacles (wall, shoes, your cat)
4. Stop, think for a second, then turn away
5. Try again in a new direction

Wiring Guide: Giving our Robot a Brain!

L298N Connect to
Arduino
IN1 Pin 7
IN2 Pin 6
IN3 Pin 5
IN4 Pin 4
ENA Pin 9
ENB Pin 3

Connect the motors to OUT1, OUT2, OUT3 and OUT4 terminals on the L298N motor
driver. Power the L298N driver with 6V-12V from the battery pack.

Ultrasonic Sensor: Our Robot‛s Googly Eyes

Ultrasonic Sensor Connect to


Pins Arduino
VCC 5V
GND GND
TRIG Pin 10
ECHO Pin 8

Open the Arduino IDE and upload the following code to the Arduino:

// Motor pins
#define IN1 7
#define IN2 6
#define IN3 5
#define IN4 4
#define ENA 9 // PWM - Left motor
#define ENB 3 // PWM - Right motor

// Ultrasonic pins
#define trigPin 10
#define echoPin 8
// Motor speed (0-255)
int motorSpeed = 180; // Adjust based on your robot's performance

// Obstacle detection distance


int safeDistance = 20; // cm

void setup () {
// Motor pins
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);

// Ultrasonic sensor pins


pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);

Serial.begin(9600);
}

void loop () {
int distance = getDistance ();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

if (distance < safeDistance) {


stopMotors ();
delay (200);
turnRight (); // You can switch to turnLeft () if your bot prefers it
delay (400);
} else {
moveForward ();
}
}

// Distance sensor logic


int getDistance () {
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;
}

// Movement functions
void moveForward () {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}
void turnRight () {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}

void turnLeft () {
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}

void stopMotors () {
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, LOW);
analogWrite (ENA, 0);
analogWrite (ENB, 0);
}

How the code works


moveForward (): tells both motors to go forward

turnRight (): left motor goes forward; right motor goes backward -> spin

turnLeft (): right motor forward, left motor backward -> spin the other way

stopMotors (): both motors stop -> time to think

Pro Tips:

• If your robot spins in circles, check your wiring.


• If it won‛t move, make sure the battery is providing enough power.
• Experiment with different motor speeds from 100 – 255. Don‛t be afraid to play
around with the code.
• Swap between turnLeft () and turnRight () to see what your robot likes best.

What You‛ll Learn

• How robot‛s sense the world


• How to use motors and sensors
• That coding is like giving your robot instructions – like teaching a puppy to sit.

So yes, our robot has a brain but it‛s not every time that it thinks with its head. Let‛s help
it out by adding a servo.

Upload this code to the Arduino if your robot acts like Mr Magoo and can‛t stop bumping
into things:

#include <Servo.h>
// Motor pins
#define IN1 7
#define IN2 6
#define IN3 5
#define IN4 4
#define ENA 9
#define ENB 3
// Ultrasonic sensor pins
#define trigPin 10
#define echoPin 8

// Servo pin
#define servoPin 11

Servo headServo;

int motorSpeed = 180;


int safeDistance = 20;

void setup () {
// Motor setup
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);

// Ultrasonic sensor setup


pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);

// Servo setup
headServo.attach(servoPin);
headServo.write(90); // Center position

Serial.begin(9600);
}

void loop () {
headServo.write(90); // Face forward
delay (300);
int distanceForward = getDistance ();

Serial.print("Forward Distance: ");


Serial.println(distanceForward);

if (distanceForward < safeDistance) {


stopMotors ();
delay (200);

// Look left and right


headServo.write(30);
delay (400);
int distanceLeft = getDistance ();

headServo.write(150);
delay (400);
int distanceRight = getDistance ();

// Return head to center


headServo.write(90);
delay (200);

// Decide direction
if (distanceLeft > distanceRight && distanceLeft > safeDistance) {
turnLeft ();
delay (500);
} else if (distanceRight > safeDistance) {
turnRight ();
delay (500);
} else {
// Stuck – back up and retry
moveBackward ();
delay (500);
}
} else {
moveForward ();
}
}

// --- Sensor Reading Function ---


int getDistance () {
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;
}

// --- Movement Functions ---


void moveForward () {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}

void moveBackward () {
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}

void turnRight () {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}

void turnLeft () {
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, motorSpeed);
analogWrite (ENB, motorSpeed);
}

void stopMotors () {
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, LOW);
analogWrite (ENA, 0);
analogWrite (ENB, 0);
}

What the Servo Will Do:

• The ultrasonic sensor will be mounted on the servo.


• The servo turns left, center, and right to check for obstacles in all directions.
• The robot will decide which way is safer before turning.

Servo Connect to Arduino


VCC 5V
GND GND
Signal Pin 11

N.B: The servo is a power-hungry component and connecting it directly to get power from
the Arduino might affect the robot function. You can opt to connect it‛s VCC and GND to
the L298N or provide clean power from an alternative power source like a breadboard
power supply.

You might also like