Nota

Ciao, benvenuto nella Community di SunFounder per appassionati di Raspberry Pi, Arduino e ESP32 su Facebook! Approfondisci Raspberry Pi, Arduino ed ESP32 insieme ad altri appassionati.

Perché unirsi?

  • Supporto Esperto: Risolvi problemi post-vendita e sfide tecniche con l’aiuto della nostra comunità e del nostro team.

  • Impara e Condividi: Scambia suggerimenti e tutorial per migliorare le tue competenze.

  • Anteprime Esclusive: Accedi in anteprima agli annunci dei nuovi prodotti e alle anticipazioni.

  • Sconti Speciali: Godi di sconti esclusivi sui nostri prodotti più recenti.

  • Promozioni e Giveaway Festivi: Partecipa a giveaway e promozioni festive.

👉 Pronto a esplorare e creare con noi? Clicca su [Qui] e unisciti oggi!

Buzzer Passivo

Panoramica

In questo progetto, utilizzeremo queste due funzioni per far vibrare il buzzer passivo e produrre suono. La funzione tone() genera un’onda quadra con una frequenza specificata (e ciclo di lavoro del 50%) su un pin. Può essere specificata una durata, oppure l’onda continua finché non viene chiamata noTone(). Simile al buzzer attivo, anche il buzzer passivo utilizza l’induzione elettromagnetica per funzionare. La differenza è che un buzzer passivo non ha una propria fonte oscillante, quindi non emette suono se vengono utilizzati segnali DC. Tuttavia, questo permette al buzzer passivo di regolare la propria frequenza di oscillazione e produrre note diverse come «do, re, mi, fa, sol, la, si».

Componenti Necessari

In questo progetto, abbiamo bisogno dei seguenti componenti.

È sicuramente conveniente acquistare un kit completo, ecco il link:

Nome

ELEMENTI IN QUESTO KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

Puoi anche acquistarli separatamente dai link seguenti.

INTRODUZIONE DEI COMPONENTI

LINK PER L’ACQUISTO

Arduino Uno R4 WiFi

-

Breadboard

ACQUISTA

Cavi Jumper

ACQUISTA

Resistenza

ACQUISTA

Transistor

ACQUISTA

Cicalino

ACQUISTA

Cablaggio

Nota

Quando colleghi il buzzer, assicurati di controllare i suoi pin. Il pin più lungo è l’anodo e quello più corto è il catodo. È importante non confonderli, poiché in tal caso il buzzer non produrrà alcun suono.

../_images/16-passive_buzzer_bb.png

Schema Elettrico

../_images/16_passive_buzzer_schematic.png

Codice

Nota

  • Puoi aprire direttamente il file 16-passive_buzzer.ino nel percorso elite-explorer-kit-main\basic_project\16-passive_buzzer.

  • Oppure copia questo codice nell’Arduino IDE.

pitches.h
/*****************
Public Constants
*****************/
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 49
16-passive_buzzer.ino
 1/*
 2  This code plays a melody on a passive buzzer module connected to an Arduino Uno board. 
 3  The melody is played once in the setup() function and consists of eight notes with 
 4  different durations.
 5  
 6  Board: Arduino Uno R4
 7  Component: Passive buzzer module
 8*/
 9
10#include "pitches.h"
11
12const int buzzerPin = 9;
13
14// notes in the melody:
15int melody[] = {
16  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
17};
18
19// note durations: 4 = quarter note, 8 = eighth note, etc.:
20int noteDurations[] = {
21  4, 8, 8, 4, 4, 4, 4, 4
22};
23
24void setup() {
25  // iterate over the notes of the melody:
26  for (int thisNote = 0; thisNote < 8; thisNote++) {
27    // to calculate the note duration, take one second divided by the note type.
28    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
29    int noteDuration = 1000 / noteDurations[thisNote];
30    tone(buzzerPin, melody[thisNote], noteDuration);
31    // to distinguish the notes, set a minimum time between them.
32    // the note's duration + 30% seems to work well:
33    int pauseBetweenNotes = noteDuration * 1.30;
34    delay(pauseBetweenNotes);
35    // stop the tone playing:
36    noTone(buzzerPin);
37  }
38}
39
40void loop() {
41  // no need to repeat the melody.
42}

Al termine del caricamento del codice sulla scheda R4, sentirai una melodia composta da sette note.

Analisi del Codice

  1. Inclusione della libreria pitches: Questa libreria fornisce i valori di frequenza per varie note musicali, permettendoti di usare la notazione musicale nel tuo codice.

    Nota

    Per favore, posiziona il file pitches.h nella stessa directory del codice per garantirne il corretto funzionamento. pitches.h

    ../_images/16_passive_buzzer_piches.png
    #include "pitches.h"
    
  2. Definizione di costanti e array:

    • buzzerPin è il pin digitale sull’Arduino a cui è collegato il buzzer.

    • melody[] è un array che memorizza la sequenza delle note da suonare.

    • noteDurations[] è un array che memorizza la durata di ciascuna nota nella melodia.

    const int buzzerPin = 8;
    int melody[] = {
      NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
    };
    int noteDurations[] = {
      4, 8, 8, 4, 4, 4, 4, 4
    };
    
  3. Esecuzione della melodia:

    • Il ciclo for itera su ciascuna nota della melodia.

    • La funzione tone() suona una nota sul buzzer per una durata specifica.

    • Viene aggiunto un ritardo tra le note per distinguerle.

    • La funzione noTone() interrompe il suono.

    void setup() {
      for (int thisNote = 0; thisNote < 8; thisNote++) {
        int noteDuration = 1000 / noteDurations[thisNote];
        tone(buzzerPin, melody[thisNote], noteDuration);
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        noTone(buzzerPin);
      }
    }
    
  4. Funzione loop vuota: Poiché la melodia viene suonata solo una volta nel setup, non c’è codice nella funzione loop.

  5. Sentiti libero di sperimentare modificando le note e le durate negli array melody[] e noteDurations[] per creare le tue melodie. Se sei interessato, c’è un repository GitHub (robsoncouto/arduino-songs ) che offre codice Arduino per suonare varie canzoni. Sebbene il loro approccio possa differire da questo progetto, puoi consultare le loro note e durate come riferimento.