Connect with us

HC-SR04 Ultrasonic Sensor

Discussion in 'Sensors and Actuators' started by kieranheyes99, Dec 2, 2013.

Scroll to continue with content
  1. kieranheyes99


    Dec 2, 2013
    Im having trouble receiving a measurement from the sensor. It is wired through a Arduino Mega 2560.

    Vcc - 5v
    Gnd - gnd
    Trig - 13
    Echo - 10


    #define trigPin 13
    #define echoPin 10

    void setup(){
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);

    void loop(){
    int duration, distance;
    digitalWrite(trigPin, HIGH);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    Serial.println (" cm ");

    Any help would be appreciated

    Attached Files:

  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    Jan 21, 2010
    I'm pretty sure this is the code I used to test mine:

    #define CM 1      //Centimeter
    #define INC 0     //Inch
    #define TP 2      //Trig_pin
    #define EP 3      //Echo_pin  
    long Distance(long time, int flag)
              long distacne;
                 distacne = time /29 / 2  ;     // Distance_CM  = ((Duration of high level)*(Sonic :340m/s))/2
                                                // = ((Duration of high level)*(Sonic :0.034 cm/us))/2
                                                // = ((Duration of high level)/(Sonic :29.4 cm/us))/2
                  distacne = time / 74 / 2;      // INC
              return distacne;
    long TP_init()
             digitalWrite(TP, LOW);
             digitalWrite(TP, HIGH);                 // pull the Trig pin to high level for more than 10us impulse
             digitalWrite(TP, LOW);
             long microseconds = pulseIn(EP,HIGH);   // waits for the pin to go HIGH, and returns the length of the pulse in microseconds
             return microseconds;                    // return microseconds
    void setup()
                 pinMode(TP,OUTPUT);       // set TP output pin for trigger
                 pinMode(EP,INPUT);        // set EP input pin for echo
                 Serial.begin(9600);      // init serial 9600
    void loop()
                 long microseconds = TP_init();   // trigger and receive
                 Serial.print("microseconds = ");
                 long distacne_cm = Distance(microseconds, CM); // Calculating the distance
                 Serial.print("Distacne_CM = ");
                 Serial.println(distacne_cm);   // printf the distance about CM
    edit: it looks functionally very similar. Also from reading this code, it is something I cut and pasted from somewhere as there are several aspects of this code that are different from the way I would have written it. It's worth checking that you have connected up the correct pins. The output numbers do not correspond to pin numbers and I've made errors with that before now...
    Last edited: Dec 2, 2013
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day