- An Arduino library used to work with ultrasonic sensors in an easy way for measuring distance.
- Works with the HC-SR04 ultrasonic sensor in the 3 Pin Mode/4 Pin Mode, the Ping))) ultrasonic sensor in the 3 Pin Mode and the DHT temperature/humidity sensors for getting precise distance values.
- You will need to use at least Arduino IDE 1.0.0 or newer version.
-
An Arduino compatible board (e.g. Arduino UNO): Arduino UNO / Arduino Mega
-
A HC-SR04 ultrasonic sensor / A Ping))) ultrasonic sensor: HC-SR04
-
(Optional - For aqqurate distance values) A DHT temperature/humidity sensor (e.g. DHT 11): DHT11 / DHT22
-
Some male to female wires or a breadboard with male to male wires: Wires / Breadboard
-
Or you can just use an Arduino compatible kit instead of buying the above components (I personally recommend the ones from Elegoo): Arduino compatible kits / ELEGOO Mega R3 kit / Official Arduino Starter Kit
- Include the library so you can use it:
#include <EasyUltrasonic.h>
- Create an EasyUltrasonic object:
EasyUltrasonic ultrasonic;
- Functions:
attach(uint8_t trigPin, uint8_t echoPin, float minDistance = 2, float maxDistance = 400); // Sets the sensor pins as output/input (The default values for the minDistance and maxDistance parameters correspond to the HC-SR04 ultrasonic sensor distance range)
getDistanceCM(); // Returns the calculated distance in centimeters
getDistanceIN(); // Returns the calculated distance in inches
getPreciseDistanceCM(float temp, float hum); // Calculates an accurate distance by knowing the temperature and humidity of the environment and returns the calculated distance in centimeters
getPreciseDistanceIN(float temp, float hum); // Calculates an accurate distance by knowing the temperature and humidity of the environment and returns the calculated distance in inches
convertToCM(float distanceValueIN); // Returns the converted value from inches to centimeters
convertToIN(float distaceValueCM); // Returns the converted value from centimeters to inches
Check out the examples folder in the EasyUltrasonic repository to understand more.
! If you want to use the HC-SR04 ultrasonic sensor in the 3 Pin Mode you will have to short the trig pin and the echo pin together so you have only one signal pin !
! If you want to use one of the ultrasonic sensors in the 3 Pin Mode, when you call the attach() function you will need to set the trigPin and the echoPin parameters as the same value !
// !!! If you are using the Ping))) ultrasonic sensor, when you call the attach() function you should set the minDistance and maxDistance parameters to the corresponding values by knowing that the Ping))) sensor has a range between 3cm and 300cm:
attach(TRIGPIN, ECHOPIN, 3, 300);
-
Calculating normal distance with the HC-SR04 ultrasonic sensor in the 3 Pin Mode and an Arduino UNO board:
-
Calculating precise distance with the HC-SR04 ultrasonic sensor in the 3 Pin Mode, a DHT temperature/humidity sensor and an Arduino UNO board:
-
Calculating normal distance with the Ping))) ultrasonic sensor in the 3 Pin Mode and an Arduino UNO board:
-
Calculating precise distance with the Ping))) ultrasonic sensor in the 3 Pin Mode, a DHT temperature/humidity sensor and an Arduino UNO board:
-
Calculating normal distance with the HC-SR04 ultrasonic sensor in the 4 Pin Mode and an Arduino UNO board:
-
Calculating precise distance with the HC-SR04 ultrasonic sensor in the 4 Pin Mode, a DHT temperature/humidity sensor and an Arduino UNO board:
Check out the examples folder in the EasyUltrasonic repository to understand more.
- Spulber George-Marian [email protected]