This library provides an easy-to-use interface for controlling Ultrasonic (HC-SR04) sensor.
- Download this repository as ZIP.
- Open the Arduino IDE.
- Go to
Sketch → Include Library → Add .ZIP Library...
. - Select the downloaded ZIP file and click
Open
. - The library is now installed.
- Include the library into your project via Arduino IDE
Sketch → Include Library → Ultrasonic
or just add the following line in your code:
#include <Ultrasonic.h>
- Then create an instance of the Ultrasonic:
Ultrasonic ultrasonic(trigger_pin, echo_pin);
Go to File → Examples → Ultrasonic → Ultrasonic
#include "Ultrasonic.h"
#define trigger_pin 5
#define echo_pin 18
Ultrasonic ultrasonic(trigger_pin, echo_pin);
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.print("Distance (cm): ");
Serial.println(ultrasonic.measureCentimeters());
delay(1000);
}
Method | Params | Description |
---|---|---|
measureCentimeters | none | measures distance in centimeters |
measureInchs | none | measures distance in inchs |
This project is licensed under the MIT license.