<h3> Buzzer Workshop </h3> <p> For this workshop, please collect the following: <ul> <li>Microcontroller </li> <li>protoboard</li> <li>Buzzer</li> </ul> </p> <h3> Buzzers </h3> <p> It's super easy to use piezo buzzers with Arduino's <code>tone()</code> function. Just wire up the buzzer to pin 10 and then to GND with a resistor. The <code>tone()</code> function needs 2 arguments, but can take three: <ul> <li> Pin# </li> <li> Frequency - this is in hertz (cycles per second) which determines the pitch of the noise made </li> <li> Duration (optional) - how long the tone plays. If you don't use the duration argument, you should have a <code>delay()</code> function following the tone instead. </li> </ul> Here's a simple example: </p> <img src='./buzzer_uno.png' alt='buzzer circuit'> <pre><code class="language-arduino"> int buzzerPin = 10; void setup() { pinMode(buzzerPin, OUTPUT); } void loop() { tone(buzzerPin, 1000, 500); delay(1000); } </code></pre> <p> Note: there have been some issues with the <code>tone()</code> function not working properly with some microcontrollers. In some cases, the <code>tone()</code> function causes the program to not compile. In other cases, it compiles fine but fails to produce sounds. If you experience problems, we recommend pasting the <code>myTone()</code> function below, and using it in the same way. So the above example would look like this: </p> <pre><code class="language-arduino"> int buzzerPin = 10; void setup() { pinMode(buzzerPin, OUTPUT); } void loop() { myTone(buzzerPin, 262, 500); delay(1000); } void myTone(int pin, int frequency, int duration){ int startTime = millis(); int period = 1000000/frequency; while ((millis() - startTime) < duration){ digitalWrite(pin, HIGH); delayMicroseconds(period/2); digitalWrite(pin, LOW); delayMicroseconds(period/2); } } </code></pre> <p> The <code>myTone()</code> function calculates the period of the wave that produces the desired frequency (in Hz, or cycles per second). Middle C is about 262 Hz. If we divide 1 million (the number of microseconds in a second) over 262 cycles per second, we get a period (or cycle) of about 3,817 microseconds. We want to turn our piezo on for half that time, then off for the remaining half, thereby making a square wave. In the above code, the square wave plays for half a second, then is silent for one second during the subsequent delay. </p> <p> Some things to keep in mind when using buzzers: <ul> <li>The `tone()` function uses one of the built in timers on the Arduino’s microcontoller. `tone()` works independently of the `delay()` function. You can start a tone and do other stuff – while the tone is playing in the background. Therefore, if you use 500 milliseconds as the third argument in `tone()`, and follow that by a delay of 1000 milliseconds, you will only be creating a “rest” of 500 milliseconds.</li> <li> You cannot generate a tone lower than 31 HZ. You can pass values 31 and less to the `tone()` function, but it doesn’t mean you will get a good representation of it. </li> <li> The `tone()` function cannot be used by two separate pins at the same time. Let’s say you have two separate piezo speakers, each on a different pin. You can’t have them both play at the same time. One has to be on, and then the other. Furthermore, before you can have the other pin use the `tone()` function, you must call the `noTone()` function and “turn off” the tone from the previous pin.</li> </ul> </p> <p> Being able to combine multiple example sketches into one is a very useful skill. See if you can combine the buzzer with a sketch you made for an input device like a potentiometer. </p> <p> Getting a buzzer to make noise is simple, but composing music is not. There are plenty of <a href='http://pages.mtu.edu/~suits/notefreqs.html'>reference tables for frequencies of musical notes</a>, but timing notes is also tricky. Check out some examples of classic tunes arranged for Arduino: <ul> <li> <a href='http://repairmypc.net/2017/08/test-post/'>Fur Elise</a></li> <li> <a href='https://gist.github.com/nicksort/4736535'>Star Wars Theme</a></li> <li> <a href='https://www.arduino.cc/en/Tutorial/PlayMelody'>Arduino Melody</a></li> <li> <a href='https://www.princetronics.com/supermariothemesong/'>Super Mario Theme</a></li> <li> <a href='https://github.com/robsoncouto/arduino-songs'>More tunes</a></li> </ul> </p> <p> For more information on sound generation with the Arduino, see this guide: <ul> <li> <a href='https://www.elektormagazine.com/files/attachment/331'>Arduino 8-bit Sound Generation</a></li> </ul> </p>