Shop OBEX P1 Docs P2 Docs Learn Events
Getting started with a 4 pin Ping Sensor, HC-SR04 — Parallax Forums

Getting started with a 4 pin Ping Sensor, HC-SR04

Kraken767Kraken767 Posts: 5
edited 2014-10-25 14:39 in Learn with BlocklyProp
I hadn't seen any tutorials on a 4 pin HC-SR04 ping sensor for use with a Parallax P8X32A, so I thought I'd give some starter info for people new to this.
I basically looked at the code for the 3 pin ping in the Simple Libraries and tweaked it a bit, here it is.
#include "simpletools.h"

int main(void){
  while(1){
    float i = -1;
    i = ping(10,11);
    print("%f\n", i/148); //divide the ping by 148 to get inches, or by 54 to get centimeters
    pause(300);
  }
}


int ping(int trig, int echo)//trig is trigger pin, echo is echo pin
{
  low(trig);//set trig low for start pulse
  low(echo);//set echo low to be safe
  pulse_out(trig, 10);//send the minimum 10 ms pulse on trig to start a ping
  return pulse_in(echo, 1);//get the pulse duration back on echo pin
}

Comments

  • PublisonPublison Posts: 12,366
    edited 2014-10-25 12:15
    Welcome to the forums!

    Thanks for the code. This will help out others with the same hardware..
  • DomanikDomanik Posts: 233
    edited 2014-10-25 14:39
    +1 Kraken, Something I planned to do now you'll save me some time. YEAH!:smile:
    Thanks,
    Dom..
Sign In or Register to comment.