I suspect the world would be better if that percentage were even greater.
Using the Parallax PING))) ultrasonic distance sensor
I intended to play around with some of the NRF24L01 radio modules I have around, but my brain didn’t feel up to it after a day of debugging. So, instead i dusted off a Parallax PING))) sensor that I’ve had around for a long time. I thought it might be fun to see if I could use the OLED display to display the distance measured by the sensor. While watching Agents of Shield, I came up with the following tiny bit of code, demo’ed below.
Got my @ParallaxInc ping sensor working with the @SFE_Engineering Red Board #arduino and my little OLED dis… https://t.co/05PIETOWtx
— Mark VandeWettering (@brainwagon) March 25, 2015
[sourcecode lang=”cpp”]
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
// _
// ___ (_)__ ___ _
// / _ \/ / _ \/ _ `/
// / .__/_/_//_/\_, /
// /_/ /___/
//
// A simple rangefinder program using the Parallax PING sensor
// Writes display out to my tiny OLED display…
const int pingPin = 7 ;
char dbuf[10] ;
void
draw()
{
u8g.setDefaultBackgroundColor();
u8g.drawBox(0, 0, 128, 64);
u8g.setDefaultForegroundColor();
u8g.setFont(u8g_font_10x20);
u8g.drawStr(14, 42, dbuf);
u8g.drawStr(94, 42, "cm");
}
void
setup()
{
Serial.begin(9600);
}
void
loop()
{
long duration ;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // Make sure we drive the pin low…
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Pulse high for 5us
delayMicroseconds(5);
digitalWrite(pingPin, LOW); // Finish the pulse…
pinMode(pingPin, INPUT); // Get ready to read…
duration = pulseIn(pingPin, HIGH); // Wait for the echo pulse to drop back to low
// The speed of sound is 29 us per cm… j
float cm = duration / (29. * 2.) ;
dtostrf(cm, 8, 2, dbuf) ;
u8g.firstPage() ;
do {
draw() ;
} while (u8g.nextPage()) ;
}
[/sourcecode]
Addendum: I’ve been snapping short videos with the Vine app, and then posting them to Twitter and Facebook. Irritatingly, these don’t seem to actually play properly when I embed them back in my website. I’ll figure out a better workflow next time.
Addendum2: Goofing around a little, it appeared that this sensor worked out until about 70cm or so, and then snaps to 340cm. Not sure what that is about: the sensor should behave more accurately than that. I’ll experiment more tomorrow. I should also note that these Parallax sensors are fairly pricey: list price is almost $30. I like the Parallax guys a lot, but that’s pretty spendy. You can get much cheaper modules on Ebay, with costs as low as around $1.50. I haven’t got any of these, but they are worth trying, although various sources on the web suggest that you might get what you pay for. Try googling for “HC-SR04” for more information.
Comment from Mike Yancey
Time 3/25/2015 at 9:13 am
I use two of those in my garage as parking lights.
One is driven by an Arduino (minimalist, on breadboard), the other by an Adafruit Trinket (ATTINY85).
Three 10mm LEDs – Green when car approaches within range, Yellow, then Red, then RED when the car is ‘clear of the garage door’.
When the garage light goes off, a photo resistor tells the micro-controllers to not Ping))) any more!
Mike Y, KM5Z
Dallas, Texas