ccarman602

Piezo Speaker Siren

Jun 3rd, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Connect the positive side of the buzzer to pin 3 on the Arduino using a breadboard,
  3. then connect the negative side of the piezo to a 1k resistor. Finally, connect the other side
  4. of the 1k resistor to one of the ground (GND) pins on the Arduino.
  5. */
  6.  
  7. #define buzzerPin 3
  8. #define minFreq 30
  9. #define maxFreq 10000
  10.  
  11. void setup() {
  12. }
  13.  
  14. void loop() {
  15.   // siren goes up
  16.   for (int i = minFreq; i <= maxFreq; i++) {
  17.     tone(buzzerPin, i);
  18.     delay(1);
  19.   }
  20.  
  21.   // siren goes down
  22.   for (int i = maxFreq; i >= minFreq; i--) {
  23.     tone(buzzerPin, i);
  24.     delay(1);
  25.   }
  26.  
  27.   noTone(buzzerPin);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment