Ультразвуковой датчик приближения на Arduino
Плата Arduino лежит в основе цепи датчика приближения при помощи популярного модуля HC-SR04. Описание модуля можете посмотреть по этой ссылке /modul-hc-sr04.html
Ультразвуковой модуль, используется здесь для бесконтактного обнаружения объекта. Модуль HC-SR04 включает ультразвуковые передатчики, приёмник и схему управления. Вам нужно всего лишь поставить короткий импульс на вход триггера, а затем модуль будет посылать ультразвук на частоте 40 кГц и будет повышаться его эхо.
Процесс настройки очень прост. Если всё подключено правильно, вы должны увидеть «горящий глаз» (то есть красный светодиод), когда нет объекта в зоне (в пределах 5 см от модуля HC-SR04), когда объект приближается появляется оглушительный звуковой сигнал от пьезо — зуммера. Этот проект можно использовать как охранное устройство.
Схема ультразвукового датчика Arduino
Скетч Arduino
- /*
- Project: Ultrasonic Proximity Sensor
- Sensor: HC-SR04
- Courtesy Note: Inspired by the Arduino Ping Sketch
- Tested At: TechNode Protolabz / June 2014
- */
- //Pins for HC-SR04
- constint trigPin =13;
- //Pin which delivers time to receive echo using pulseIn()
- int echoPin =12;
- int safeZone =5;
- // Pins for Indicators
- int statusLed =11;
- int pzBzr =10;
- void setup(){
- }
- void loop()
- {
- //raw duration in milliseconds, cm is the
- //converted amount into a distance
- long duration, cm;
- //initializing the pin states
- pinMode(trigPin, OUTPUT);
- pinMode(statusLed, OUTPUT);
- pinMode(pzBzr, OUTPUT);
- //sending the signal, starting with LOW for a clean signal
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- //setting up the input pin, and receiving the duration in uS
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- // convert the time into a distance
- cm = microsecondsToCentimeters(duration);
- //Checking if anything is within the safezone
- // If not, keep status LED on
- // Incase of a safezone violation, activate the piezo-buzzer
- if(cm > safeZone)
- {
- digitalWrite(statusLed, HIGH);
- digitalWrite(pzBzr, LOW);
- }
- else
- {
- digitalWrite(pzBzr, HIGH);
- digitalWrite(statusLed, LOW);
- }
- delay(100);
- }
- long microsecondsToCentimeters(long microseconds)
- {
- // The speed of sound is 340 m/s or 29 microseconds per centimeter
- // The ping travels forth and back
- // So to calculate the distance of the object we take half of the travel
- return microseconds /29/2;
- }