Skip to content

Instantly share code, notes, and snippets.

@donly
Created December 27, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donly/a83a1e1f5b2c34c15aff2e84e2d86a3c to your computer and use it in GitHub Desktop.
Save donly/a83a1e1f5b2c34c15aff2e84e2d86a3c to your computer and use it in GitHub Desktop.
An Arduino project of HC-SR04+ demo
//HC-SR04+
const int trigpin = D4;
const int echopin = D3;
long duration;
int distance;
void setup(){
pinMode(trigpin,OUTPUT);
pinMode(echopin,INPUT);
Serial.begin(115200);
}
void loop(){
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance = duration*0.034/2;
Serial.println(distance);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment