© 2024 EasyEDA Some rights reserved ISO/IEC
1.Easy to use and quick to get started
2.The process supports design scales of 300 devices or 1000 pads
3.Supports simple circuit simulation
4.For students, teachers, creators
1.Brand new interactions and interfaces
2.Smooth support for design sizes of over 5,000 devices or 10,000 pads
3.More rigorous design constraints, more standardized processes
4.For enterprises, more professional users
STD UltraSonic Distance Measurement HC-SR04
Mode: Editors' pick
Cloned from UltraSonic Distance Measurement HC-SR04
#include "NewPing.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define TRIGGER_PIN 5
#define ECHO_PIN 4
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
//Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.println("UltraSonic Distance Measurement");
Serial.println("easyeda.com easyeda.com");
}//--(end setup )---
void loop()
{
delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
DistanceIn = sonar.ping_in();
Serial.print("Ping: ");
Serial.print(DistanceIn); // Convert ping time to distance and print result
// (0 = outside set distance range, no ping echo)
Serial.print(" in ");
delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
DistanceCm = sonar.ping_cm();
Serial.print("Ping: ");
Serial.print(DistanceCm);
Serial.println(" cm");
}
After downloading this to your Arduino, open the Serial Monitor and you should see output like this:
![HC-SR04][3]
NewPing Library:http://playground.arduino.cc/Code/NewPing#Download
[1]: /editor/20151230/5683c87a711c8.JPG
[2]: /editor/20151230/5683c5b8edd7a.JPG
[3]: /editor/20151230/5683c7132d860.jpgID | Name | Designator | Footprint | Quantity |
---|---|---|---|---|
1 | HC_SR04 | ULTRASONIC1 | DIP | 1 |
2 | New Schematic Lib | U1,U2 | DIP | 2 |
3 | Arduino MEGA 2560 | U3 | DIP | 1 |
Unfold
Loading...
Do you need to add this project to the album?