CIPAD Leçon 48 4e partie: la fonction tone() et buzzer piezo

3 min read 3 months ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to help beginners understand how to use the tone() function with a piezo buzzer in Arduino programming. This lesson builds on basic programming concepts and is relevant for anyone looking to create sound effects or music in their Arduino projects.

Step 1: Setting Up Your Hardware

To get started, you will need the following components:

  • Arduino board (e.g., Arduino Uno)
  • Piezo buzzer
  • Jumper wires
  • Breadboard (optional)

Connections

  1. Connect the positive terminal of the piezo buzzer to a digital pin on the Arduino (e.g., pin 8).
  2. Connect the negative terminal of the piezo buzzer to the GND pin on the Arduino.

Step 2: Writing Your First Code

Now that your hardware is set up, it’s time to write the code to generate sound using the tone() function.

Basic Code Structure

Use the following basic structure for your Arduino sketch:

void setup() {
  // Set the pin mode for the buzzer
  pinMode(8, OUTPUT);
}

void loop() {
  // Generate a sound at 440 Hz for 500 milliseconds
  tone(8, 440, 500);
  delay(1000); // Wait for 1 second
}

Explanation of the Code

  • pinMode(8, OUTPUT): Sets pin 8 as an output for the buzzer.
  • tone(8, 440, 500): Generates a tone on pin 8 at 440 Hz for 500 milliseconds.
  • delay(1000): Pauses the program for 1 second before repeating the loop.

Step 3: Experimenting with Different Frequencies

To explore sound generation, modify the frequency and duration in your code.

Examples

  • To play a higher pitch, change the frequency to 880 Hz:
    tone(8, 880, 500);
    
  • To extend the duration, change the third parameter to 1000 milliseconds:
    tone(8, 440, 1000);
    

Step 4: Stopping the Tone

You can stop the sound using the noTone() function. Add this to your code:

void loop() {
  tone(8, 440, 500);
  delay(1000);
  noTone(8); // Stop the tone
  delay(1000); // Wait for 1 second before next tone
}

Step 5: Creating a Melody

You can create a simple melody by defining an array of notes and durations. Here’s an example:

Melody Code

int melody[] = { 262, 294, 330, 349, 392, 440, 494, 523 }; // Frequencies for C4 to C5
int noteDurations[] = { 4, 4, 4, 4, 4, 4, 4, 4 }; // Durations

void setup() {
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);
    delay(noteDuration); // Wait for the note to finish playing
    noTone(8); // Stop the tone
  }
}

void loop() {
  // Empty loop
}

Explanation

  • melody[]: Array of frequencies.
  • noteDurations[]: Array of durations corresponding to each note.
  • The loop iterates through the notes, playing each one for its specified duration.

Conclusion

In this tutorial, you learned how to use the tone() function with a piezo buzzer in Arduino. You set up your hardware, wrote basic code to generate sounds, experimented with different frequencies, and even created a simple melody.

As a next step, consider exploring how to integrate this sound functionality into larger projects, such as creating alarms or interactive games. Happy coding!