Friday, June 18, 2021

Ultra Sonic HC-SR04 for Arduino

 Ultra Sonic HC-SR04 for Arduino

The Ultra Sonic HC-SR04 emits ultrasound at 40,000Hz that travels in the air. If there is an object or obstacle in its path, then it collides and bounces back to the Ultra Sonic module.

The formula distance = speed*time is used to calculate the distance.

Suppose, an object is placed at a distance of 10 cm away from the sensor, the speed of sound in air is 340 m/s or 0.034 cm/µs. It means the sound wave needs to travel in 294 µs. But the Echo pin double the distance (forward and bounce backward distance). So, to get the distance in cm multiply the received travel time value with echo pin by 0.034 and divide it by 2.

The distance between Ultra Sonic HC-SR04 and an object is:





Digital circuit diagram

Ultrasonic Sensor HC-SR04       Arduino UNO

VCC --------------------------------> 5v

Trig --------------------------------> Pin 8

Echo --------------------------------> Pin 7

GND --------------------------------> GND




pulseIn() function [Advanced I/O]

Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.

Syntax

pulseIn(pin, value)

pulseIn(pin, value, timeout)

Parameters

pin: the number of the Arduino pin on which you want to read the pulse. Allowed data types: int.

value: type of pulse to read: either HIGH or LOW. Allowed data types: int.

timeout (optional): the number of microseconds to wait for the pulse to start; default is one second. Allowed data types: unsigned long.

Returns

The length of the pulse (in microseconds) or 0 if no pulse started before the timeout. Data type: unsigned long.

Arduino Code:

#include <Mouse.h>  

  

const int trigpin= 8;  

const int echopin= 7;  

long duration;  

int distance;  

void setup(){  

  pinMode(trigpin,OUTPUT);  

  pinMode(echopin,INPUT);  

  Serial.begin(9600);  

}  

  

void loop(){  

  digitalWrite(trigpin,HIGH);  

  delayMicroseconds(10);  

  digitalWrite(trigpin,LOW);  

  duration=pulseIn(echopin,HIGH);  

  distance = duration*0.034/2;  

  Serial.println(distance);  

}  



Ultrasonic library

Minimalist library for ultrasound module to Arduino

Compatible with HC-SR04Ping and Seed SEN136B5B (from Seed Studio)

Work with ultrasonic modules is fairly simple, but can be even more practical if you abstract the control of some features. This library aims to resource efficiency and to simplify access to data.

Where necessary use the ultrasonic module HC-SR04 (one of the most common on the market), Ping))) and/or Seeed SEN136B5B (from Seeed Studio), there are hundreds of libraries that purport to provide the most diverse roles for the user, however, the vast majority of the time, we just need to find out the distance and is that's what does this library.

This library is minimalist, reduces code execution, validation and unnecessary use of global variables, prioritizing smaller data types.

Wiring:



It is very easy to connect an ultrasound module to the Arduino. For example, if you are using HC-SR04, connect the trigger and echo pin module on pin 12 and 13 of the Arduino, respectively. As in the above picture.

If you are using a module with three pins (like Ping))) or Seeed SEN136B5B), you can conect the sig pin module on pin 13 of the Arduino.

How to use:

The idea is to provide a simpler environment possible. To do this, simply follow the steps:

Installing

First you need to import the library so that the IDE recognizes it. The simplest way is importing through the IDE itself:

Click in Sketch > Include Library > Manage Libraries...;

In the search field type: ultrasonic;

In the list, look for Ultrasonic by Erick Simões;

Importing on code

To import the library to your code, just write at the beginning of the code #include <Ultrasonic.h> or, in the Arduino IDE, click in Sketch > Include Library > Ultrasonic (will have the same result).

Starting (the most exciting part)

Now is simply create a variable of type Ultrasonic passing as parameters two values representing, respectively, the Trig (emitter) and Echo (receiver) pins. Like this:

Ultrasonic ultrasonic(12, 13);

If you are using a module with three pins (like Ping))) or Seeed SEN136B5B), pass as a parameter only the signal pin. Like this:

Ultrasonic ultrasonic(13);

Discovering the distance

Having initialized a variable, you can run hers from the method that returns the distance read by module Ultrasonic: read():

ultrasonic.read()

Only this?

Yes. That's it. By default, the value returned from the function read() is the distance in centimeters.

Seriously?

You can still do a little more determining the unit of measurement that will be returned (centimeters (CM) or inches (INC)).

ultrasonic.read()    // distance in CM

ultrasonic.read(CM)  // distance in CM

ultrasonic.read(INC) // distance in INC

You can also use more than one ultrasound module:

ultrasonic ultrasound1(12, 13);

ultrasonic ultrasound2(10, 11);

ultrasonic ultrasound3(5);

Timeouts

If there is no object in range, the library will lock-up as it waits for the return pulse. You can change how long to wait by setting a timeout (in microseconds) in the constructor:

Ultrasonic ultrasonic(12, 13, 40000UL);

Or during runtime:

ultrasonic.setTimeout(40000UL);

Using a 40ms timeout should give you a maximum range of approximately 6.8m. You may need to adjust this parameter.


No comments:

Post a Comment