fbpx
Arduino Lesson 19: Ultrasonic Sensor
Arduino Lesson 19

Ultrasonic Sensor

Find the distance to object with ultrasonic sensor

Components Required

Magicbit
Buy

 Introduction

An ultrasonic sensor is used to measure the distance to objects in front of the sensor by using ultrasonic waves. The human body doesn’t sensitive to this signal. Therefore, we can’t hear any sound when it is working.

Learning outcomes:

  • Using HC-SR04 ultrasonic sensor and getting outputs of distances
  • Apply Ultrasonic sensor in projects

Theory

Any kind of ultrasonic sensor works on same way. For measuring distance to object it uses ultrasonic waveform. The sensor have two parts. One is wave transmitter part and other one is receiving part. The transmitter part emits an ultrasonic wave and receives the reflected waveform back from the emitter. The time duration between transmit and receive is used to measure the distance. If the time duration is low then object is near. If the time duration is high the object is too far. Distance and the time duration is directly proportional parameters. Distance between object and the sensor can be determined by following equation.

Distance=(speed of ultrasound wave in air )*(time duration)/2

Speed of ultrasound wave in air is 340 meters per second. To measure the distance, we have to trigger the transmitter in certain time duration. If this time duration is very small, then it cant be measured. If this is too high it can cause to noises. Therefor it emits ultrasonic waves in small certain time durations. Then checks the receiver part(echo pin) until it detects.

Methodology

Connect the ultrasonic sensor module to Magicbit using a connector wire. Here, we connect the sensor module to the upper left (D32) connector on the Magicbit. Then connect the Magicbit to your pc and upload the following code. Now open the serial monitor. For good results, keep the sensor vertically and keep the object’s surface parallel to the sensor face.

https://www.researchgate.net/profile/Rakan_Bashir/publication/335140788/figure/fig4/AS:801160889892865@1568023049877/Work-principle-of-the-ultrasonic-sensor.pngFig :1

 Code

 

#include <NewPing.h>
#define TRIGGER_PIN  32
#define ECHO_PIN     32
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
Serial.begin(115200);
}

void loop() {
delay(50);
Serial.print("Ping: ");
Serial.print(sonar.ping_cm());
Serial.println("cm");
}
Related Posts
Leave a Reply