Jan Zumwalt
Published © GPL3+

Pinguino HC-SR04 Ultrasonic Sonar

Olimex Pinguino Micro board & hc-sr04 ultrasonic sensor with output to CDC.

BeginnerFull instructions provided1,010
Pinguino HC-SR04 Ultrasonic Sonar

Things used in this project

Hardware components

HC-SR04 Ultrasonic Sonar
×1
Pinguino compatible microcontroller
×1

Story

Read more

Code

Code snippet #1

Plain text
/* program:  hc-sr04_ping_cdc.pde 
   author:   Jan Zumwalt - 2016.01.18
   hardware: Olimex Micro brd, hc-sr04 sensor
   software: Pinguino.cc IDE
   
   Description:  Ping ))) Sensor
                 yellow blink is ping being sent
                 green blink is successful measurement
  
   (1)	A trigger at least 10us HIGH is sent
   (2)	Module sends 8 40kHz pulses and detects an echo.
   (3)	Return pulse triggers echo pin HIGH.
   (4)	Distance is width of the HIGH echo pulse.
        distance        =  (high echo pulse) x (speed of sound)  / 2
        distance (cm)   =  pulse width(uS)  / 58
        distance (inch) =  pulse width(uS)  / 148

    Connection:
        sensor #1 to board +5v
        sensor #2 to board #14 - trig (output), used to initiate ping
        sensor #3 to board #15 - pulse width proportionate to distance
        sensor #4 to board Gnd  */

 const int echoPin    = 15;         // echo pin 
 const int triggerPin = 14;         // trigger pin
 float dist_inch      = 0;          // measured distance inches
 float dist_cent      = 0;          // measured distance centimetre
 int   ping_status    = 0;          // valid reading status 0=no, 1=yes
 float ping_start     = 0;          // ping start time
 float ping_end       = 0;          // ping end time
 float ping_time      = 0;          // ping pulse time
 u32   cnt            = 0;          // ping pulse time
 
 void setup() {
   pinMode(GREENLED, OUTPUT);       // init led pin
   pinMode(YELLOWLED, OUTPUT);      // init led pin
   pinMode(triggerPin, OUTPUT);     // init trigger pin
   pinMode(echoPin, INPUT);         // init echo pin
                                   
   digitalWrite(GREENLED, LOW);     // led off
   digitalWrite(YELLOWLED, LOW);    // led off
   digitalWrite(triggerPin, LOW);   // ping off 
 }
 
 void loop()  {
   ping();                          // request measurement
   if (ping_status) {               // if measurement was successful
     CDC.printf("%fin (%fcm)\r\n", dist_inch, dist_cent);
     CDC.printf("%fus\r\n\r\n", ping_time);
     digitalWrite(GREENLED,HIGH);   // long green blink, success
     delay(1000);
     digitalWrite(GREENLED,LOW);  
     delay(1000);                   // wait another second
   } else {
     CDC.print("no echo\r\n");
   }  // end if
 } // end loop
 
 void ping ()  {
   ping_status        = 0;          // no reading yet
 
   digitalWrite(YELLOWLED, HIGH);   // blink led quickly to show ping
   delay(100);
   digitalWrite(YELLOWLED, LOW);   
   
   digitalWrite(triggerPin, HIGH);  // trigger ping
   delayMicroseconds(15);           // delay 15 millionths of a sec
   digitalWrite(triggerPin, LOW);   // end of pulse
   
   cnt = 0;                         // counter   
   while (digitalRead(echoPin) == LOW )  { // wait for echo pulse to go HIGH
     cnt++;                         // loop counter
     if (cnt > 100000) {            // timeout if wait too long
       break; 
     } // no echo came back
   }  // end while
   
   if (cnt < 100000) {              // we didn't time echo
     cnt = 0;                       // new timeout counter
     ping_status = 1;               // assume we have good measurement
     ping_start = micros();         // ping start time
     
     while (digitalRead(echoPin) == HIGH)  {  // wait for end of echo
        cnt++;                      // loop counter    
       if (cnt > 100000) {          // timeout if wait too long
         ping_status = 0;           // bad measurement
         break;                     // didn't work
       } // end if pulse       
     }  // end while
     
     ping_end = micros();           // ping end or abort time     
     ping_time = ping_end - ping_start; // ping travel time
     dist_inch = ping_time  / 148;  // inch distance
     dist_cent = ping_time  / 58;   // centimetre distance
   
   } // end if
 } // end ping
 

Credits

Jan Zumwalt
9 projects • 14 followers
Retired aerospace engineer, aviation pilot, mechanic
Contact

Comments

Please log in or sign up to comment.