使用arduino和超声波hc-sr04传感器进行速度测量?

时间:2014-01-20 13:02:46

标签: arduino sensor arduino-ide

我希望使用Arduino和两个超声波hc-sr04 like this link进行速度检测“设备”。但我想用超声波代替LDR。

来自该链接。如何激光和ldr工作,像这样

  

电阻器用作下拉电阻器,我将传感器接线并放入外壳中,以避免它们检测周围的光线。对于每种情况,钻一个孔,以便激光束可以照亮传感器,而环境光不会影响传感器。   工作原理很简单:经过的物体将“切割”激光束,这意味着LDR传感器将检测到这种突然的光强度下降。首先,我定义了一个阈值,在该阈值下传感器被认为是触发的,一旦该值低于第一个传感器的阈值,则Arduino等待第二个触发器被触发。在此等待时间内,它计算两个事件之间经过的时间。当第二个光束中断时,计时器停止,现在只是简单的数学运算。两个传感器之间的距离是已知的,两个事件之间的时间是已知的,速度可以计算为速度=距离/时间。

Arduino代码下方:

/* 
by Claudiu Cristian 
*/ 

unsigned long time1; 
int photocellPin_1 = 0; // 1st sensor is connected to a0 
int photocellReading_1; // the analog reading from the analog port 
int photocellPin_2 = 1; // 2nd sensor is connected to a1 
int photocellReading_2; // the analog reading from the analog port 
int threshold = 700; //value below sensors are trigerd 
float Speed; // declaration of Speed variable 
float timing; 
unsigned long int calcTimeout = 0; // initialisation of timeout variable 

void setup(void) { 
// We'll send debugging information via the Serial monitor 
Serial.begin(9600); 
} 

void loop(void) { 
photocellReading_1 = analogRead(photocellPin_1); //read out values for sensor 1 
photocellReading_2 = analogRead(photocellPin_2); //read out values for sensor 2 
// if reading of first sensor is smaller than threshold starts time count and moves to             calculation function 
if (photocellReading_1 < threshold) { 
time1 = millis(); 
startCalculation(); 
} 
} 

// calculation function 
void startCalculation() { 
calcTimeout = millis(); // asign time to timeout variable 
//we wait for trigger of sensor 2 to start calculation - otherwise timeout 
while (!(photocellReading_2 < threshold)) { 
photocellReading_2 = analogRead(photocellPin_2); 
if (millis() - calcTimeout > 5000) return; 
} 
timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds 
Speed = 0.115 / timing; //speed in m/s given a separation distance of 11.5 cm 
delay(100); 
Serial.print(Speed); 
Serial.print("\n"); 
} 

如何使用超声波HC-SR04传感器实现代码? 编码对我来说是个问题。希望有人可以帮助我...... :( 请原谅我可怜的英文!

1 个答案:

答案 0 :(得分:1)

互联网上已经有很多例子,所以如果你想做的就是复制,谷歌arduino sr04

但如果你想知道怎么做...... sr04有4个引脚,vin,gnd,trigger和echo。 将vin和地面连接到+5和gnd 将触发器连接到数字输出引脚 将回声连接到数字输入引脚

触发低电平2微秒(us)然后再高电平10 us然后再低电平 然后使用echo引脚中的pulseIn获得结果

阅读数据表以获取更多信息