.. 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 [|link_sf_facebook|] and join today! .. _2.2.4_py_pi5: 2.2.4 Reed Switch Module ======================== Introduction ------------------- In this project, we will learn about the reed switch, which is an electrical switch that operates by means of an applied magnetic field. Required Components ------------------------------ In this project, we need the following components. .. image:: ../python_pi5/img/2.2.4_reed_switch_list.png :width: 700 :align: center It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_resistor` - |link_resistor_buy| * - :ref:`cpn_led` - |link_led_buy| * - :ref:`cpn_reed_switch` - |link_reed_switch_buy| Schematic Diagram ----------------------- ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO17 Pin 11 0 17 GPIO27 Pin 13 2 27 GPIO22 Pin 15 3 22 ============ ======== ======== === .. image:: ../python_pi5/img/2.2.4_reed_switch_schematic_1.png :width: 400 :align: center .. image:: ../python_pi5/img/2.2.4_reed_switch_schematic_2.png :width: 400 :align: center Experimental Procedures ------------------------------- **Step 1:** Build the circuit. .. image:: ../python_pi5/img/2.2.4_reed_switch_circuit.png :width: 700 :align: center **Step 2:** Change directory. .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **Step 3:** Run. .. raw:: html .. code-block:: sudo python3 2.2.4_ReedSwitch_zero.py The green LED will light up when the code is run. If a magnet is placed close to the reed switch module, the red LED lights up; take away the magnet and the green LED lights up again. .. warning:: If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc` **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. .. raw:: html .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, Button # Initialize the reed switch and LEDs using GPIO Zero reed_switch = Button(17, pull_up=True) # Reed switch on GPIO 17, using an internal pull-up resistor green_led = LED(27) # Green LED connected to GPIO pin 27 red_led = LED(22) # Red LED connected to GPIO pin 22 def update_leds(): """ Update the state of the LEDs based on the reed switch. Turns the red LED on and green LED off when the reed switch is pressed, and vice versa. """ if reed_switch.is_pressed: green_led.off() # Turn off the green LED red_led.on() # Turn on the red LED else: green_led.on() # Turn on the green LED red_led.off() # Turn off the red LED try: green_led.on() # Turn on the green LED at the start while True: # Set the callback functions for reed switch state changes reed_switch.when_pressed = update_leds # Callback when the switch is pressed reed_switch.when_released = update_leds # Callback when the switch is released except KeyboardInterrupt: # Clean up resources and exit on Ctrl+C green_led.off() red_led.off() pass **Code Explanation** #. This line specifies that the script is to be run using Python 3. It imports ``LED`` and ``Button`` (used for the reed switch) from the gpiozero library. .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, Button #. Initializes the reed switch on GPIO pin 17 with an internal pull-up resistor. Also initializes two LEDs connected to GPIO pins 27 and 22. .. code-block:: python # Initialize the reed switch and LEDs using GPIO Zero reed_switch = Button(17, pull_up=True) # Reed switch on GPIO 17, using an internal pull-up resistor green_led = LED(27) # Green LED connected to GPIO pin 27 red_led = LED(22) # Red LED connected to GPIO pin 22 #. Defines the ``update_leds`` function, which updates the LED states based on the reed switch's state. The red LED is turned on and the green LED is turned off when the switch is pressed, and the opposite when released. .. code-block:: python def update_leds(): if reed_switch.is_pressed: green_led.off() # Turn off the green LED red_led.on() # Turn on the red LED else: green_led.on() # Turn on the green LED red_led.off() # Turn off the red LED #. Sets the initial state of the green LED to on. The main loop assigns the ``update_leds`` function as callbacks for the ``when_pressed`` and ``when_released`` events of the reed switch. Includes exception handling for KeyboardInterrupt to clean up and exit the program gracefully. .. code-block:: python try: green_led.on() # Turn on the green LED at the start while True: # Set the callback functions for reed switch state changes reed_switch.when_pressed = update_leds # Callback when the switch is pressed reed_switch.when_released = update_leds # Callback when the switch is released except KeyboardInterrupt: # Clean up resources and exit on Ctrl+C green_led.off() red_led.off() pass