Difference between revisions of "Experiment 6: Active Buzzer Module Experiment"

From Diustou Wiki
(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...")
 
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
  
 
=== Circuit Connection ===
 
=== Circuit Connection ===
*[[File:实验六:有源蜂鸣器模块实验_接线1.png|400px]]
+
*[[File:实验六:有源蜂鸣器模块实验_接线1.png|800px]]
  
 
=== Reference Program ===
 
=== Reference Program ===

Latest revision as of 14:01, 11 February 2025

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

  • 实验六:有源蜂鸣器模块实验 接线1.png

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

  • 实验六:有源蜂鸣器模块实验 接线2.png

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.

Example Download

Related Examples

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