
In this project an obstacle detection system will be built with Arduino. This system mainly uses an ultrasound sensor, buzzer and LED.
When the ultrasonic sensor detects an obstacle at a distance < 3cm, the Arduino orders the buzzer to ring and the red LED to light up.
To make the assembly, we can connect :
For LED and buzzer:
For HC-SR04 sensor:
or the LCD display:
the SDA pin at the Arduino analog pin A4.
the SCL pin at the Arduino analog pin A5.
the VCC pin at the Arduino 5v pin.
the GND pin at the Arduino GND pin
The Obstacle Detection System program is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <HCSR04.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 20, 4); const int LEDBuzzerPin=1; const int trigPin = 2; const int echoPin = 3; UltraSonicDistanceSensor distanceSensor(trigPin, echoPin); void setup(){ lcd.init(); pinMode(LEDBuzzerPin,OUTPUT); } void loop(){ lcd.backlight(); lcd.clear(); lcd.print("distance = "); lcd.setCursor(0,1); lcd.print(distanceSensor.measureDistanceCm()+1); lcd.print(" cm"); if((distanceSensor.measureDistanceCm()+1)<3) { digitalWrite(LEDBuzzerPin,HIGH); // red LED lights up and buzzer starts ringing }else { digitalWrite(LEDBuzzerPin,LOW); // red LED goes out and buzzer stops } delay(500); } |