By Kumantech | 30 June 2017 | 1096 Comments
Auduino ultrasonic distance measuring tool
What is the ultrasonic distance measuring module?
Let's see HC-SR04 sensor:
Ultrasonic ranging module HC - SR04 provides 2cm - 700cm non-contact measurement function, the ranging accuracy can reach to 3mm.Ensured stable signal within 5m, gradually faded signal outside 5m till disappearing at 7m position.
The module includes ultrasonic transmitters, receiver and control circuit. The basic principle of work:
(1) Using IO trigger for at least 10us high level signal;
(2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
(3) IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning. Test distance = (high level time×velocity of sound (340M/S) / 2.
Pin Function:
TRIG: Trigger Pulse Input
ECHO: Echo Pulse Output
GND: Ground
VCC: 5V Supply
Features:
Working Voltage: DC5V
Working Current: 16mA
Working Frequency: 40Hz
Max Range: 700cm, ensured stable signal within 5m, gradually faded signal outside 5m till disappearing at 7m position.
Min Range: 2cm
Trigger Input Signal: 10uS TTL pulse
Echo Output Signal Input: TTL lever signal and the range in proportion
Dimension: 46x20.5x15 mm
Pricinple:
The Timing diagram is shown below. You only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo. The Echo is a distance object that is pulse width and the range in proportion .You can calculate the range through the time interval between sending trigger signal and receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the range = high level time * velocity (340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
Test Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Echo 11 //Echo connnect to pin11
#define Trig 12 //Trig connect to pin12
unsigned long rxTime; //define a variable
float distance;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200); //set the baud rate of serial monitor
pinMode(Echo,INPUT);
pinMode(Trig,OUTPUT);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight(); //turn o the backlight
lcd.setCursor(0, 0); //set the cursor on 0 row,0 col
lcd.print("Ping:"); //print the "Ping: "on the LCD
}
void loop()
{
// Generates a pulse
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
rxTime = pulseIn(Echo, HIGH); //read the Receive time
// Serial.print("rxTime:");
// Serial.println(rxTime);
distance = (float)rxTime * 34 / 2000.0; //Converted into a distance ,cm
if(distance < 800 ) //filter interference signal
{
Serial.print("distance:");
Serial.print(distance); //print it the distance in serial monitor
Serial.println("CM");
lcd.setCursor(6, 0);
lcd.print(distance);//print it in LCD1602
lcd.print("CM");
delay(100);
}
}
Introduction to the 8 Segment Display:
This experiment is the use of Arduino to drive a total of four digital tube. The limit of the flow resistance is essential to drive the digital tube, the current limit resistor has two kinds of connection, a total of 4 D1-D4. This connection is less demand resistance, but will produce a different number of digital brightness will not be the same, 8 of the most dark. This method is the other 8 pin, the brightness of the same, but with more resistance 220 100 8 ohm resistor, so use 220 instead of 10. 0 ohm brightness will be relatively high.

4 digital tube a total of 12 pins, the decimal point is placed in front of the bottom, the lower left corner of the 1, the other pins in the order of the counterclockwise rotation. The upper left corner for the largest 12 pin.

The following figure manual of digital tube:

Four 8-segment digital tube code tables:

Ultrasonic range testing:
The hc-sr04 module has stable performance, accurate measurement range, and can be compared with foreign SRF05, SRF02 and other ultrasonic ranging module. Module high precision, blind area (2cm) ultra near, stable ranging is the product of this product successfully to the market!
Components
- 1* Arduino control board *1
- 2 * Breadboard
- 1 * USB data cable
- 1 * 8 Segment Display (4 digit)
- Several jumper wires
The following is a hardware connection diagram:

Here is the program:
//Pin definition
#define TrigPin 2
#define EchoPin 3
#define d_a A2
#define d_b A3
#define d_c 4
#define d_d 5
#define d_e 6
#define d_f 7
#define d_g 8
#define d_h 9
#define COM1 10
#define COM2 11
#define COM3 12
#define COM4 13
float dis;
int dist;
unsigned long changeTimes;
//digital tube 0-9
//without the decimal point
unsigned char num[16][8] =
{
{0, 0, 0, 0, 0, 0, 1, 1}, //0
{1, 1, 1, 1, 0, 0, 1, 1}, //1
{0, 0, 1, 0, 0, 1, 0, 1}, //2
{0, 0, 0, 0, 1, 1, 0, 1}, //3
{1, 0, 0, 1, 1, 0, 0, 1}, //4
{0, 1, 0, 0, 1, 0, 0, 1}, //5
{0, 1, 0, 0, 0, 0, 0, 1}, //6
{0, 0, 0, 1, 1, 1, 1, 1}, //7
{0, 0, 0, 0, 0, 0, 0, 1}, //8
{0, 0, 0, 1, 1, 0, 0, 1}, //9
};
//With decimal point
unsigned char num_dp[10][8] =
{
{0, 0, 0, 0, 0, 0, 1, 0}, //0
{1, 1, 1, 1, 0, 0, 1, 0}, //1
{0, 0, 1, 0, 0, 1, 0, 0}, //2
{0, 0, 0, 0, 1, 1, 0, 0}, //3
{1, 0, 0, 1, 1, 0, 0, 0}, //4
{0, 1, 0, 0, 1, 0, 0, 0}, //5
{0, 1, 0, 0, 0, 0, 0, 0}, //6
{0, 0, 0, 1, 1, 1, 1, 0}, //7
{0, 0, 0, 0, 0, 0, 0, 0}, //8
{0, 0, 0, 1, 1, 0, 0, 0}, //9
};
void setup()
{
//Set to output pins
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(d_a,OUTPUT);
pinMode(d_b,OUTPUT);
pinMode(d_c,OUTPUT);
pinMode(d_d,OUTPUT);
pinMode(d_e,OUTPUT);
pinMode(d_f,OUTPUT);
pinMode(d_g,OUTPUT);
pinMode(d_h,OUTPUT);
pinMode(COM1,OUTPUT);
pinMode(COM2,OUTPUT);
pinMode(COM3,OUTPUT);
pinMode(COM4,OUTPUT);
}
void loop()
{
Display(1,dist%10); //take the decimal point
delay(3);
Display_dp(2,dist/10%10); //Take a single
delay(3);
Display(3,dist/100%10); //Take ten digits
delay(3);
Display(4,dist/1000); //Take the digits
delay(3);
if ((millis() - changeTimes) > 1000) //Check once a second
{
digitalWrite(TrigPin, LOW);
delay(2);
digitalWrite(TrigPin, HIGH);
delay(10);
digitalWrite(TrigPin, LOW);
dis=pulseIn(EchoPin,HIGH)/58.00;//Convert the echo time to cm
dist=(int(dis*10));
changeTimes = millis() ;
}
}
//The display function, com can choose a range of 1-4, num can be selected for a range of 0-9
void Display(unsigned char com,unsigned char n)
{
digitalWrite(d_a,LOW); //Remove the light
digitalWrite(d_b,LOW);
digitalWrite(d_c,LOW);
digitalWrite(d_d,LOW);
digitalWrite(d_e,LOW);
digitalWrite(d_f,LOW);
digitalWrite(d_g,LOW);
digitalWrite(d_h,LOW);
switch(com) //Gate a selected
{
case 1:
digitalWrite(COM1,LOW); //Choose a 1
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,HIGH);
break;
case 2:
digitalWrite(COM1,LOW);
digitalWrite(COM2,LOW); //Choose a 2
digitalWrite(COM3,HIGH);
digitalWrite(COM4,LOW);
break;
case 3:
digitalWrite(COM1,LOW);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,LOW); //Choose a three
digitalWrite(COM4,LOW);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,LOW); //Choose a 4
break;
default:break;
}
digitalWrite(d_a,num[n][0]); //The query code value table
digitalWrite(d_b,num[n][1]);
digitalWrite(d_c,num[n][2]);
digitalWrite(d_d,num[n][3]);
digitalWrite(d_e,num[n][4]);
digitalWrite(d_f,num[n][5]);
digitalWrite(d_g,num[n][6]);
digitalWrite(d_h,num[n][7]);
}
//The display function, com can choose a range of 1-4, num can be selected for a range of 0-9
void Display_dp(unsigned char com,unsigned char n)
{
digitalWrite(d_a,LOW); //Remove the light
digitalWrite(d_b,LOW);
digitalWrite(d_c,LOW);
digitalWrite(d_d,LOW);
digitalWrite(d_e,LOW);
digitalWrite(d_f,LOW);
digitalWrite(d_g,LOW);
digitalWrite(d_h,LOW);
switch(com) //Gate a selected
{
case 1:
digitalWrite(COM1,LOW); //Choose a 1
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,HIGH);
break;
case 2:
digitalWrite(COM1,LOW);
digitalWrite(COM2,LOW); //Choose a 2
digitalWrite(COM3,HIGH);
digitalWrite(COM4,LOW);
break;
case 3:
digitalWrite(COM1,LOW);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,LOW); //Choose a 3
digitalWrite(COM4,LOW);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,LOW); //Choose a 4
break;
default:break;
}
digitalWrite(d_a,num_dp[n][0]); //The query code value table
digitalWrite(d_b,num_dp[n][1]);
digitalWrite(d_c,num_dp[n][2]);
digitalWrite(d_d,num_dp[n][3]);
digitalWrite(d_e,num_dp[n][4]);
digitalWrite(d_f,num_dp[n][5]);
digitalWrite(d_g,num_dp[n][6]);
digitalWrite(d_h,num_dp[n][7]);
}
Let's see HC-SR04 sensor:
Ultrasonic ranging module HC - SR04 provides 2cm - 700cm non-contact measurement function, the ranging accuracy can reach to 3mm.Ensured stable signal within 5m, gradually faded signal outside 5m till disappearing at 7m position.
The module includes ultrasonic transmitters, receiver and control circuit. The basic principle of work:
(1) Using IO trigger for at least 10us high level signal;
(2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
(3) IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning. Test distance = (high level time×velocity of sound (340M/S) / 2.
Pin Function:
TRIG: Trigger Pulse Input
ECHO: Echo Pulse Output
GND: Ground
VCC: 5V Supply
Features:
Working Voltage: DC5V
Working Current: 16mA
Working Frequency: 40Hz
Max Range: 700cm, ensured stable signal within 5m, gradually faded signal outside 5m till disappearing at 7m position.
Min Range: 2cm
Trigger Input Signal: 10uS TTL pulse
Echo Output Signal Input: TTL lever signal and the range in proportion
Dimension: 46x20.5x15 mm
Pricinple:
The Timing diagram is shown below. You only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo. The Echo is a distance object that is pulse width and the range in proportion .You can calculate the range through the time interval between sending trigger signal and receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the range = high level time * velocity (340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.

Test Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Echo 11 //Echo connnect to pin11
#define Trig 12 //Trig connect to pin12
unsigned long rxTime; //define a variable
float distance;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200); //set the baud rate of serial monitor
pinMode(Echo,INPUT);
pinMode(Trig,OUTPUT);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight(); //turn o the backlight
lcd.setCursor(0, 0); //set the cursor on 0 row,0 col
lcd.print("Ping:"); //print the "Ping: "on the LCD
}
void loop()
{
// Generates a pulse
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
rxTime = pulseIn(Echo, HIGH); //read the Receive time
// Serial.print("rxTime:");
// Serial.println(rxTime);
distance = (float)rxTime * 34 / 2000.0; //Converted into a distance ,cm
if(distance < 800 ) //filter interference signal
{
Serial.print("distance:");
Serial.print(distance); //print it the distance in serial monitor
Serial.println("CM");
lcd.setCursor(6, 0);
lcd.print(distance);//print it in LCD1602
lcd.print("CM");
delay(100);
}
}
Introduction to the 8 Segment Display:
This experiment is the use of Arduino to drive a total of four digital tube. The limit of the flow resistance is essential to drive the digital tube, the current limit resistor has two kinds of connection, a total of 4 D1-D4. This connection is less demand resistance, but will produce a different number of digital brightness will not be the same, 8 of the most dark. This method is the other 8 pin, the brightness of the same, but with more resistance 220 100 8 ohm resistor, so use 220 instead of 10. 0 ohm brightness will be relatively high.

4 digital tube a total of 12 pins, the decimal point is placed in front of the bottom, the lower left corner of the 1, the other pins in the order of the counterclockwise rotation. The upper left corner for the largest 12 pin.

The following figure manual of digital tube:

Four 8-segment digital tube code tables:

Ultrasonic range testing:
The hc-sr04 module has stable performance, accurate measurement range, and can be compared with foreign SRF05, SRF02 and other ultrasonic ranging module. Module high precision, blind area (2cm) ultra near, stable ranging is the product of this product successfully to the market!
Components
- 1* Arduino control board *1
- 2 * Breadboard
- 1 * USB data cable
- 1 * 8 Segment Display (4 digit)
- Several jumper wires
The following is a hardware connection diagram:

Here is the program:
//Pin definition
#define TrigPin 2
#define EchoPin 3
#define d_a A2
#define d_b A3
#define d_c 4
#define d_d 5
#define d_e 6
#define d_f 7
#define d_g 8
#define d_h 9
#define COM1 10
#define COM2 11
#define COM3 12
#define COM4 13
float dis;
int dist;
unsigned long changeTimes;
//digital tube 0-9
//without the decimal point
unsigned char num[16][8] =
{
{0, 0, 0, 0, 0, 0, 1, 1}, //0
{1, 1, 1, 1, 0, 0, 1, 1}, //1
{0, 0, 1, 0, 0, 1, 0, 1}, //2
{0, 0, 0, 0, 1, 1, 0, 1}, //3
{1, 0, 0, 1, 1, 0, 0, 1}, //4
{0, 1, 0, 0, 1, 0, 0, 1}, //5
{0, 1, 0, 0, 0, 0, 0, 1}, //6
{0, 0, 0, 1, 1, 1, 1, 1}, //7
{0, 0, 0, 0, 0, 0, 0, 1}, //8
{0, 0, 0, 1, 1, 0, 0, 1}, //9
};
//With decimal point
unsigned char num_dp[10][8] =
{
{0, 0, 0, 0, 0, 0, 1, 0}, //0
{1, 1, 1, 1, 0, 0, 1, 0}, //1
{0, 0, 1, 0, 0, 1, 0, 0}, //2
{0, 0, 0, 0, 1, 1, 0, 0}, //3
{1, 0, 0, 1, 1, 0, 0, 0}, //4
{0, 1, 0, 0, 1, 0, 0, 0}, //5
{0, 1, 0, 0, 0, 0, 0, 0}, //6
{0, 0, 0, 1, 1, 1, 1, 0}, //7
{0, 0, 0, 0, 0, 0, 0, 0}, //8
{0, 0, 0, 1, 1, 0, 0, 0}, //9
};
void setup()
{
//Set to output pins
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(d_a,OUTPUT);
pinMode(d_b,OUTPUT);
pinMode(d_c,OUTPUT);
pinMode(d_d,OUTPUT);
pinMode(d_e,OUTPUT);
pinMode(d_f,OUTPUT);
pinMode(d_g,OUTPUT);
pinMode(d_h,OUTPUT);
pinMode(COM1,OUTPUT);
pinMode(COM2,OUTPUT);
pinMode(COM3,OUTPUT);
pinMode(COM4,OUTPUT);
}
void loop()
{
Display(1,dist%10); //take the decimal point
delay(3);
Display_dp(2,dist/10%10); //Take a single
delay(3);
Display(3,dist/100%10); //Take ten digits
delay(3);
Display(4,dist/1000); //Take the digits
delay(3);
if ((millis() - changeTimes) > 1000) //Check once a second
{
digitalWrite(TrigPin, LOW);
delay(2);
digitalWrite(TrigPin, HIGH);
delay(10);
digitalWrite(TrigPin, LOW);
dis=pulseIn(EchoPin,HIGH)/58.00;//Convert the echo time to cm
dist=(int(dis*10));
changeTimes = millis() ;
}
}
//The display function, com can choose a range of 1-4, num can be selected for a range of 0-9
void Display(unsigned char com,unsigned char n)
{
digitalWrite(d_a,LOW); //Remove the light
digitalWrite(d_b,LOW);
digitalWrite(d_c,LOW);
digitalWrite(d_d,LOW);
digitalWrite(d_e,LOW);
digitalWrite(d_f,LOW);
digitalWrite(d_g,LOW);
digitalWrite(d_h,LOW);
switch(com) //Gate a selected
{
case 1:
digitalWrite(COM1,LOW); //Choose a 1
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,HIGH);
break;
case 2:
digitalWrite(COM1,LOW);
digitalWrite(COM2,LOW); //Choose a 2
digitalWrite(COM3,HIGH);
digitalWrite(COM4,LOW);
break;
case 3:
digitalWrite(COM1,LOW);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,LOW); //Choose a three
digitalWrite(COM4,LOW);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,LOW); //Choose a 4
break;
default:break;
}
digitalWrite(d_a,num[n][0]); //The query code value table
digitalWrite(d_b,num[n][1]);
digitalWrite(d_c,num[n][2]);
digitalWrite(d_d,num[n][3]);
digitalWrite(d_e,num[n][4]);
digitalWrite(d_f,num[n][5]);
digitalWrite(d_g,num[n][6]);
digitalWrite(d_h,num[n][7]);
}
//The display function, com can choose a range of 1-4, num can be selected for a range of 0-9
void Display_dp(unsigned char com,unsigned char n)
{
digitalWrite(d_a,LOW); //Remove the light
digitalWrite(d_b,LOW);
digitalWrite(d_c,LOW);
digitalWrite(d_d,LOW);
digitalWrite(d_e,LOW);
digitalWrite(d_f,LOW);
digitalWrite(d_g,LOW);
digitalWrite(d_h,LOW);
switch(com) //Gate a selected
{
case 1:
digitalWrite(COM1,LOW); //Choose a 1
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,HIGH);
break;
case 2:
digitalWrite(COM1,LOW);
digitalWrite(COM2,LOW); //Choose a 2
digitalWrite(COM3,HIGH);
digitalWrite(COM4,LOW);
break;
case 3:
digitalWrite(COM1,LOW);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,LOW); //Choose a 3
digitalWrite(COM4,LOW);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,LOW);
digitalWrite(COM3,LOW);
digitalWrite(COM4,LOW); //Choose a 4
break;
default:break;
}
digitalWrite(d_a,num_dp[n][0]); //The query code value table
digitalWrite(d_b,num_dp[n][1]);
digitalWrite(d_c,num_dp[n][2]);
digitalWrite(d_d,num_dp[n][3]);
digitalWrite(d_e,num_dp[n][4]);
digitalWrite(d_f,num_dp[n][5]);
digitalWrite(d_g,num_dp[n][6]);
digitalWrite(d_h,num_dp[n][7]);
}
Recently Reviews
Read MoreLeave a Reply
Your email address will not be published.Required fields are marked. *
POPULAR BLOG
CATEGORIES