Experiment 4: Button-Controlled LED Experiment

From Diustou Wiki

Arduino

Experimental Phenomenon

  • Press button S1, and the onboard LED light will turn on; release the button, and the LED will turn off.

Circuit Connection

  • 实验四:按键控制LED实验 接线.png

Reference Program

  • The module contains a pull-down resistor, so detecting if the button is pressed means detecting a high level.
int Led=13;                             //Define LED interface
int KEY=3;                              //Define button switch sensor interface

void setup()
{
  pinMode(Led,OUTPUT);                  //// Define LED as output interface
  pinMode(KEY,INPUT);
  digitalWrite(Led,LOW);
}
void loop()
{
  if( digitalRead(KEY) == HIGH )        //Check if the button is pressed
  {
    delay(20);                          // Delay 20ms for debounce 
    if( digitalRead(KEY) == HIGH )      //Check if the button is pressed
    {
      digitalWrite(Led,HIGH);
      while(digitalRead(KEY) == HIGH);  //Detect release 
    }
  }
  else  
    digitalWrite(Led,LOW);
}

Raspberry Pi

Circuit Connection

  • 实验四:按键控制LED实验 接线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 Raspberry Pi, you can install the library using the following command:
sudo pip3 install gpiozero
    • Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
  • Download the Raspberry Pi reference example, unzip the file, copy it to the user directory, and run:
cd /home/pi/raspberrypi/4/python_gpiozero
python button.py
  • At this point, pressing button S1 will turn on the red light, releasing it will turn off the red light; pressing button S2 will turn on the yellow light, releasing it will turn off the yellow light; pressing button S3 will turn on the green light, releasing it will turn off the green light; pressing button S4 will turn on all three lights, releasing it will turn off all three lights. To exit, press ctrl+C.
  • Command description: gpiozero.Button(pin, pull_up, active_state, bounce_time, hold_time, hold_repeat)
    • Button inherits from DigitalInputDevice and represents a simple button or switch. One end of the button is connected to the ground, and the other end is connected to any GPIO pin; or one end of the button is connected to the 3V3 pin, and the other end is connected to any GPIO pin, and then set pull_up to False in the Button's initialization constructor.
    • Main parameters:
      • pin: GPIO pin number;
      • pull_up: Internal pull-up/pull-down resistor setting,
        • When set to True (default), the GPIO pin is pulled high, and the other end of the button should be connected to ground.
        • When set to False, the GPIO pin is pulled low, and the other end of the button should be connected to 3V3.
        • When set to None, the GPIO pin is floating, and gpiozero cannot guess the active state, so active_state must be set.
      • active_state:
        • When set to True, the software pin state is also "high" when the hardware pin state is "high".
        • When set to False, the input polarity is reversed, and the software pin state is "low" when the hardware pin state is "high".
        • When pull_up is set to None, use this parameter to set the unknown pin active state.
        • When pull_up is set to True or False, the pin's active state is automatically assigned.
      • bounce_time: Software debounce time. Generally, a switch has an unstable signal for about 20ms, known as "switch bounce".
        • When set to None, no software debounce compensation is performed; otherwise, this parameter is the length of time (in seconds) that the component ignores after the initial change, with a default of 1s.
      • hold_time: The time after pressing the button until when_held is triggered, in seconds.
      • hold_repeat:
        • If True, when_held will continue to be triggered every hold_time seconds as long as the button remains pressed.
        • If False, when_held will only be triggered once.
  • Command description: LEDBoard(pin, pwm, active_high, initial_value)
    • Main parameters:
      • pin: GPIO pin number,
      • pwm:
        • If True, constructs a PWMLED instance for each pin.
        • If False (default), constructs a regular LED instance.
      • active_high: Internal pull-up/pull-down resistor setting,
        • When set to True (default), on() sets the pin to High, and off() sets the pin to LOW.
        • When set to False, on() sets the pin to LOW, and off() sets the pin to High.
      • initial_value:
        • If False (default), all LEDs are initially off.
        • If "None", all LEDs are initially in an unstable state.
        • If True, all LEDs are initially 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