今天做一个超声波测距的仪器,有想了解超声波的百度百科哈,说的很详细。LCD1602的应用,结合超声波模块,实现测距数显的小仪器。
接线图中我没有将电池充电口留出来,有做DIY的朋友可以将充电口留出来,以便长期使用。
#include #include LiquidCrystal_I2C lcd(0x27,16,2);// if your LCD does not display,change it to 0x27 or other addressconst int trigPin = 4;const int echoPin = 3;// defines variableslong duration;int distance;void display1(){ lcd.setCursor(0,0); lcd.print("Distance:"); lcd.setCursor(13, 1); lcd.print("cm"); }void setup() { // put your setup code here, to run once: lcd.init(); // initialize the lcd lcd.backlight(); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication pinMode(5,OUTPUT); //virtual power pin digitalWrite(5,HIGH); // virtual power pin pinMode(2,OUTPUT); //virtual power pin digitalWrite(2,LOW); }void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); display1(); lcd.setCursor(8,1); lcd.print(distance); delay(1000); lcd.clear(); }
超声波是声波的一部分,是人耳听不见、频率高于20KHZ的声波,它和声波有共同之处,即都是由物质振动而产生的,并且只能在介质中传播;同时,它也广泛地存在于自然界,许多动物都能发射和接收超声波,其中以蝙蝠最为突出,它能利用微弱的超声回波在黑暗中飞行并捕捉食物。但超声还有它的特殊性质'如具有较高的频率与较短的波长,所以,它也与波长很短的光波有相似之处。
超声波是弹性机械振动波,它与可听声相比还有一些特点:传播的方向较强,可聚集成定向狭小的线束;在传播介质质点振动的加速度非常之大;在液体介质中当超声强度达到一定值后便会发生空化现象。
利用超声的机械作用、空化作用、热效应和化学效应,可进行超声焊接、钻孔、固体的粉碎、乳化 、脱气、除尘、去锅垢、清洗、灭菌、促进化学反应和进行生物学研究等,在工矿业、农业、医疗等各个部门获得了广泛应用。