How to read ultrasonic sensor echo pulse

I’m trying to use an ultrasonic sensor with the programmable XBee-PRO 900HP. Model is HC-SR04, data sheet is This sensor is ubiquitous in the Arduino world.

To read the distance from the ultrasonic sensor I send a 10 microsecond pulse on the trigger pin and measure the duration of the return pulse on the echo pin, which is proportional to the measured distance. This code to measure the duration of the echo pulse is trivial on Arduino (e.g., using Arduino’s pulseIn function, but I can’t figure out how to do the same on the XBee.

It seems like I should be using the input capture library to measure the echo pulse duration (is this correct?) but I’m struggling as I can’t find working code examples online that demonstrate how to drive an ultrasonic sensor using a programmable XBee.

Basically I want to know how to write the equivalent of Arduino’s pulseIn function for the XBee.

Any help much appreciated.


See these resources:

Also make sure that you have the proper level shifting occurring between the sensor and the XBee. The XBee’s DIO lines will be 1.2V and your sensor is 5V.