Project 4.00 Using the Piezo

In this project, you'll learn how to generate a simple sound using a Piezo buzzer!
This project shows how to use the MC Trainer to produce a noise from the piezo onboard when SW1 is pressed.

Project Code:

				
					/////////////////////////////////////////
// 4.00 - Using the Piezo

byte piezoPin = 12;

byte SW1 = 1;
bool pressed = LOW;

void setup() {
  pinMode(piezoPin, OUTPUT);
  pinMode(SW1, INPUT);
}

void loop() {
  if (digitalRead(SW1) == pressed) {
    digitalWrite(piezoPin, HIGH);
    delay(1);
    digitalWrite(piezoPin, LOW);
    delay(1);
  }
}
/////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

The piezo is connected to pin 12 on the MC Trainer.
				
					byte piezoPin = 12;
				
			
Since we are writing to this pin, it’s an output.
				
					pinMode(piezoPin, OUTPUT);
				
			

The piezo buzzer creates sound through the rapid physical deformation of an internal crystal. When a voltage is applied, the crystal changes shape. By quickly fluctuating the applied voltage, a sound is generated with a frequency that matches the rate of these voltage changes. Since we know how to turn a pin on and off (digitalWrite) we can create noise! To prevent a continuous annoying buzzing, we will only write to the Piezo when SW1 is pressed.

				
					  if (digitalRead(SW1) == pressed) {
    digitalWrite(piezoPin, HIGH);
    delay(1);
    digitalWrite(piezoPin, LOW);
    delay(1);
  }
				
			

Frequency, expressed in Hertz (Hz), indicates the number of cycles that occur per second. In the context of a piezo buzzer, understanding the frequency requires identifying the number of these cycles within a one-second timeframe. A “cycle”, in this case, is defined as one complete sequence of the buzzer turning “on” and then “off”. This sequence is also referred to as a “period”, marking the duration of one full cycle of operation.

In our case the period is 2 milliseconds. It takes 2 milliseconds to make a full cycle. How many of those cycles happen in one second? Remember, 1 second is 1000ms.

1000ms / 2ms = 500Hz

The noise you hear is at a frequency of 500Hz!

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.