Experiment 6: Active Buzzer Module Experiment
From Diustou Wiki
Revision as of 17:30, 8 February 2025 by Yousimaier17 (talk | contribs) (Created page with "*Basic Experiment Kits For Arduino *Basic Experiment Kits For Raspberry Pi == Arduino == === Experimental Phenomenon === *When button S1 is pressed, the onboard LED l...")
Contents
Arduino
Experimental Phenomenon
- When button S1 is pressed, the onboard LED light turns on and the buzzer sounds; when button S1 is released, the onboard LED light turns off and the buzzer stops sounding.
Circuit Connection
Reference Program
#define LED 13 #define KEY 2 #define Buzzer 3 int KEY_NUM = 0; // Variable for key value void setup() { pinMode(LED,OUTPUT); // LED as output pinMode(KEY,INPUT); // Key as input pinMode(Buzzer,OUTPUT); // Buzzer as output digitalWrite(Buzzer,LOW); // Buzzer initially silent } void loop() { if(digitalRead(KEY) == HIGH) // Detect key press { delay(20); // Delay for debouncing if(digitalRead(KEY) == HIGH) { digitalWrite(LED,HIGH); // Turn LED on digitalWrite(Buzzer,HIGH); // Buzzer sounds while(digitalRead(KEY) == HIGH);// Wait for release } } else { digitalWrite(LED,LOW); // Turn LED off digitalWrite(Buzzer,LOW); // Buzzer silent } }
Raspberry Pi
Circuit Connection
Program Execution
Python
- Install the gpiozero library
- You can install the library using the following command:
sudo apt update sudo apt install python3-gpiozero
- For other systems on the Raspberry Pi, you can install the library using the following command:
sudo pip3 install gpiozero
- Run the following command to see the GPIO pin definitions on the Raspberry Pi:
pinout
- Download the Raspberry Pi reference example, unzip the file, copy it to your user directory, and run:
cd raspberrypi/6/python_gpiozero python Buzzer.py
- At this point, you will see the Raspberry Pi running the active buzzer program correctly, with the buzzer sounding every 0.5 seconds. To exit, press ctrl+C.
- Command description: gpiozero.Buzzer(pin, active_high, initial_value)
- Main parameters:
- pin: GPIO pin number,
- active_high: Setting for internal pull-up/pull-down resistors,
- When set to True (default), connect the negative terminal of the buzzer to GND and the other terminal to the GPIO pin.
- When set to False, connect the negative terminal of the buzzer to the GPIO pin and the other terminal to the 3.3V pin.
- initial_value:
- If False (default), the buzzer starts off.
- If None, the buzzer's connected pin is in an unstable state.
- If True, the buzzer starts on.
- Main parameters:
- For more commands, please refer to the gpiozero documentation