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

    kieranheyes99

    1
    0
    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


    CODE

    #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);
    delayMicroseconds(100);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    Serial.print(distance);
    Serial.println (" cm ");
    delay(100);
    }


    Any help would be appreciated
     

    Attached Files:

  2. (*steve*)

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

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

    Code:
    #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;
              if(flag)
                 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
             else
                  distacne = time / 74 / 2;      // INC
              return distacne;
    }
    
    long TP_init()
    {
             digitalWrite(TP, LOW);
             delayMicroseconds(2);
             digitalWrite(TP, HIGH);                 // pull the Trig pin to high level for more than 10us impulse
             delayMicroseconds(10);
             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
                 Serial.println("-------------------Ultra_Demo_Start---------------------------------");
    }
    
    void loop()
    {
                 long microseconds = TP_init();   // trigger and receive
                 Serial.print("microseconds = ");
                 Serial.println(microseconds);
                 long distacne_cm = Distance(microseconds, CM); // Calculating the distance
                 Serial.print("Distacne_CM = ");
                 Serial.println(distacne_cm);   // printf the distance about CM
                 delay(3000);
    }
    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

-