![]() ![]() We hope you have found this Ultrasonic Sensor Piezo Buzzer Circuit very useful. If the distance is more than 20 cm, the buzzer is turned off. For example, if the distance is less than 20 cm, the buzzer is turned on and the distance is printed to the serial monitor. Then an if-else statement is used to control the piezoelectric buzzer. In the loop() function, the distance() method is used to get the distance in cm from the sensor and store it in a variable. In the setup() function, the serial communication is initialized with a baud rate of 9600, and the pin mode of the piezoelectric buzzer is set to output. Then the trig and echo pins are defined, and an Ultrasonic object is created. The first step in the code is to include the necessary libraries, in this case, the Ultrasonic library, which allows the Arduino to interact with the HC-SR04 sensor. measure duration of pulse from ECHO pinĭigitalWrite(BUZZER_PIN, HIGH) // turn on Piezo BuzzerĭigitalWrite(BUZZER_PIN, LOW) // turn off Piezo Buzzer generate 10-microsecond pulse to TRIG pin PinMode(BUZZER_PIN, OUTPUT) // set arduino pin to output mode PinMode(ECHO_PIN, INPUT) // set arduino pin to input mode PinMode(TRIG_PIN, OUTPUT) // set arduino pin to output mode Serial.begin (9600) // initialize serial port 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |