NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

jaylfc:
Hi Tim,

I was wondering if you could advise me on how to connect a HC-SR04 to the official arduino robot, I am stumped and have promised some kids a model of R2-D2 for a music event on Sunday using the robot as it's base but only realised afterwards that it doesn't support digital sensors out of the box, please help!

Thanks,
Jay.

I don't know what you mean buy it doesn't support digital sensors. The Arduino Robot's control board has 5 digital I/O ports and the motor board has 4 I/O ports. It looks like the control board has 8 multiplexed pins labeled TK0-TK7 that can't be used with NewPing. But, the specs say there's 5 digital I/O pins, so the interfaces to these pins are in other locations. The motor board's 4 I/O pins TK1-TK4 do have connection ports right on the board that are not multiplexed so they can go straight to the ultrasonic sensor. NewPing can interface with a HC-SR04 using only one pin, so you can wire both the trigger and echo pins on the sensor to the same I/O pin on the Arduino Robot. While TK1 and TK2 are analog pins, they will work just fine as digital pins as well. Remember, analog pins are also always digital pins (but not vice versa).

Since it seems that the motor board is the easier to connect an ultrasonic sensor via NewPing, here's some code that should work (keep in mind that I don't have an Arduino Robot so this is just a shot in the dark, anyone willing to donate one?):

#include <NewPing.h>

NewPing sonar(TK3, TK4, 200);

void setup() {}

void loop() {
  delay(50);
  unsigned int cm = sonar.ping_cm();
  // Do something with the results
}

To conserve available pins, you could also use the same pin for trigger and echo. Also, remember that you could just as well use TK1 and TK2 even though they're analog pins, they will work as digital.

Hope this helps!

Tim