…turns out to be ludicrously easy. Just wire it up – Vcc at 5V, ground, and the trigger and echo pins to a couple of the Arduino’s digital pins:
and some nice chap has written an OO library for it. The code to read it turns out to be just this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <Ultrasonic.h> Ultrasonic *s; #define TRIG 10 #define ECHO 11 void setup(){ Serial.begin(9600); s = new Ultrasonic(TRIG,ECHO); } void loop(){ long timing = s->timing(); float distance = s->convert(timing,Ultrasonic::CM); Serial.println(distance); delay(1000); } |