melodie-buzzer.ino

Arduino UNO R4 Minima · File : melodie-buzzer.ino · Language : Arduino · Use case : Jouer une mélodie sur buzzer passif avec tone()

A little melody on a passive buzzer to check tone() on the R4: the (frequency, duration) table plays in sequence. Also handy as an audible status signal in a screenless project.

Hardware

An Arduino UNO R4 Minima (Renesas RA4M1, Cortex-M4 @ 14 MHz, 5 V, 12-bit DAC, no radio) powered by USB or 6-24 V. 1 passive buzzer 3.3-5 V.

Wiring

Buzzer between D3 and GND. An active buzzer would only emit a fixed beep.

How it works

tone() sets an RA4M1 timer to the requested frequency; the loop plays the table note by note with its durations.

Running it

Arduino IDE 2.x (with the « Arduino UNO R4 Boards » core installed), pick « Arduino UNO R4 Minima », select the port and Upload. The serial monitor runs at 9600.

Variants

Store the melody in flash (PROGMEM), or trigger it on a reading (light threshold, button press).

melodie-buzzer.ino

// Melodie sur buzzer passif (D3) avec table de notes.
const int BUZZER = 3;

const int MELODIE[][2] = {
  {262, 200}, {330, 200}, {392, 200}, {523, 400},  // do mi sol do2
  {523, 150}, {494, 150}, {440, 300}, {392, 300},  // descente
  {0, 300},                                        // pause
  {349, 200}, {392, 200}, {440, 400}, {0, 400},
};

void setup() {
  Serial.begin(9600);
  Serial.println(F("Melodie"));
}

void loop() {
  for (auto &note : MELODIE) {
    if (note[0] == 0) {
      noTone(BUZZER);
    } else {
      tone(BUZZER, note[0]);
    }
    delay(note[1]);
  }
  delay(2500);
}

// tone() du core R4 utilise un timer du RA4M1 : la frequence
// est exacte et ne gene pas les autres broches PWM.

← Back to the board