Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. // Today I learned that if you are sloppy with your parts bin
  2. // organization, and mean to grab a piezo speaker and instead grab a
  3. // piezo buzzer with a built-in oscillator, you get an interesting
  4. // spectrum from a frequency sweep. The sum-and-difference signals
  5. // and their harmonics make neat patterns in a spectrogram!
  6. //
  7. // This originally was supposed to use a piezo speaker, so the
  8. // amplifier below is based around that. However, I grabbed the wrong
  9. // part; I grabbed a buzzer with an internal 2 kHz oscillator, similar
  10. // to Adafruit's part 1536. (1536 is 5v tolerant, but I meant to grab
  11. // a 3.3v part, so I'm sourcing from 3.3v in the amplifier.)
  12. //
  13. // FWIW, my buzzer uses about 20mA at 3.3v and 25mA at 5v. So you
  14. // could hook it up straight to an Arduino pin, but it's a bit hot for
  15. // my taste.
  16. //
  17. // Here's the amplifier I used:
  18. //
  19. // +3.3v (from Arduino)
  20. // ^
  21. // |
  22. // *------+
  23. // + | ---
  24. // [Piezo ] ^ Diode (1N4148 or whatever)
  25. // [buzzer] /_\
  26. // | |
  27. // *------+
  28. // |
  29. // |
  30. // >
  31. // 220 or so | /
  32. // pin 11 ----/\/\/\-----|< NPN transistor (2N2222 or whatever)
  33. // | \
  34. // \
  35. // |
  36. // -----
  37. // ---
  38. // -
  39.  
  40. // Hook up a buzzer to digital pin 11.
  41. const int buzzer_pin = 11;
  42.  
  43. // You can hook a button between digital pin 5 and ground to replay
  44. // the sound, or touch a wire briefly between pin 5 and ground, or
  45. // just hit the Reset button on the Arduino.
  46. const int button_pin = 5;
  47.  
  48. void beep()
  49. {
  50. digitalWrite(buzzer_pin, HIGH);
  51. delay(100);
  52. digitalWrite(buzzer_pin, LOW);
  53. delay(100);
  54. }
  55.  
  56. void setup() {
  57. pinMode(button_pin, INPUT);
  58. digitalWrite(button_pin, HIGH);
  59. pinMode(buzzer_pin, OUTPUT);
  60. }
  61.  
  62. void loop()
  63. {
  64. beep();
  65. beep();
  66. digitalWrite(buzzer_pin, HIGH);
  67. delay(1000);
  68. digitalWrite(buzzer_pin, LOW);
  69. delay(1500);
  70. for (float freq = 60; freq < 20000; freq *= 1.008) {
  71. tone(buzzer_pin, (long)freq);
  72. delay(10);
  73. }
  74. noTone(buzzer_pin);
  75. delay(1500);
  76. beep();
  77. beep();
  78. while (digitalRead(button_pin))
  79. ;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement