Experiment 7: Passive Buzzer Module Experiment

From Diustou Wiki

Arduino

Experimental Phenomenon

  • The buzzer sounds once every 1 second.
  • The frequency of the buzzer is controlled by a potentiometer.

Circuit Connection

  • 实验七:无源蜂鸣器实验 接线1.png

Reference Program

#define Pot A0
#define Buzzer 2

int PotBuffer = 0;

void setup()
{
  pinMode(Buzzer,OUTPUT);           // Set buzzer pin as output
}

void loop()
{
  PotBuffer = analogRead(Pot);      // Read ADC value
  
  for(int i = 0 ; i < 100 ; i++)    // Loop 100 times
  {
    digitalWrite(Buzzer,LOW);      // Set output to low 
    delayMicroseconds(PotBuffer);   // Delay for PotBuffer microseconds
    digitalWrite(Buzzer,HIGH);       // Set output to high
    delayMicroseconds(100);         // Delay for 100 microseconds
  }
  delay(1000);                      // Delay for 1000 milliseconds
}

Raspberry Pi

Circuit Connection

  • 实验七:无源蜂鸣器实验 接线2.png

Program Execution

Python

  • Install the gpiozero library
    • You can install the library using the following commands:
sudo apt update
sudo apt install python3-gpiozero
  • For other systems on the Raspberry Pi, you can install the library using:
sudo pip3 install gpiozero
  • Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
  • Download the Raspberry Pi reference examples, unzip the files, copy them to your user directory, and run:
cd raspberrypi/7/python_gpiozero
python Buzzer.py
  • You should now see the Raspberry Pi running the passive buzzer program correctly. To exit, press ctrl+C.
  • Command explanation: gpiozero.TonalBuzzer(pin, initial_value, mid_tone, octaves)
    • Main parameters:
      • pin: GPIO pin number,
      • initial_value:
        • If None (default), the buzzer's initial state is off.
        • You can specify a value between -1 and 1 as the buzzer's initial value.
      • mid_tone: Represents the pitch of the device's middle value (0). The default value is "A4".
      • octaves: Number of octaves different from the base pitch, with the default base note being A4.
        • If 1, it indicates one octave higher than the base note, i.e., A5.
        • If -1, it indicates one octave lower than the base note, i.e., A3.
  • For more commands, please refer to the gpiozero documentation

Example Download

Related Examples

Basic Experiment Kits and Examples for Arduino and Raspberry Pi
教程名2
  • 列表2
教程名3
  • 列表3
教程名4
  • 列表4
教程名5
  • 列表5
教程名6
  • 列表6