.. 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 [|link_sf_facebook|] et rejoignez-nous aujourd'hui !
.. _2.1.3_py_pi5:
2.1.3 Module de Commutateur Tactile
=======================================
Introduction
----------------------
Dans ce projet, vous allez découvrir le module de commutateur tactile. Il peut remplacer les types de commutateurs traditionnels avec ces avantages : utilisation pratique, sensation de toucher agréable, contrôle précis et usure mécanique minimale.
Composants Nécessaires
------------------------------
Pour ce projet, nous avons besoin des composants suivants.
.. image:: ../python_pi5/img/2.1.3_touch_switch_list.png
:width: 700
:align: center
Il est certainement pratique d'acheter un kit complet, voici le lien :
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Nom
- ÉLÉMENTS DANS CE KIT
- LIEN
* - Kit Raphael
- 337
- |link_Raphael_kit|
Vous pouvez également les acheter séparément via les liens ci-dessous.
.. list-table::
:widths: 30 20
:header-rows: 1
* - PRÉSENTATION DES COMPOSANTS
- LIEN D'ACHAT
* - :ref:`cpn_gpio_extension_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_touch_switch`
- |link_touch_buy|
Schéma de Câblage
--------------------
.. image:: ../python_pi5/img/2.1.3_touch_switch_schematic.png
:width: 500
:align: center
Procédures Expérimentales
---------------------------------
**Étape 1 :** Construire le circuit.
.. image:: ../python_pi5/img/2.1.3_touch_switch_circuit.png
:width: 700
:align: center
**Étape 2 :** Changer de répertoire.
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**Étape 3 :** Exécuter.
.. raw:: html
.. code-block::
sudo python3 2.1.3_TouchSwitch_zero.py
Pendant que le code s'exécute, la LED rouge s'allume ; lorsque vous appuyez sur le module de commutateur tactile, la LED jaune s'allume.
.. warning::
Si vous recevez le message d'erreur ``RuntimeError: Cannot determine SOC peripheral base address``, veuillez consulter :ref:`faq_soc`
**Code**
.. note::
Vous pouvez **Modifier/Réinitialiser/Copier/Exécuter/Arrêter** le code ci-dessous. Mais avant cela, vous devez aller au 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.
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import LED, Button # Import LED and Button classes from gpiozero
from time import sleep # Import sleep for delay
# Initialize touch sensor (Button) on GPIO pin 17, pull-up resistor disabled
touch_sensor = Button(17, pull_up=False) # Suitable for sensors that pull the pin low when pressed
# Initialize LED1 and LED2 connected to GPIO pins 22 and 27 respectively
led1 = LED(22) # LED1 connected to GPIO pin 22
led2 = LED(27) # LED2 connected to GPIO pin 27
try:
# Continuously monitor the state of the touch sensor and control LEDs accordingly
while True:
if touch_sensor.is_pressed: # Check if the touch sensor is pressed
print('You touch it!') # Output message indicating sensor activation
led1.off() # Turn off LED1
led2.on() # Turn on LED2
else: # If the sensor is not pressed
led1.on() # Turn on LED1
led2.off() # Turn off LED2
sleep(0.5) # Pause for 0.5 seconds before rechecking the sensor state
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop
pass
**Explication du Code**
#. Cette ligne configure le script pour qu'il s'exécute avec Python 3. Elle importe ``LED`` et ``Button`` de ``gpiozero`` pour contrôler les dispositifs GPIO, et ``sleep`` de ``time`` pour les délais.
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import LED, Button # Import LED and Button classes from gpiozero
from time import sleep # Import sleep for delay
#. Initialise un capteur tactile (en tant que Button) sur la broche GPIO 17 avec la résistance pull-up désactivée, et deux LED sur les broches GPIO 22 et 27.
.. code-block:: python
# Initialize touch sensor (Button) on GPIO pin 17, pull-up resistor disabled
touch_sensor = Button(17, pull_up=False) # Suitable for sensors that pull the pin low when pressed
# Initialize LED1 and LED2 connected to GPIO pins 22 and 27 respectively
led1 = LED(22) # LED1 connected to GPIO pin 22
led2 = LED(27) # LED2 connected to GPIO pin 27
#. La boucle principale vérifie l'état du capteur tactile. Lorsqu'il est touché, LED2 s'allume et LED1 s'éteint ; lorsqu'il n'est pas touché, LED1 est allumée et LED2 éteinte. La boucle se répète toutes les 0,5 secondes. Capture un KeyboardInterrupt (comme Ctrl+C) pour permettre une terminaison propre du script.
.. code-block:: python
try:
# Continuously monitor the state of the touch sensor and control LEDs accordingly
while True:
if touch_sensor.is_pressed: # Check if the touch sensor is pressed
print('You touch it!') # Output message indicating sensor activation
led1.off() # Turn off LED1
led2.on() # Turn on LED2
else: # If the sensor is not pressed
led1.on() # Turn on LED1
led2.off() # Turn off LED2
sleep(0.5) # Pause for 0.5 seconds before rechecking the sensor state
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop
pass