Lesson 10--Microduino Ultrasonic Sensor Measures Distance

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

This lesson will show how to use the Microduino ultrasonic sensor to control buzzer and measure distance. You'll know the ultrasonic control by this simple example.

Test object is the SRF-04 ultrasonic sensors with four pins: 5v power supply (Vcc), trigger control pin (Trig), the receiver (Echo), and the GND.


Equipment

IntermediateLesson10All1.jpg
IntermediateLesson10All2.jpg

Ultrasonic sensor

第十课-超声波传感器.jpg

Brief Introduction: Ultrasonic sensor is designed by the characteristics of ultrasonic. Ultrasound is a kind of mechanical wave of vibration frequency is higher than the sound wave, is generated by changing the chip vibration under the excitation of voltage. It has a higher frequency, shorter wavelengths, small diffraction phenomenon, especially good directivity, can become rays and directional transmission etc. Ultrasonic penetrating power of liquid and solid is very big, especially in sunshine opaque solid, it can penetrate tens of metres in depth. Ultrasonic encounter impurities or interface can produce significant reflection form into echo, touching the moving objects can produce doppler effect. So ultrasonic detection is widely used in industry, national defense, biomedicine etc.

Components: Ultrasonic probe is mainly composed of piezoelectric wafer, can emit ultrasonic already, also can receive ultrasonic. Low power ultrasound probe for detecting effect. It has a lot of different structure, can be divided into straight probe (longitudinal wave), oblique probe (shear wave) and surface wave probe (surface wave), lamb wave sensor (lamb wave), double probe (a probe reflection, a probe receiving) etc.

Schematic

第十课-Microduino超声波测距原理图.jpg

Program

Download program: https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Advanced/MicroduinoUltrasonicRanging

const int TrigPin = 3;
const int EchoPin = 2;
float cm;

void setup()
{
	Serial.begin(9600);
	pinMode(TrigPin, OUTPUT);
	pinMode(EchoPin, INPUT);
	pinMode(8,OUTPUT);
}
void loop()
{
	digitalWrite(8, LOW);
	digitalWrite(TrigPin, LOW); //Send a low level voltage pulse to TrigPin
	delayMicroseconds(2);
	digitalWrite(TrigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(TrigPin, LOW);

	cm = pulseIn(EchoPin, HIGH) / 58.0; //Echo time convert to cm
	cm = (int(cm * 100.0)) / 100.0; //Keep two decimal places
	if (cm>=2 && cm<=10)
		digitalWrite(8, HIGH);
}

Debug

Step 1: Copy the upper code to IDE and compile

Step 2: Compile no error, then set up the circuit, as follows:

IntermediateLesson10Connect1.jpg
IntermediateLesson10Connect2.jpg
第十课-Microduino超声波测距电路图.jpg

Step 3: Run program

Step 4: Put the hand in front of ultrasonic sensor, listening the sound of the buzzer.

Result

If there is an object in front of the ultrasonic sensor, and the distance of the object is larger than 2 cm, less than 10 cm, buzzer will sound. You can make an ultrasonic alarm based on this.

Video