电子说
Arduino水位指示器如何工作?
该Arduino水位指示器使用超声波传感器或Ping传感器来确定水箱中的水位。 Ping传感器使用声纳测量距离。从该单元发射超声波(远高于人类听觉)脉冲,并且通过测量回波返回所需的时间来确定到目标的距离。 Ping传感器的输出是可变宽度脉冲,对应于到目标的距离。然后将其输入微控制器,确定水位并通过一系列LED显示。
以下项目可以是如果您有一个或直接连接到面板上的ATmega 328微控制器,则可以连接到Arduino板。您还可以查看Jeff的Maker Pro教程,了解如何将超声波传感器连接到Arduino。
Arduino水位指示器代码
将提供的草图复制粘贴到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或面包板上填充电路。这是在运行Arduino的面包板上的ATMEga328。您可以按照Mayoogh Girish的教程在面包板上使用ATMega328制作您自己的Arduino板。如果您使用的是Arduino板,您可以按如下方式连接LED和超声波传感器。
上传代码
将Arduino水位指示器的代码直接刻录到Arduino板或ATMega328P微控制器上。
将超声波传感器连接到水箱上
固定Ping传感器,使其直接面向水箱中的水。带 指示LED的主控板可以在任何舒适的位置固定在家中。任何多芯 电缆(以太网电缆)都可用于连接Ping传感器和 电路的其余部分。请记住,不要增加组件之间的长度 超过20mts。
现在只需连接电池,您的非接触式Arduinowater水平指示器即可使用。
全部0条评论
快来发表一下你的评论吧 !