0% found this document useful (0 votes)
26 views7 pages

Obstacle Detection Ultrasonic Sensor Program

The document describes the components and connection steps to build an obstacle detection robot using an Arduino, motor driver shield, ultrasonic sensor, servo motor, and switch. The robot moves forward when the switch is pressed, and stops and turns if the ultrasonic sensor detects an obstacle within 20cm. The included code explains how to initialize the components, move the robot forward, stop it, turn the servo motor, and measure distance with the ultrasonic sensor.

Uploaded by

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

Obstacle Detection Ultrasonic Sensor Program

The document describes the components and connection steps to build an obstacle detection robot using an Arduino, motor driver shield, ultrasonic sensor, servo motor, and switch. The robot moves forward when the switch is pressed, and stops and turns if the ultrasonic sensor detects an obstacle within 20cm. The included code explains how to initialize the components, move the robot forward, stop it, turn the servo motor, and measure distance with the ultrasonic sensor.

Uploaded by

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

Obstacle Detection Ultrasonic Sensor Program

Components Needed:
1. Arduino Board
2. Motor Driver Shield (with appropriate motor connections)
3. Servo Motor
4. Ultrasonic Sensor
5. Switch
6. Jumper Wires

Connection Steps:
Step 1: Motor Driver Shield
1. Connect the motor driver shield to the Arduino board.

Step 2: Motor Connections


2. Connect the motors to the motor driver shield's output pins:
- Motor 1: Connect to `motor1Pin1` and `motor1Pin2`
- Motor 2: Connect to `motor2Pin1` and `motor2Pin2`
- Motor 3: Connect to `motor3Pin1` and `motor3Pin2`
- Motor 4: Connect to `motor4Pin1` and `motor4Pin2`

Step 3: Ultrasonic Sensor


3. Connect the ultrasonic sensor to the Arduino:
- Connect the sensor's `trig` pin to `trigPin` (Pin 10 on the Arduino).
- Connect the sensor's `echo` pin to `echoPin` (Pin 11 on the Arduino).
- Connect the sensor's `VCC` pin to a 5V output on the Arduino.
- Connect the sensor's `GND` pin to the Arduino's GND.

Step 4: Servo Motor


4. Connect the servo motor to the Arduino:
- Connect the servo's control wire to Pin 13 on the Arduino.
- Connect the servo's power wire to a 5V output on the Arduino.
- Connect the servo's ground wire to the Arduino's GND.

Step 5: Switch
5. Connect the switch to the Arduino:
- Connect one end of the switch to the `switchPin` (Pin 12 on the Arduino).
- Connect the other end of the switch to the Arduino's GND.
Step 6: Power
6. Power the Arduino through USB or an external power source.

Step 7: Upload Program


7. Upload the provided Arduino program to your Arduino board.

Step 8: Test
8. Power on the circuit and press the switch to see if the robot moves forward.
- The ultrasonic sensor will detect obstacles, and if the distance is less than 20 cm,
the robot will stop and turn.

Additional Notes:
- Ensure that the motors, servo, and ultrasonic sensor are properly connected, and their
power requirements are met.
- Double-check the pin assignments in the program to match your hardware
connections.
- Open the Serial Monitor in the Arduino IDE to view distance readings for debugging.

Note: Ensure that you have the Arduino IDE installed on your computer, and you
should be able to compile and upload the code to your Arduino board without needing
any additional libraries

Code is given below, You can copy paste. Be Careful about connections.
// Include libraries
#include <Servo.h>

// Define pins for motor driver shield


#define motor1Pin1 2
#define motor1Pin2 3
#define motor2Pin1 4
#define motor2Pin2 5
#define motor3Pin1 6
#define motor3Pin2 7
#define motor4Pin1 8
#define motor4Pin2 9

// Define pins for ultrasonic sensor


#define trigPin 10
#define echoPin 11

// Define pins for switch


#define switchPin 12

// Define variables
int distance; // variable to store distance measured by ultrasonic sensor
int speed = 150; // variable to control speed of motors
Servo servo; // create servo object to control servo motor

void setup() {
// Initialize motor pins as output
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor3Pin1, OUTPUT);
pinMode(motor3Pin2, OUTPUT);
pinMode(motor4Pin1, OUTPUT);
pinMode(motor4Pin2, OUTPUT);

// Initialize switch pin as input


pinMode(switchPin, INPUT);

// Initialize servo pin


servo.attach(13);
// Initialize serial communication
Serial.begin(9600);
}

void loop() {
// Check if switch is pressed
if(digitalRead(switchPin) == HIGH) {
// Move forward
moveForward();

// Check distance using ultrasonic sensor


distance = getDistance();

// Check if obstacle is detected


if(distance < 20) {
// Stop and turn
stop();
turn();
}
}
else {
// Stop if switch is not pressed
stop();
}
}

// Function to move forward


void moveForward() {
// Set motor directions
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, HIGH);
digitalWrite(motor3Pin2, LOW);
digitalWrite(motor4Pin1, HIGH);
digitalWrite(motor4Pin2, LOW);

// Set motor speeds


analogWrite(motor1Pin1, speed);
analogWrite(motor2Pin1, speed);
analogWrite(motor3Pin1, speed);
analogWrite(motor4Pin1, speed);
}
// Function to stop
void stop() {
// Set all motor pins to LOW
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
digitalWrite(motor4Pin1, LOW);
digitalWrite(motor4Pin2, LOW);
}

// Function to turn
void turn() {
// Turn servo motor to 90 degrees
servo.write(90);

// Wait for 1 second


delay(1000);

// Turn servo motor back to 0 degrees


servo.write(0);
}

// Function to get distance using ultrasonic sensor


int getDistance() {
// Send pulse to trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure pulse duration on echo pin


long duration = pulseIn(echoPin, HIGH);

// Calculate distance in cm
int distance = duration * 0.034 / 2;

// Print distance to serial monitor


Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Return distance value


return distance;
}

// Code sourced from Arduino website for ultrasonic sensor and servo motor control.
If you’re Still Confused, Look below what we’ve done in the
code.

1. Servo Motor Initialization:


In the `setup` function, you're attaching the servo to pin 13. Make sure that your servo
is connected to pin 13 on your Arduino board. If it's connected to a different pin, adjust
the `servo.attach(13)` line accordingly.

2. Motor Speed Control:


In the `moveForward` function, you're using `analogWrite` to set the motor speeds.
However, you have set the directions using `digitalWrite` just before that. The
`analogWrite` should be used for speed control only on the PWM pins. If your motor
driver does not support PWM speed control, you might need to use a different
approach.

3. Ultrasonic Sensor Initialization:


You have defined `trigPin` and `echoPin`, but you haven't set their modes in the
`setup` function. You should set them as follows:
```cpp
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
```

4. Switch Pin Initialization:


You've set the `switchPin` as an input in the `setup` function, which is correct. Just
ensure that the switch is connected properly, and when pressed, it connects the
`switchPin` to the ground (LOW).

5. Serial Monitor Output:


You are printing the distance to the Serial Monitor, which is a good practice for
debugging. Just ensure that you have the Serial Monitor open on your computer to view
the distance readings.

6. Speed Variable:
You have a `speed` variable that controls the speed of your motors. You might want to
experiment with different speed values to find the optimal speed for your robot.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy