Experiment 20: SW-18010P Vibration Sensor Experiment
From Diustou Wiki
Contents
Arduino
Experimental Phenomenon
- When vibration is detected by the module, the onboard LED lights up; when no vibration is detected, the onboard LED turns off.
Circuit Connection
Reference Program
int hzPin = 2; int ledPin = 13; int hzState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(hzPin, INPUT); } void loop(){ hzState = digitalRead(hzPin); if(hzState == LOW) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
Raspberry Pi
Circuit Connection
Program Execution
Python
- Install the gpiozero library
- You can use the following command to install the library:
sudo apt update sudo apt install python3-gpiozero
- For other systems on the Raspberry Pi, you can use the following command to install the library:
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 it:
cd raspberrypi/20/python_gpiozero python sensor.py
- At this point, you can see the Raspberry Pi running the program correctly. 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; alternatively, one end of the button is connected to the 3V3 pin, and the other end is connected to any GPIO pin, then set pull_up to False in the Button's initialization method.
- 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 unstable signals within about 20ms, known as "switch bounce".
- When set to None, software debounce compensation is not performed; otherwise, this parameter is the length of time (in seconds) that the component ignores after an 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: gpiozero.LED(pin, pwm, active_high, initial_value)
- Main parameters:
- pin: GPIO pin number,
- active_high:
- When set to True (default), connect the negative lead of the LED to GND and the other end to the GPIO pin.
- When set to False, connect the negative lead of the LED to the GPIO pin and the other end to the 3.3V pin.
- initial_value:
- If False (default), the LED initial state is off.
- If None, the LED pin is in an unstable state.
- If True, the LED initial state is on.
- Main parameters:
- For more commands, please refer to the gpiozero documentation