Experiment 7: Passive Buzzer Module Experiment
From Diustou Wiki
Revision as of 11:59, 11 February 2025 by Yousimaier17 (talk | contribs) (Created page with "*Basic Experiment Kits For Arduino *Basic Experiment Kits For Raspberry Pi == Arduino == === Experimental Phenomenon === * The buzzer sounds once every 1 second. * The...")
Contents
Arduino
Experimental Phenomenon
- The buzzer sounds once every 1 second.
- The frequency of the buzzer is controlled by a potentiometer.
Circuit Connection
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
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.
- Main parameters:
- For more commands, please refer to the gpiozero documentation