User Tools

Site Tools


oak:tutorials:buzzer

Oak: Making Noise with the Buzzer

This lesson will show you how to connect the 3.3v Active Buzzer to your Oak and make it beep morse code.

Components Used:

Part Quantity Identification
Oak with soldered headers 1
Breadboard 1
3.3v Active Buzzer 1
M to M 30cm Jumper Wire 2 Red, Black

Concepts:

Piezo Buzzers: In this lesson we will be using a Piezo buzzer to make sound. We will be achieving this by turning it on and off, much like an LED.

A piezo buzzer

A piezo buzzer is a sandwich of two different conductive metals. When voltage is applied to the two different metals it bends, creating waves in air - which we call sound. They are usually small and circular and can be found in anything that provides an annoying beep.

We can make a piezo buzzer beep like this in code:

 // turn pin 1 "on" by making the voltage HIGH)
  digitalWrite(1, HIGH);

Where digitalWrite(1, HIGH); means turn Pin1 fully on. Don't forget to turn it off again!

Circuit:

Buzzer

Remember that when connecting a buzzer, you must pay attention to the (+) sign on the top of the buzzer casing. The (+) sign represents the positive connector and is where the red wire from pin 2 must go. If you get this backwards your buzzer may stop working.

Code:

//set the buzzer pin
int buzzer = 2;

// the setup function runs once when you reset or power the board
void setup() {
  // initialize buzzer as an output.
  pinMode(buzzer, OUTPUT);
  
}

// the loop function runs over and over again forever
void loop() {
  // make 3 dots to make an S
  for (int a = 0; a < 3; a++) {
    dot();
  }
  // wait 100 miliseconds after the first S
  delay(100);
  // make 3 dashes to make an o
  for (int b = 0; b < 3; b++) {
    dash();
  }
  // wait 100 miliseconds after the o
  delay(100);
  // make 3 dots to make an S
  for (int c = 0; c < 3; c++) {
    dot();
  }
  //wait 5 seconds before playing again
  delay(5000);
}

// make a dot noise
void dot()
{
  //turn on the buzzer
  digitalWrite(buzzer, HIGH);
  delay(100);
  // turn off the buzzer
  digitalWrite(buzzer, LOW);
  delay(100);  
}

// make a dash noise
void dash()
{
  // turn on the buzzer
  digitalWrite(buzzer, HIGH);
  delay(300);
  // turn off the buzzer
  digitalWrite(buzzer, LOW);
  delay(100);
}

In this example, we create two separate functions dot() and dash() and use them in for loops to save us having to write the same code over and over again. We also use a variable buzzer to declare which pin we are using for the buzzer.

dot() and dash() turn on and off the buzzer with different timings to simulate a morse code dot and dash using the delay() function which tells the Oak to pause for that many milliseconds.

Conclusion:

With this newfound power, perhaps you could make an internet connected morse code system to read out important tweets or secret messages from your friends.

oak/tutorials/buzzer.txt · Last modified: 2016/03/25 23:42 by nog3