Ultrasonic Sensor(HC-SR04)
Measure Distance/Detect Any Motion
Based on my experience, this sensor need a good reflector to get better result. Reflector must be:
-Flat
-Hard
And size of the reflector must be bigger than this sensor size. It help sensor receive reflected signal better.
This how Ultrasonic sensor looks
VIDEO
i recommend watch this video on youtube for better quality:
https://www.youtube.com/watch?v=hCmmrtMHlVw&feature=youtu.be
https://www.youtube.com/watch?v=hCmmrtMHlVw&feature=youtu.be
Hardware Setup
This how to connect HC-SR04 Ultrasonic sensor to Arduino
How to measure the distance(cm) using Ultrasonic sensor using Arduino.
1. First of all you need to download the library for this sensor first.
You can download here;
https://drive.google.com/open?id=0B3c_VPBO9Qq3eWIyNi1maWN6R1U
This library can help you to get more accurate measurement and also can make your program easier. But you need to add this library first before you can upload the program.
2. How to add library to Arduino
-download the library and unzip the zip item.
-copy & paste the unzip item to desktop
-open Arduino IDE and click on "include library", then click on "Add .Zip Library.."
-then select the folder that tou have move to dekstop.
-once you click open, you succesful to add the library to Arduino. You can check the library by click on Sketch > Inculde Library. Then you can see the folder "New Ping"
3. Now you ready to upload the program.
Here link to download the program:
or you can just copy and paste this code to your ide
#include <NewPing.h>
#define TRIGGER_PIN 12 //trigger pin kat arduino 12
#define ECHO_PIN 11 //echo pin kat arduino 11
#define MAX_DISTANCE 80 //set jarak maksimum
int led = 7;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
unsigned int pingSpeed = 50; //speed bacaan ping
unsigned long pingTimer;
void setup() {
Serial.begin(115200);
pingTimer = millis(); // Start now.
pinMode(led, OUTPUT);
}
void loop() {
if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set untuk ping seterusnya.
sonar.ping_timer(echoCheck); // Keluarkan ping seterusnya, call balik function echoCheck
}
int reading = (sonar.ping_result / US_ROUNDTRIP_CM); //tukar nama kepada bacaan
if(reading <= 21)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
}
void echoCheck() { //Check Timer2 kalau ada interrupt
if (sonar.check_timer()) { // check ping yang diterima setiap 24uS
Serial.print("Ping: ");
Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); //tukar bacaan US_ROUNDTRIP_CM kedalam centimeter
Serial.println("cm");
int reading = (sonar.ping_result / US_ROUNDTRIP_CM);
}
}
4. Sensor Reading
- To see the sensor reading you can press ctrl + shift + m or click on serial monitor icon at the top left.
- I've make a little modification to the code. You can see the distance in "cm" at serial monitor.
- I've make a little modification to the code. You can see the distance in "cm" at serial monitor.
5. Code explaination
#include <NewPing.h>
-Include the library. Tell arduino that youre using "NewPing" library.
#define TRIGGER_PIN 12 //trigger pin kat arduino 12
-use pin12 as trigger pin
#define ECHO_PIN 11 //echo pin kat arduino 11
-tell arduino pin11 is echo pin
#define MAX_DISTANCE 80 //set jarak maksimum
-set the maximum distance that youre need. I set it at 80cm
Serial.begin(115200);
-set baud rate at 115200. I use Arduino Mega 2560 so the baud rate is 115200. If youre using Arduino Uno set it at 9600.
pingTimer = millis(); // Start now.
-set timer in milisecond
pinMode(led, OUTPUT);
-set led pin as output. I use LED as output in this test.
if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set untuk ping seterusnya.
sonar.ping_timer(echoCheck); // Keluarkan ping seterusnya, call balik function echoCheck
}
-dont make any change at this if dont know what is this.
int reading = (sonar.ping_result / US_ROUNDTRIP_CM); //tukar nama kepada bacaan
if(reading <= 21)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
}
-if distance between sensor and reflector equal and less than 21cm, led is ON. If distance more than 22cm, led will turn OFF.
void echoCheck() { //Check Timer2 kalau ada interrupt
if (sonar.check_timer()) { // check ping yang diterima setiap 24uS
Serial.print("Ping: ");
Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); //tukar bacaan US_ROUNDTRIP_CM kedalam centimeter
Serial.println("cm");
int reading = (sonar.ping_result / US_ROUNDTRIP_CM);
}
}
-void to check the echo.
No comments:
Post a Comment