Building an ultrasonic sensor backpack
Note: This is a bit of a scratch to see what's involved in getting the ping sensor to work as an I2C backpack.
- Jeff Hoeffs http://github.com/soundanalogous for guidance on using interrupts to do the time measurement on the ATTINY and https://gist.github.com/soundanalogous/e94c6ebbc457b98b8383
- http://github.com/rwaldron for support of the backpack idea and keeping me moving. ;)
I did this on a Trinket as that's what I had to hand to make programming a bit faster. If you use a trinket you will need the custom arduino. Check out the Adafruit Trinket info https://learn.adafruit.com/introducing-trinket/introduction
You also need a custom form of the TinyWireS library that manages sending multiple registers on one read request (the default implementation doesn't do this and locks up the buffer). This can be had here: https://github.com/Godziluu/TinyWire/ Do the usual thing and install into libraries folder of your sketch directory.
If you want to debug then you'll need a special form of Software serial - called http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip which does what you'd expect and only uses a single pin (handy on an ATTINY).
ping.ino can work on both an ATTiny85 and standard Arduino. It sets up as an I2C peripheral device. However, ATTINY can't use the wire library so has to use tiny wire. Read the code for the places to comment / uncomment to get this to work.
includes.h provides the #if hacks for arduino
ping.js A hack of the existing ping.js library in J5 using that as the base. Removed things that were no longer necessary and migrated relevant calls to I2C rather than pin manipulation.
pingtest.js is simply a demo to get it spitting out the data events. Note that this is using a hardcoded port just to eliminate any confusion across devices (I was using an arduino at one point so was getting port conflicts - easier to be explicit).
- Arduino requires only standard firmata
- Wire up the Trinket / ATTINY85 so that the ping trigger / echo is on pin 4.
- Wire up VCC and GND, then SDA and SCL to the arduino master.
Power everything up then run your sketch.
npm install johnny-five node ping-test.js
You should get an output of distance in CM.
- Lag issues have been resolved and were due to pulsein blocking
- Speed is viable at 100msec pin intervals on an ATTINY85 no problems.
- Tool chain is very clutzy due to issues with inotool not understanding attiny
- Ranging is accurate (to the level of accuracy of the actual sensor at least).
Some kind of issue with I2C whereby if the JS application is stopped then restarted but the Backpack isn't reset then Firmata times out and won't connect.
Track down this lag thing Fix issue with timing delay causing inflated timings.
- Fix issue with the firmata timeout.
- Get ATTINY using avr-gcc etc so no arduino deps at all
- clean up the schmozzle that is the inotool build chain with attiny
- Figure out how this comes back into Johnny-five as backpack with rwaldron