Off-The-Shelf Hacker: Repurpose Your DIY Projects

As I mentioned in my previous Off-The-Shelf Hacker columns, I’m a big fan of modularizing my projects so I can mix and match parts as needed. Last Christmas my daughter gave me a little 1.8” color TFT display. It took an hour or so to get it working with an Arduino. Since then the little screen has seen service in my Steampunk conference badge series of prototypes, using a plain-Jane Arduino, the Yun and even the Raspberry Pi.
A while back, I took delivery of an HRC-SR04 ultrasonic range finder module from Amazon. As you might expect, it was only a matter of time before the range finder found itself hooked up to the 1.8” Arduino/TFT screen combination, in some kind of a portable format. Putting a quick proof-of-concept together was pretty easy. I just pirated the Arduino Pro-Mini, battery and nano-regulator module from version 1 of the Steampunk badge, configured the software and in a short time was measuring distances with sound.
Let’s see what it takes to get the HC-SR04 reading distances and outputting those values to the TFT display. Next week, we’ll look at how you can use and package this technology into a project.
Repurpose Your Projects
The basic Arduino Pro-Mini can serve as the base for a lot of interesting projects.
Take a look at the following schematic. I removed the DS18B20 digital temperature sensor and red LED features (from the schematic), then replaced them with the HC-SR04 ultrasonic range finder. Adding the wires and sensor took all of 15 minutes.
Otherwise, the schematic is the same as the old badge project.
That’s the beauty of module design and using microcontrollers in your projects. In this case, the Arduino Pro-Mini and TFT display combo can do multiple jobs, simply by hooking up different sensors and output devices, then modding the code.
Speaking of the code, reading an ultrasonic sensor isn’t overly complicated either.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// ------------------------------------------------------------ // Example NewPing library sketch that does a ping about // 20 times per second. // ------------------------------------------------------------ #include <NewPing.h> #include <SPI.h> #include <TFT.h> #define TRIGGER_PIN 6 #define ECHO_PIN 5 #define MAX_DISTANCE 450 // Maximum distance (in centimeters) #define sd_cs 4 // define tft display pins #define lcd_cs 10 #define dc 8 #define rst 9 TFT TFTscreen = TFT(lcd_cs, dc, rst); //init tft display char distanceVal[4]; //char string - sonar-to-text conversion NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //initialize sonar sensor void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud TFTscreen.begin(); TFTscreen.background(0,0,0); TFTscreen.setTextSize(2); } void loop() { String distance = String(sonar.ping_in()); //send ping (in.) distance.toCharArray(distanceVal,4); //convert int to char Serial.print(distance); //diagnostics to serial monitor Serial.println(" inches"); TFTscreen.stroke(255,255,255); //set text to white TFTscreen.text(distanceVal,10,50); //print distance @ 10,50 TFTscreen.text(" inches",10,50); delay(200); TFTscreen.stroke(0,0,0); //set text to black TFTscreen.text(distanceVal,10,50); //erase old text TFTscreen.text(" inches",10,50); } |
I used the NewPing Arduino library for this project.
First, we go through the usual initialization steps of defining pins, setting up variables and initializing the ultrasonic sensor and serial communications. I typically like to include serial output when developing code, for diagnostic purposes. You can always drop the println statements and such after everything is running right. Keep in mind also, that most Arduinos have about 32 KB of program space. Naturally, one space saving technique is to eliminate any extra, non-essential serial text producing code.
Finally, the main loop reads the ultrasonic sensor, does a conversion from its integer output to a character string and sends it to the TFT display. Using a black background, the distance and “inches” are printed on the screen, at the location (10, 50). The same string is then printed in black, to erase the white text, after which the cycle is repeated. It’s necessary to erase the text with black text, so the values don’t pile up on top of each other, eventually resulting in just white squares on the screen. It’s kind of brute-force, although it works. Use a minimum of 29 milliseconds between pings to make sure you get a good reading. I set the delay at 200 because when smaller values are used, the numbers on the screen blink increasingly faster and it becomes annoying.
You’ll notice, in the opening picture above, that the device is running on 4 AAA batteries. That’s a carry over from the Steampunk badge and will work fine in a wearable project. Make sure the switch is turned off when the USB-to-Serial cable it hooked up for programming. I also like to take a battery out of the holder, just in case the switch is inadvertently left in the on position or when stuffing the device in my computer bag.
Yes, I know, the Dallas DS18B20 is still hooked up in the picture. As my story deadline loomed I chose to sideline that “feature” for another time. Off-the-shelf hackers always dream about what’s next, but it’s good to keep potential project creep in check, so as not to have undesirable downstream workflow effects.
Wrap Up
Being able to measure from 1 inch to about 10 feet has real application in the physical computing World. Wiring it up and putting the code together is pretty straightforward as well.
Next week, I’ll cover a few interesting applications I’ve found around the Web, such as, how to increase the range with a reflector and how to find the position of your garage door, using a sonar sensor. We’ll also talk about ways to incorporate sonar in a wearable.
Until then, may all manner of cool physical computing hardware materialize in your Christmas stockings.