Arduino Basics: Big Buzzy

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.

Your code

We’re going to be changing our code around a lot, but let’s start with this:

// You'll install the switch on pin 2:
int switchPin = 2;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop() {
  int switchValue = digitalRead(switchPin);
  Serial.println(switchValue);
}

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.

Your setup

You’ll need the following:

  • A momentary switch is a button that can be used to turn the flow of electricity off and on inside of a circuit. The ones we’re using today have two legs which are a little different than the ones you usually get with Arduino (4 pins), so other guides might not work out.
  • A piezo is a little buzzer that you’ll find incredibly delightful and everyone around you will find incredibly annoying.

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.

  1. Connect a wire from pin 2 into row 1.
  2. Have a switch join row 1 with row 3.
  3. Connect a black wire between row 3 and GND.

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.

The Serial monitor

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 1s and a stream of 0s.

INPUT_PULLUP

Okay, so.

pinMode(switchPin, INPUT_PULLUP);

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.

Buzzing off

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.

int speakerPin = 7;

Then in setup() you’ll want to set it to have OUTPUT mode.

pinMode(speakerPin, OUTPUT);

And in loop() you’ll want to send it HIGH when switchValue is HIGH a.k.a. 1

if(switchValue == HIGH) {
  digitalWrite(speakerPin, LOW);
} else {
  digitalWrite(speakerPin, HIGH);
}

Push the button. Annoy everyone.

Using tone

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

if(switchValue == HIGH) {
  noTone();
} else {
  tone(speakerPin, 117);
} 

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.

Looking to the past

Let’s try a couple changes. I’m just going to give you all the code at once.

int switchPin = 2;
int speakerPin = 7;
int lastSwitchValue = HIGH;
int frequency = 31;

void setup() {
  Serial.begin(9600);
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  // read from switchPin to see if it was successful
  int switchValue = digitalRead(switchPin);
  
  if(switchValue == HIGH) {
    // button is up
    noTone(speakerPin);
  } else {
    // button is down

    // if last loop it was HIGH, but now it's
    // LOW, it was just pressed, so double the
    // frequency
    if(lastSwitchValue == HIGH) {
      frequency = frequency * 2;
    }
    // write the current frequency to the serial monitor
    Serial.println(frequency);
  
    tone(speakerPin, frequency);
  } 
  // store the last switch value
  lastSwitchValue = switchValue;
}

Looking to the Past v2

Add long start up to the very very top.

if(lastSwitchValue == HIGH) {
  // store the time (in ms) that we started
  // pushing the button
  start = millis();
}

// compute how long we've held it down
int duration = millis() - start;
frequency = duration;

Next steps

Now let’s make some cooler input - try out Lil’ Lightie

Want to hear when I release new things?
My infrequent and sporadic newsletter can help with that.