Arduino水位指示器如何工作?
該Arduino水位指示器使用超聲波傳感器或Ping傳感器來確定水箱中的水位。 Ping傳感器使用聲納測量距離。從該單元發(fā)射超聲波(遠(yuǎn)高于人類聽覺)脈沖,并且通過測量回波返回所需的時(shí)間來確定到目標(biāo)的距離。 Ping傳感器的輸出是可變寬度脈沖,對(duì)應(yīng)于到目標(biāo)的距離。然后將其輸入微控制器,確定水位并通過一系列LED顯示。
以下項(xiàng)目可以是如果您有一個(gè)或直接連接到面板上的ATmega 328微控制器,則可以連接到Arduino板。您還可以查看Jeff的Maker Pro教程,了解如何將超聲波傳感器連接到Arduino。
Arduino水位指示器代碼
將提供的草圖復(fù)制粘貼到Arduino IDE中并找到“int d = 18;”行并將“18”更改為 坦克的深度,單位為厘米。
//Coded by MATHEW VARGHESE
//Note that the numbering of arduino pins are different from microcontroller pinout
int d = 18; //Enter depth of your tank here in centimeters
int trig = 11; // Attach Trig of ultrasonic sensor to pin 11
int echo = 10; // Attach Echo of ultrasonic sensor to pin 10
int pin1 = 2;//Highest level
int pin2 = 3;
int pin3 = 4;
int pin4 = 5;
int pin5 = 6;
int pin6 = 7;//Lowest evel
void setup() {
pinMode (pin1, OUTPUT);// Set pins to output for controlling I/O
pinMode (pin2, OUTPUT);
pinMode (pin3, OUTPUT);
pinMode (pin4, OUTPUT);
pinMode (pin5, OUTPUT);
pinMode (pin6, OUTPUT);
}
void loop()
{ digitalWrite(pin1, LOW);//Resetting the LEDs to off state
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin5, LOW);
// Establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, in, cm; //‘in’ is inches and ‘cm’ is centimeter
// 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:
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);
// The same pin is used to read the signal from the PING: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
// Convert the time into a distance
in = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
delay(100);// To save battery,remove if felt inconvenient
if (in 《 6 * d / 7)// Else is included to light only one led at a level and thus save battery charge
digitalWrite(pin1, HIGH);
else if (in 《 5 * d / 6)
digitalWrite(pin2, HIGH);
else if (in 《 4 * d / 6)
digitalWrite(pin3, HIGH);
else if (in 《 3 * d / 6)
digitalWrite(pin4, HIGH);
else if (in 《 2 * d / 6)
digitalWrite(pin5, HIGH);
else if (in 《 1 * d / 6)
digitalWrite(pin5, HIGH);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax‘s datasheet for the PING, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second)。 This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PI.。.
return microseconds / 74 / 2;
}
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;
}
建立連接
按照附帶的Fritzing圖表在PCB或面包板上填充電路。這是在運(yùn)行Arduino的面包板上的ATMEga328。您可以按照Mayoogh Girish的教程在面包板上使用ATMega328制作您自己的Arduino板。如果您使用的是Arduino板,您可以按如下方式連接LED和超聲波傳感器。
上傳代碼
將Arduino水位指示器的代碼直接刻錄到Arduino板或ATMega328P微控制器上。
將超聲波傳感器連接到水箱上
固定Ping傳感器,使其直接面向水箱中的水。帶 指示LED的主控板可以在任何舒適的位置固定在家中。任何多芯 電纜(以太網(wǎng)電纜)都可用于連接Ping傳感器和 電路的其余部分。請記住,不要增加組件之間的長度 超過20mts。
現(xiàn)在只需連接電池,您的非接觸式Arduinowater水平指示器即可使用。
-
指示器
+關(guān)注
關(guān)注
0文章
249瀏覽量
38257 -
Arduino
+關(guān)注
關(guān)注
187文章
6464瀏覽量
186665
發(fā)布評(píng)論請先 登錄
相關(guān)推薦
評(píng)論