Tuesday 23 June 2015

Tutorial: How to work with the Ultasonic Distance Sensor HC-SR04

Hello Guys! On this tutorial We're going to learn how to use  the Ultasonic Distance Sensor HC-SR04, onde of the most versatile sensors available. You can use it in several fields, from robotics to industry and much more.

Material Used

A Garagino Basic Kit

An Arduino Uno

A 3mm Red LED

A 3mm Green LED

A 3mm yellow LED

Four 330R Resistor

Jumper wires

Library Download

In order to reproduce this tutorial you must download the library used, the download can be done by clicking  here

1. Introduction

This sensor operates with a simple, but clever, logic. The device sends a wave that reflects when reaches some object, this reflected wave then reaches the sensor's receptor (Echo). As we know the speed that the sound travels we can measure the distance from the sensor to the object. It operates with a range from 2 cm to 4 m.

2. Operation

This tutorial presents two examples, the first one using Arduino Uno where the systems is set to work as a electronic measuring tape. And the second, using Garagino where there's a system with 3 different states, one for dangerous distance (too close), second warning distance and the last one secure distance.

2.1. Electronic Measuring Tape

This circuit is very simple to assemble it's just connect the 4 wires from Arduino to the sensor. Two of these wires are supply wires and the other two are the signal wires that follow the sketch code. Here we're using the pin 12 to the Trig and 13 to the Echo. This system will operate as an electronic measuring tape. Thus, you can measure any distance from an object to the sensor.

2.2. Distance Sensor (3 state System)

In this circuit we built a 3-state system. When the distance from an object to the sensor is less than or equal to 10 cm the system turns on the red LED e makes a characteristic noise to tell us that we're within a dangerous distance.

When the distance is between 11 cm and 25 cm, it turns on the yellow LED, turns off the other two LEDs and makes a different sound from the previous one, telling us that we're within a warning (alert) distance.

And finally to distances higher than or equal to 26 cm, it turns on the Green LED, turns off the other two (no sound in this case) to tell us that the distance is secure.

All these values can be modified in order to fit to your project.

3. Sketch

3.1. Arduino

#include "Ultrasonic.h"      Ultrasonic ultrasonic(12,13);
void setup() {

Serial.begin(9600);

}

void loop()
{
Serial.print(ultrasonic.Ranging(CM));
Serial.println("cm");
delay(1000);

}


3.2. Garagino
      #include "Ultrasonic.h"      Ultrasonic ultrasonic(12,13);  long ultra;
int ledPin_5=5;
int ledPin_6=6;
int ledPin_7=7;
int buzzer=9;
void setup() {
pinMode (ledPin_5,OUTPUT);
pinMode (ledPin_6,OUTPUT);
pinMode (ledPin_7,OUTPUT);
pinMode(buzzer,OUTPUT);
Serial.begin(9600);

}

void loop()
{
Serial.print(ultrasonic.Ranging(CM));
Serial.println("cm ");
delay(100);
ultra=(ultrasonic.Ranging(CM));
if(ultra<=10)
{
digitalWrite(ledPin_5,HIGH);
digitalWrite(ledPin_6,LOW);
digitalWrite(ledPin_7,LOW);
tone(buzzer,1500);
delay(250);
noTone(buzzer);
delay(250);
tone(buzzer,1500);
delay(250);

}
if(11<ultra&&ultra<=25)
{
digitalWrite(ledPin_5,LOW);
digitalWrite(ledPin_6,HIGH);
digitalWrite(ledPin_7,LOW);
tone(buzzer,1200);
delay(500);

;
}
if(ultra>=26)
{
digitalWrite(ledPin_5,LOW);
digitalWrite(ledPin_6,LOW);
digitalWrite(ledPin_7,HIGH);
noTone(buzzer);
delay(500);
}

}


4. References:


http://ift.tt/SzTgsD
http://ift.tt/KupOQH
http://ift.tt/1Rtvc2G








from electronic for robot http://ift.tt/1K8vrzB

0 comments:

Post a Comment