Now that we’ve made a LED blink, let’s learn to use digital input and a piezo buzzer, along with multiple routes to ground.
If you did the extra challenges in Big Blinkie, you used a switch to interrupt the circuit and turn a light off and on. This time we’re going to monitor the switch in the program and have the program decide to turn a buzzer off and on.
This one has a lot of steps, I think.
If you happen to have a four-leg switch (lucky you!) you should definitely check out this tutorial, and maybe even this and this to learn about the piezo.
We’re going to be changing our code around a lot, but let’s start with this:
Serial
is a way of communicating between the Arduino and your computer. Serial.begin
tells the Arduino to create a connection during setup, and Serial.println
is used to print to the little screen at the bottom of the Arduino app.
Because we have weird switches, we need to do pinMode(switchPin, INPUT_PULLUP)
. We’ll explain a little bit more later.
digitalRead
is just like digitalWrite
, except we’re reading HIGH
and LOW
from the pin instead of writing it.
You’ll need the following:
First, set up a connection like this:
pin 2 ——> switch ——> GND
If you’d like to go step by step, you can use the following. And remember the row numbers don’t matter as long as you keep things that need to be connected in the same row.
So voltage will always be coming out of the 5V, going to the switch, and if you push the button it jumps on ahead to the GND.
Run your code. Nothing seems to happen, right? Turns out in order to see the output of Serial.println
we need to open the Serial Monitor.
You can open it by clicking the little magnifying glass icon in the upper right-hand side, or by going to Tools > Serial Monitor
.
Try pushing the button off and on a little bit, you should see it change between a stream of 1
s and a stream of 0
s.
Okay, so.
You’ll practically never use INPUT_PULLUP, it’s just because of our weird two-leg switches. Normal four-leg switches measure electricity’s ability to get to ground, giving you 1
a.k.a. HIGH
if something is pushed down, and 0
a.k.a. LOW
if it isn’t. You’ll notice our switch is the opposite - 0
if you’re pushed down, and 1
otherwise.
It uses something built into the Arduino called a pull-up resistor which I’m not going to try to explain because I don’t think I really understand it, myself.
What you need to know you can tell by reading the serial monitor - when it’s pushed down, it’s 0
, and when it’s not pushed down, it’s 1
.
Now it’s time to get annoying!
Add a new set of connections to your board,
pin 7 ——> piezo ——> GND
You’ll need to add three lines to your code.
First, up top you’ll define what pin you’re using for the speaker.
Then in setup()
you’ll want to set it to have OUTPUT
mode.
And in loop()
you’ll want to send it HIGH
when switchValue
is HIGH
a.k.a. 1
Push the button. Annoy everyone.
That buzzing sound is god-awful. It’s so bad. It’s what happens when you just send a HIGH
straight on through the buzzer.
First, let’s add a little resistor between pin 7 and the speaker to quiet it down. You’ll need to move the speaker and the ground wire down a few rows. Try a resistor that’s somewhere between 100 and 400 ohms.
Next, turns out you can use a function called tone to send specific tones to your arduino instead of just the buzzy noise.
Try editing your code to match this
Try changing 117
around a little - maybe some numbers between about 31 and 4978?
Turns out you’re sending a frequency to the piezo buzzer, making it buzz at a certain rate and creating musical notes! Every musical note has its own frequency, and you could use a chart like this to pick the ones you’re interested in.
Let’s try a couple changes. I’m just going to give you all the code at once.
Add long start
up to the very very top.
Now let’s make some cooler input - try out Lil’ Lightie