buzzer-melodie.ino

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

A test melody for the passive buzzer: the Mbed core tone() rides a hardware timer, so the frequency stays true whatever the M7 load. The note table plays in a non-critical sequence.

Hardware

An Arduino Portenta H7 (ST STM32H747 dual-core module Cortex-M7 @ 480 MHz + M4 @ 240 MHz, 8 MB SDRAM, high-density connector), powered by USB-C or 5 V. 1 passive buzzer 3.3-5 V.

Wiring

Buzzer between D6 and GND. An active buzzer (built-in beep) cannot vary frequency.

How it works

tone() sets a timer to produce the square wave at the requested frequency; the loop reads the table (frequency, duration) note by note.

Running it

Arduino IDE with the « Arduino Mbed OS Portenta Boards » core: pick « Portenta H7 » (M7 core), the USB-C port, then Upload. The DFU mode M7/M4 selector also loads the M4 core.

Variants

Store the melody in PROGMEM/flash, or trigger it from the M4 over RPC as an event signal.

buzzer-melodie.ino

// Melodie sur buzzer passif (D6) : table de notes en flash.
const int BUZZER = 6;

const int MELODIE[][2] = {
  {523, 180}, {659, 180}, {784, 180}, {1046, 360},
  {784, 180}, {659, 180}, {523, 360},
  {0, 300},
  {587, 180}, {698, 180}, {880, 360}, {0, 400},
};

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

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 Mbed s'appuie sur un timer materiel : la
// frequence reste exacte meme a 240 MHz ou 480 MHz.

← Back to the board