Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
4.1.1 Magnetic Induction Alarm System¶
Introduction¶
When you get a precious vase, you can make a magnetic induction alarm system for it, no matter who moves it, you can hear the alarm in time.
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
Schematic Diagram¶
T-Board Name |
physical |
wiringPi |
BCM |
GPIO17 |
Pin 11 |
0 |
17 |
GPIO27 |
Pin 13 |
2 |
27 |
Experimental Procedures¶
Step 1: Build the circuit.
Step 2: Get into the folder of the code.
cd ~/raphael-kit/python-pi5
Step 3: Run.
sudo python3 4.1.6_MagneticAlarmSystem_zero.py
If the reed switch is affected by the magnet (for example, the reed switch is placed on the base and the magnet is placed in the vase), the object is safe. At this time, the reed switch is in the closed state, and the buzzer is silent. After removing the magnet (such as the vase being stolen), the reed switch is not affected by the magnetic, the switch opens, and the buzzer sounds an alarm.
Code
Note
You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python-pi5
. After modifying the code, you can run it directly to see the effect.
#!/usr/bin/env python3
from gpiozero import Buzzer, Button
import time
# Initialize the buzzer on GPIO pin 27
buzzer = Buzzer(27)
# Initialize the reed switch on GPIO pin 17 with pull-up resistor enabled
reed_switch = Button(17, pull_up=True)
try:
while True:
# Check if the reed switch is pressed
if reed_switch.is_pressed:
# Turn off the buzzer if reed switch is pressed
buzzer.off()
else:
# If reed switch is not pressed, beep the buzzer
buzzer.on()
time.sleep(0.1) # Buzzer on for 0.1 seconds
buzzer.off()
time.sleep(0.1) # Buzzer off for 0.1 seconds
except KeyboardInterrupt:
# Turn off the buzzer when the program is interrupted (e.g., keyboard interrupt)
buzzer.off()
pass
Code Explanation
This imports the necessary classes
Buzzer
andButton
from thegpiozero
library, and thetime
module from Python’s standard library.#!/usr/bin/env python3 from gpiozero import Buzzer, Button import time
The
Buzzer
object is linked to GPIO pin 27, and aButton
(acting as a reed switch) is connected to GPIO pin 17 with thepull_up=True
argument, enabling the internal pull-up resistor.# Initialize the buzzer on GPIO pin 27 buzzer = Buzzer(27) # Initialize the reed switch on GPIO pin 17 with pull-up resistor enabled reed_switch = Button(17, pull_up=True)
The
try
block contains an infinite loop (while True
) checking the state of the reed switch. If pressed (is_pressed
), the buzzer is turned off. Otherwise, the buzzer beeps (0.1 seconds on, 0.1 seconds off).try: while True: # Check if the reed switch is pressed if reed_switch.is_pressed: # Turn off the buzzer if reed switch is pressed buzzer.off() else: # If reed switch is not pressed, beep the buzzer buzzer.on() time.sleep(0.1) # Buzzer on for 0.1 seconds buzzer.off() time.sleep(0.1) # Buzzer off for 0.1 seconds
The
except
block handles a KeyboardInterrupt (like a Ctrl+C in the terminal) to turn off the buzzer safely.except KeyboardInterrupt: # Turn off the buzzer when the program is interrupted (e.g., keyboard interrupt) buzzer.off() pass