Welcome! Log In Create A New Profile


microseconds delay to use HC_SR04 distance sensor

Posted by jon1977 
microseconds delay to use HC_SR04 distance sensor
November 24, 2016 10:17PM
I'm trying to implemet code on a BV500 for this device.
I have it working on a Partical Core micro, so I know it works.
The issue is timing in microseconds, and specifically how I deal with that in the bypic language.

pseudo code:
Setup an output pin (called trigger)
Setup an input pin (called echo)

Start with trigger low
Send a 10us (microsecond) high trigger pulse
Read duration that the input pin is high, where Formula: uS / 58 = centimeters

This is the working extract of the partical core code:

double HC_SR04::getDistanceCM(){
  waitForEcho(_echoPin, HIGH, 100);
  long startTime = micros();
  waitForEcho(_echoPin, LOW, 100);
  long endTime = micros();
  long duration = endTime - startTime;
  double distance = duration / 29.0 / 2.0;
  if (distance < _minCM || distance > _maxCM){
   return -1;
  return distance;

void HC_SR04::sendTriggerPulse(int pin){
    digitalWrite(pin, HIGH);
    digitalWrite(pin, LOW);

void HC_SR04::waitForEcho(int pin, int value, long timeout){
    long giveupTime = millis() + timeout;
    while (digitalRead(pin) != value && millis() < giveupTime) {}

This is as far as I have got with the BV500, which does not work (note, I'm using main2() so I can manually start the function, I'll replace this with main() when I know it works! Note, I've also put some debug prints in there to try and work out what is going on. There must be a simpler way of doing this!

// #include "lib_tmr.bas"
// #include "lib_intr.bas"
// #include "lib_io.bas"
//send 10us trigger pulse
//wait for echo pin to go high
//time for echo pin to go low (ms/= distance
//10cm to 250cm
//the trigger is connected to pin 15 (RB6)
constant trigPin {PORTB(PORT),8}
//the echo is connected to pin 16 (RB7)
constant echoPin {PORTB(PORT),7}

constant ANSELA          0xBF886000
constant ANSELACLR       0xBF886004
constant ANSELASET       0xBF886008
constant ANSELAINV       0xBF88600C
constant TRISA           0xBF886010
constant TRISACLR        0xBF886014
constant TRISASET        0xBF886018
constant TRISAINV        0xBF88601C
constant PORTA           0xBF886020
constant LATA            0xBF886030
constant PORTACLR        0xBF886024
constant PORTASET        0xBF886028
constant PORTAINV        0xBF88602C
constant CNPUASET        0xBF886058

function init()
	io_pinRole(*trigPin(),OUT,WOFF)	//set output
	io_pinRole(*echoPin(),IN,WPD)	//set input with weak pull down

function waitforecho(*echotime,value,timeout)
    print "Waiting for echo:"
    print value
    print "rn"
    while (io_pinGet(*echoPin()) <> value) && (echotime < timeout)
        echotime = echotime + 1
    print "Echo received:"
    print echotime
    print " msrn"

function settrigger()
    print "Sending Triggerrn"
    tmr_init(*TIMER4(),2,100) //10us pulse
    print "Timer initialisedrn"
    print "Trig pin highrn"
    while tmr_get(*TIMER4()) < 100
         //print "*"
    //print "rn"
    io_pinSet(*trigPin(),0) //put the pin low
    tmr_clr(*TIMER4()) //reset the timer
    print "Trig pin lowrn"

function getdistance()
    dim echotime
    dim distance#
    echotime = 0
    io_pinSet(*trigPin(),0) //put the pin low
	distance# = echotime / 29.0 / 2.0
    print "rn"
    print "Distance is: "
    print distance#
    print "cmrn"

function settriglow()
    io_pinSet(*trigPin(),0) //put the pin low

function settrighigh()
    io_pinSet(*trigPin(),1) //put the pin low

function readecho()

function main2()
	tmr_init(*TIMER23(),7,312500) //1 second repeating
	ir_set(*TIMER23(),3,"getdistance") //priority3

Can anyone point me in the correct direction?

Edited 1 time(s). Last edit at 11/24/2016 10:17PM by jon1977.
Re: microseconds delay to use HC_SR04 distance sensor
November 25, 2016 09:12AM
hello, bellow is from the ByPic web site with a working example for this device.


Re: microseconds delay to use HC_SR04 distance sensor
November 27, 2016 10:22PM
That works great, thanks.
I started playing with this code earlier in the year. I guess that guide wasnt on the site then!
Re: microseconds delay to use HC_SR04 distance sensor
November 27, 2016 10:24PM
Although I had to change the constant from 12 to 7 to get a more acurate measurement.
Sorry, you do not have permission to post/reply in this forum.