[Arduino] Achieve ultrasonic ranging, stable performance and accurate measuring distance

[Arduino] Achieve ultrasonic ranging, stable performance and accurate measuring distance

Due to the strong directivity of the ultrasonic wave, the energy consumption is slow, and the distance traveled in the medium is long. Therefore, the ultrasonic wave is often used for distance measurement, such as a range finder and a level measuring instrument, which can be realized by ultrasonic waves. The use of ultrasonic testing is often quick, convenient, simple to calculate, easy to achieve real-time control, and can meet industrial and practical requirements in terms of measurement accuracy, so it has also been widely used in the development of mobile robots.

Let’s take a look at the Ultrasonic (HC-SR04) module, which has a very high frequency sound that exceeds the frequency range of sounds that humans can hear. The ultrasonic pulse frequency emitted by the ultrasonic sensor is 42 kHz, and the upper limit of the average human audible frequency is 20 kHz.

The HC-SR04 module has stable performance, and is able to measure accurately. Besides, with high precision, it has small blind zone. It applies to many fields, such as robot obstacle avoidance, object ranging, public security, parking space detection, etc.

working principle of the HC-SR04 module: When a control port sends a high level of at least 10 us, it will trigger the ranging function of SR04. After the trigger, the module will automatically send 8 40KHZ ultrasonic pulses, and automatically detect if there is a signal return. This will be done automatically inside the module. The time at which the other control port is at the high level is the time of the distance measurement, thereby calculating the distance. This continuous period measurement can obtain the value of your mobile measurement.

The code used in the experiment is as follows

/***********************************************************
File name: _36_UltrasonicDistanceSensorModule.ino
Description: When you move the obstacle in front of the
             ultrasonic module,you can see the data on
             the serial montiol.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/03/18
***********************************************************/
const int pingPin = 5;  // pin connected to Echo Pin in the ultrasonic distance sensor
const int trigPin = 6;  // pin connected to trig Pin in the ultrasonic distance sensor
const int stbPin = 7;  //the segment display module STB pin connected to digital pin 7
const int clkPin = 9;  //the segment display module CLK pin connected to digital pin 9
const int dioPin = 8;  //the segment display module DIO pin connected to digital pin 8



void sendCommand(uint8_t value)
{
   digitalWrite(stbPin, LOW);                   //pin low.  To begin receiving data
   shiftOut(dioPin, clkPin, LSBFIRST, value);   //send data(value) to the segment display module
   digitalWrite(stbPin, HIGH);                  //pin high.  Stop receiving data
}


void setup()
{
   pinMode(pingPin, INPUT); //Set the connection pin output mode Echo pin
   pinMode(trigPin, OUTPUT);//Set the connection pin output mode trog pin
   pinMode(stbPin, OUTPUT); //initialize the stbPin as an output
   pinMode(clkPin, OUTPUT); //initialize the clkPin as an output
   pinMode(dioPin, OUTPUT); //initialize the dioPin as an output
   sendCommand(0x8f);       //activate
   Serial.begin(115200);     //opens serial port, sets data rate to 9600 bps
}
                     /*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/
 uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };


int ping(int pingPin)
{
   // establish variables for duration of the ping,
   // and the distance result in inches and centimeters:
   long duration, cm;
   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(trigPin, LOW);


   duration = pulseIn(pingPin, HIGH);

   // convert the time into a distance
   cm = microsecondsToCentimeters(duration);
   return cm ;
}

long microsecondsToCentimeters(long microseconds)
{
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
}

void loop()
{
   int cm = ping(pingPin);
   Serial.print("distance: "); // Print a message of "Temp: "to the serial montiol.
   Serial.print(cm);           // Print a centigrade temperature to the serial montiol.
   Serial.println(" cm");      // Print the unit of the centigrade temperature to the serial montiol.

     sendCommand(0x40);                                       //setting the Write Data Command,using automatic address genuine.
   digitalWrite(stbPin, LOW);                               //pin low.  To begin receiving data
   shiftOut(dioPin, clkPin, LSBFIRST, 0xc0);                //Set the start address 0C0H
   if(cm >= 1000 )
   {
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm/1000%10]);//thousand data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm/100%10]); //hundred data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm/10%10]);  //ten data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm%10]);     //bit data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
   }

   else if(cm >= 100 && cm <=1000)
   {
      shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);//thousand data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm/100%10]); //hundred data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm/10%10]);  //ten data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm%10]);     //bit data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
   }

   else  if(cm >= 10 && cm <=100)
   {
      shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);//thousand data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[0]); //hundred data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm/10%10]);  //ten data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm%10]);     //bit data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
   }

  else  if(cm > 0 && cm <=10)
   {
      shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);//thousand data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[0]); //hundred data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);  //ten data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
     shiftOut(dioPin, clkPin, LSBFIRST, digits[cm%10]);     //bit data
     shiftOut(dioPin, clkPin, LSBFIRST, 0x00);                //filling high 8-bit data
   }

   digitalWrite(stbPin, HIGH);                              //pin high.  Stop receiving data


   delay(500);
}

Leave a Reply