Use the piezoelectric loudspeaker in the SP&PIR Leaf to make the melody sound.

Leaf to use

Use the following leaves.

Type Name Q’ty
AP03 STM32 MCU 1
AV01 CR2032 1
AZ01 USB 1
AZ62 Connector Cover 1
CR2032 coin cell battery 1
M2*15mm screw 2


Let’s assemble leaves as shown in the figure below.


Source code

Open the Arduino, go to File → Sketch Examples → 02.Digital → toneMelody.

In this sample code, the speaker is supposed to be connected to pin number 8.
But as the loudspeaker is connected to pin 5 in Leafony, you should fix the following part to make it correct.1

  • Change the 8 to PB4 in line 37.
tone(PB4, melody[thisNote], noteDuration);
  • Change 8 to PB4 in line 44.

Execution Results

The melody sounds only once from the speakers.

Back to previous page

  1. The program differs from the AVR MUC leaf only in the designation of the pins. For more information, here. ↩︎

Last modified 15.03.2021