Sunday, June 20, 2021

Project 7: Arduino Radar using Ultra Sonic HC-SR04 Sensor

 Project 7: Arduino Radar using Ultra Sonic HC-SR04 Sensor

Main Ideas:

Introduction to the new electronic board Ultra Sonic HC-SR04 Sensor and physical theory behind it which depend on measuring the time between sending ultra-sonic signal and receiving echo it hit an object, knowing speed of sound in air (340 m/s), we can calculate the distance (distance = speed * time) but in our case the distance = (speed * time)/2  divided by two because time is for sending signal and time receiving the echo. This will be used later in Robot project as small radar system to help Robot to find front obstacle and measure the distance to take the correct actions. Introduction to new programming concept of adding library to Arduino IED to help us in distance calculation.


Project Description

We will use the Ultra Sonic HC-SR04 Sensor to create small radar to detect objects.  We will use two methods to find the distance one by send signal our self as plus width  10 microsecond and then read duration based on the echo using pulseIn function the calculate the distance. The second code we will import the Ultrasonic.h library which have ready function can do everything for us and find the distance either in cm or inch.





Circuit

  • HC-SR04 Sensor
  • Arduino UNO

Code1 (using pulseIn function)

// connect to trigger Pin in HC-SR04 Sensor   

const int trigpin= 12;  

// connect to echo Pin in HC-SR04 Sensor  

const int echopin= 13;  

long duration;  

int distance;  

void setup(){

  // put your setup code here, to run once:  

  pinMode(trigpin,OUTPUT);  

  pinMode(echopin,INPUT);  

  Serial.begin(9600);  

}  

void loop(){ 

  // put your main code here, to run repeatedly:

  digitalWrite(trigpin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigpin,HIGH);  

  delayMicroseconds(10);  

  digitalWrite(trigpin,LOW);  

  duration=pulseIn(echopin,HIGH);  

  distance = duration*0.034/2;  

  Serial.println(distance);  

  delay(100);

}  


 Code2 (using Ultrasonic.h library)

#include <Ultrasonic.h>

//Ultrasonic ultrasonic(12, 13); // (Trig PIN,Echo PIN)

Ultrasonic ultrasonic(12, 13, 10000UL);

void setup() {

  Serial.begin(9600);

}

 

void loop()

{

  scan ();

  delay(100);

}

void scan ()

{

  Serial.print(ultrasonic.read(CM)); // CM or INC

  Serial.println(" cm" );

  delay(100);

}

No comments:

Post a Comment