Note: This is a bit of a scratch to see what's involved in getting the ping sensor to work as an I2C backpack.
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.
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.
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.
At the moment you get a bit of lag creep in after a while. I think this might be something to do with the I2C send buffer but will dig in more.
- Track down this lag thing
- Try this on a raw ATTINY using avr-gcc etc so no arduino deps
- Figure out how this comes back into Johnny-five as backpack