Skip to content

Instantly share code, notes, and snippets.

@maeishoj
Created August 18, 2016 12:55
Show Gist options
  • Save maeishoj/cdd1e931105c21461ab68d228ca59bb2 to your computer and use it in GitHub Desktop.
Save maeishoj/cdd1e931105c21461ab68d228ca59bb2 to your computer and use it in GitHub Desktop.
Red Ultrasonic 5 PIN
#define TRIG A2
#define ECHO A3
void setup() {
Serial.begin (9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
int duration, cm;
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
cm = duration / 58;
Serial.print(cm);
Serial.println(" cm");
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment