const int TRIG_PIN = 6 // Arduino pin connected to Ultrasonic Sensor's TRIG pinĬonst int ECHO_PIN = 7 // Arduino pin connected to Ultrasonic Sensor's ECHO pinĬonst int BUZZER_PIN = 3 // Arduino pin connected to Piezo Buzzer's pinĬonst int DISTANCE_THRESHOLD = 50 // centimeters Now copy the following code and upload it to Arduino IDE Software. Here is a simple step-by-step guide on “ How to install Arduino IDE“. Wiring / Connections Arduinoįirst, you need to install Arduino IDE Software from its official website Arduino. Make connections according to the circuit diagram given below. If the distance is less than 20cm the piezoelectric buzzer will be turned on and if the distance is more than 20cm it will be turned off. Upload the code to the Arduino board, open the serial monitor, and set the baud rate to 9600, you should see the distance in centimeters being printed every 100ms. Use Serial.flush() to clear the serial buffer, and delay(100) to prevent the program from sending too many requests to the sensor at once.ġ0.Serial.println("Distance: " + String(distance) + " cm") For example, if the distance is less than 20 cm, turn on the buzzer and print the distance to the serial monitor. Use an if-else statement to control the piezoelectric buzzer.In the loop() function, use the distance() method to get the distance in cm and store it in a variable.In the setup() function, initialize the serial communication and set the pin mode of the piezoelectric buzzer to output.Define the trig and echo pins, and create an Ultrasonic object. In the Arduino IDE, create a new sketch and include the following library:.Connect the positive pin of the piezoelectric buzzer to pin 9 on the Arduino and the negative pin to GND.Connect the TRIG pin of the sensor to pin 12 on the Arduino and the ECHO pin to pin 11.Connect the GND pin of the sensor to GND on the Arduino. Connect the VCC pin of the HC-SR04 sensor to the 5V pin of the Arduino Uno.Ultrasonic Sensor Piezo Buzzer with Arduino
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |