Maker Pro
Maker Pro

HC-SR04 Ultrasonic Sensor

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
 

Attachments

  • IMG_1635.jpg
    IMG_1635.jpg
    144.7 KB · Views: 187
  • IMG_1636.jpg
    IMG_1636.jpg
    143.8 KB · Views: 164

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
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:
Top