• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
DarkBlueBit.com

DarkBlueBit.com

DIY projects with Arduino and Raspberry Pi

  • Arduino
  • Raspberry Pi
  • About
    • Contact
    • About Me
    • Amazon Affiliate Disclosure
    • Privacy Policy
You are here: Home / Arduino / Arduino Robot – Ultrasonic Sensor

Arduino Robot – Ultrasonic Sensor

The goal is to make the robot aware of obstacles in front of him, so he can change direction and avoid them. In the previous article we made the robot move – now we will give him some autonomy.

https://youtu.be/JhjXJYpjn6E

Watch video directly on YouTube

youtube-subscribe-button

Ultrasonic sensor

2pcs Hc-sr04 Ultrasonic Distance Measuring Sensor Module Good Compatible Arduino UNO Mega R3 Mega2560 Duemilanove Nano Robot HC-SR04 is a circuit able to measure distance to objects up to 4 meters using ultrasonic waves. It sends a ping (like a submarine) and measures the time (in microseconds) between sending and receiving anything back. This time is then divided by 2 as the wave travels back and forth. And then divide by 29 to get a distance in centimeters (or 74 for inches), because sound travels 29.4µs per centimeter (340 m/s). The sensor is very accurate with ~3 mm tolerance and easy to integrate with Arduino.

Circuit

robot-ultrasonic_bb

Code

The code below contains only an “ultrasonic” extension to DC motor control using an H-Bridge from the previous article. When the robot detects an obstacle in front of it, he turns around (random degree) and continues moving forward. This functionality could be easily extended to keep turning and detecting obstacles at the same time – so the robot would not turn randomly, but start moving forward only when no object is detected.

int EchoPin = 2; // connect Pin 2 to Echo at HC-SR04
int TrigPin = 3; // connect Pin 3 to Trig at HC-SR04
unsigned long Time_Echo_us = 0;
unsigned long Len_cm  = 0;

void setup() {
// Set EchoPin as input, to receive measure result   pinMode(EchoPin, INPUT);
// Set TrigPin as output, to send a pulse to start   pinMode(TrigPin, OUTPUT);       delay(5000); } void loop() {   if (motorsInStandby) return;  // robot is in standby mode     // Drive both motors forward   motorDrive(leftMotor, 1, 128);   motorDrive(rightMotor, 1, 128);
// begin to send a high pulse to begin measuring   digitalWrite(TrigPin, HIGH);             
// set this high pulse width as 50us   delayMicroseconds(50);                   
// end this high pulse   digitalWrite(TrigPin, LOW);              
// calculate the pulse width at EchoPin   Time_Echo_us = pulseIn(EchoPin, HIGH);   
// a valid pulse width should be between (1, 60000)   if ((Time_Echo_us < 60000) && (Time_Echo_us > 1)) {  
// calculate the distance in cm     Len_cm = (Time_Echo_us / 2) / 29;           if (Len_cm < 10) {       // very close object (<10 cm), stop the robot (standby)       motorStop(leftMotor);       motorStop(rightMotor);       motorsInStandby = true;     } else if (Len_cm < 40) {       // turn around       motorDrive(leftMotor, 1, 128);       motorDrive(rightMotor, 0, 128);       delay(random(220, 800));     }   } }

Category: Arduino

Disclosure

Articles on this website contain paid links leading to Amazon.com.

As an Amazon Associate I earn from qualifying purchases.

Read more

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

15 − 3 =

About

About Me

Contact

Privacy Policy

Amazon Affiliate Disclosure

Social Profiles

  • Facebook
  • Twitter
  • YouTube

Copyright © 2023 · DarkBlueBit.com · All Rights Reserved · A redistribution or reproduction of the contents in any form is prohibited except with our explicit written permission.