Note

Bonjour et bienvenue dans la Communauté Facebook des passionnés de Raspberry Pi, Arduino et ESP32 de SunFounder ! Plongez plus profondément dans l’univers des Raspberry Pi, Arduino et ESP32 avec d’autres passionnés.

Pourquoi rejoindre ?

  • Support d’experts : Résolvez les problèmes après-vente et les défis techniques avec l’aide de notre communauté et de notre équipe.

  • Apprendre et partager : Échangez des astuces et des tutoriels pour améliorer vos compétences.

  • Aperçus exclusifs : Accédez en avant-première aux annonces de nouveaux produits et aux aperçus.

  • Réductions spéciales : Profitez de réductions exclusives sur nos produits les plus récents.

  • Promotions festives et cadeaux : Participez à des cadeaux et des promotions de vacances.

👉 Prêt à explorer et à créer avec nous ? Cliquez [Ici] et rejoignez-nous aujourd’hui !

1.3.3 Relais

Introduction

Dans ce projet, nous allons apprendre à utiliser un relais. C’est l’un des composants couramment utilisés dans les systèmes de contrôle automatique. Lorsque la tension, le courant, la température, la pression, etc., atteignent, dépassent ou sont inférieurs à la valeur prédéterminée, le relais connecte ou interrompt le circuit, pour contrôler et protéger l’équipement.

Composants Nécessaires

Dans ce projet, nous avons besoin des composants suivants.

../_images/1.3.3_relay_list.png

Il est certainement pratique d’acheter un kit complet, voici le lien :

Nom

ARTICLES DANS CE KIT

LIEN

Kit Raphael

337

Raphael Kit

Vous pouvez également les acheter séparément via les liens ci-dessous.

INTRODUCTION DES COMPOSANTS

LIEN D’ACHAT

Carte d’extension GPIO

ACHETER

Plaque d’expérimentation (Breadboard)

ACHETER

Fils de Liaison

ACHETER

Résistance

ACHETER

LED

ACHETER

Transistor

ACHETER

Relais

ACHETER

Diode

ACHETER

Schéma de Câblage

../_images/1.3.3_relay_schematic.png

Procédures Expérimentales

Étape 1 : Construisez le circuit.

../_images/1.3.3_relay_circuit.png

Étape 2 : Ouvrez le fichier de code.

cd ~/raphael-kit/python-pi5

Étape 3 : Exécutez.

sudo python3 1.3.3_Relay_zero.py
Pendant l’exécution du code, la LED s’allume. De plus, vous pouvez entendre un
tic-tac causé par l’ouverture du contact normalement fermé et la fermeture du

contact normalement ouvert.

Avertissement

Si vous recevez le message d’erreur RuntimeError: Cannot determine SOC peripheral base address, veuillez consulter Si « gpiozero » ne fonctionne pas.

Code

Note

Vous pouvez Modifier/Réinitialiser/Copier/Exécuter/Arrêter le code ci-dessous. Mais avant cela, vous devez aller dans le chemin du code source comme raphael-kit/python-pi5. Après avoir modifié le code, vous pouvez l’exécuter directement pour voir l’effet.

#!/usr/bin/env python3
from gpiozero import OutputDevice  # Import the class for controlling GPIO pins
from time import sleep  # Import the sleep function for delay

# Initialize the relay connected to GPIO pin 17, starting in the 'off' state
relay = OutputDevice(17, initial_value=False)

try:
    # Loop to continuously toggle the relay's state every second
    while True:
        print('Relay open...')  # Inform that the relay is being activated
        relay.on()  # Turn on the relay (assuming active low configuration)
        sleep(1)   # Maintain the relay in the on state for 1 second

        print('...Relay close')  # Inform that the relay is being deactivated
        relay.off()  # Turn off the relay
        sleep(1)   # Maintain the relay in the off state for 1 second

except KeyboardInterrupt:
    # Handle a keyboard interrupt (Ctrl+C) to exit the loop
    relay.off()  # Ensure the relay is turned off before exiting
    pass

Explication du Code

  1. Importer OutputDevice de gpiozero pour contrôler les broches GPIO et sleep de time pour ajouter des délais.

    #!/usr/bin/env python3
    from gpiozero import OutputDevice  # Import the class for controlling GPIO pins
    from time import sleep  # Import the sleep function for delay
    
  2. Initializes an OutputDevice object for the relay connected to GPIO pin 17. The initial_value=False sets the relay to the off state initially (assuming active low configuration).

    # Initialize the relay connected to GPIO pin 17, starting in the 'off' state
    relay = OutputDevice(17, initial_value=False)
    
  3. Inside the try block, a while True loop continuously toggles the relay’s state. The relay is turned on and off with a 1-second delay between each state, accompanied by console print statements.

    try:
        # Loop to continuously toggle the relay's state every second
        while True:
            print('Relay open...')  # Inform that the relay is being activated
            relay.on()  # Turn on the relay (assuming active low configuration)
            sleep(1)   # Maintain the relay in the on state for 1 second
    
            print('...Relay close')  # Inform that the relay is being deactivated
            relay.off()  # Turn off the relay
            sleep(1)   # Maintain the relay in the off state for 1 second
    
  4. Attrape une interruption clavier (comme Ctrl+C) pour permettre une terminaison en douceur du script. Le relais est éteint avant de sortir du script.

    except KeyboardInterrupt:
      # Handle a keyboard interrupt (Ctrl+C) to exit the loop
      relay.off()  # Ensure the relay is turned off before exiting
      pass