.. 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 ! .. _4.1.3_py: 4.1.3 Horloge Parlante ============================= Introduction --------------- Dans ce projet, nous allons créer une horloge parlante avec un haut-parleur et un afficheur 7 segments à 4 chiffres. L'afficheur 7 segments à 4 chiffres affichera l'heure, et le haut-parleur annoncera l'heure à chaque heure. Composants Nécessaires ------------------------- Pour ce projet, nous avons besoin des composants suivants. .. image:: ../img/3.1.17components.png :width: 800 :align: center Il est très 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 avec les liens ci-dessous. .. list-table:: :widths: 30 20 :header-rows: 1 * - Introduction 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_audio_speaker` - \- * - :ref:`cpn_4_digit` - \- * - :ref:`cpn_74hc595` - |link_74hc595_buy| Schéma de Connexion ----------------------- ============ ======== ======== === Nom T-Board Physique wiringPi BCM GPIO17 Pin 11 0 17 GPIO27 Pin 13 2 27 GPIO22 Pin 15 3 22 SPIMOSI Pin 19 12 10 GPIO18 Pin 12 1 18 GPIO23 Pin 16 4 23 GPIO24 Pin 18 5 24 ============ ======== ======== === .. image:: ../img/4.1.3_speechclock_schematic.png :width: 700 :align: center .. image:: ../img/3.1.17_schematic.png :width: 400 :align: center Procédures Expérimentales ------------------------------ **Étape 1 :** Construisez le circuit. .. image:: ../img/3.1.17fritzing.png :width: 900 :align: center Avant ce projet, vous devez vous assurer que vous avez complété :ref:`3.1.4_py`. **Étape 2 :** Utilisez la commande ``date`` pour afficher l'heure locale. .. raw:: html .. code-block:: date Si l'heure locale est différente de l'heure réelle, vous devez utiliser la commande suivante pour définir le fuseau horaire. .. raw:: html .. code-block:: sudo dpkg-reconfigure tzdata Choisissez votre fuseau horaire. .. image:: ../img/tzdata.png **Étape 3 :** Accédez au dossier du code. .. raw:: html .. code-block:: cd ~/raphael-kit/python/ **Étape 4 :** Exécutez. .. raw:: html .. code-block:: python3 4.1.3_SpeechClock.py Après l’exécution du code : - Un message de bienvenue est prononcé : « Le système d’horloge est démarré. Bienvenue ! » - L’afficheur à quatre chiffres montre l’heure actuelle au format HH:MM. - Au début de chaque heure (minute = 0), le système annonce l’heure une fois. - L’affichage se met continuellement à jour jusqu’à ce que ``Ctrl+C`` soit pressé, après quoi les ressources GPIO sont nettoyées. **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``. Après avoir modifié le code, vous pouvez l'exécuter directement pour voir l'effet. .. raw:: html .. code-block:: python import RPi.GPIO as GPIO from tts import TTS import time # Initialize TTS tts = TTS(engine="espeak") tts.lang('en-US') # GPIO pins SDI = 24 RCLK = 23 SRCLK = 25 placePin = (10, 22, 27, 17) # Seven-segment encoding number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90) def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(SDI, GPIO.OUT) GPIO.setup(RCLK, GPIO.OUT) GPIO.setup(SRCLK, GPIO.OUT) for pin in placePin: GPIO.setup(pin, GPIO.OUT) def clearDisplay(): for _ in range(8): GPIO.output(SDI, 1) GPIO.output(SRCLK, GPIO.HIGH) GPIO.output(SRCLK, GPIO.LOW) GPIO.output(RCLK, GPIO.HIGH) GPIO.output(RCLK, GPIO.LOW) def hc595_shift(data): for i in range(8): GPIO.output(SDI, (0x80 & (data << i))) GPIO.output(SRCLK, GPIO.HIGH) GPIO.output(SRCLK, GPIO.LOW) GPIO.output(RCLK, GPIO.HIGH) GPIO.output(RCLK, GPIO.LOW) def pickDigit(digit): # Turn all digits off for pin in placePin: GPIO.output(pin, GPIO.LOW) # Turn selected digit ON GPIO.output(placePin[digit], GPIO.HIGH) def loop(): status = 0 while True: now = time.localtime() hour = now.tm_hour minute = now.tm_min # Display minute (unit) clearDisplay() pickDigit(0) hc595_shift(number[minute % 10]) # Display minute (tens) clearDisplay() pickDigit(1) hc595_shift(number[(minute // 10) % 10]) # Display hour (unit) clearDisplay() pickDigit(2) hc595_shift(number[hour % 10]) # Display hour (tens) clearDisplay() pickDigit(3) hc595_shift(number[(hour // 10) % 10]) # Speak once every hour (at minute == 0) if minute == 0 and status == 0: tts.say(f'The time is now {hour} hours and {minute} minutes') time.sleep(3) # Give time to finish speaking status = 1 elif minute != 0: status = 0 time.sleep(0.005) # Prevent CPU overload def destroy(): GPIO.cleanup() if __name__ == '__main__': setup() # ★ Welcome message at startup tts.say("Clock system started. Welcome!") time.sleep(3) try: loop() except KeyboardInterrupt: destroy() **Explication du code** #. Initialise le moteur de synthèse vocale en utilisant ``espeak`` et définit la langue sur l’anglais. .. code-block:: python tts = TTS(engine="espeak") tts.lang('en-US') #. Définit les broches GPIO utilisées pour le registre à décalage (74HC595) ainsi que les broches de sélection des chiffres pour l’afficheur 4 digits. .. code-block:: python SDI = 24 RCLK = 23 SRCLK = 25 placePin = (10, 22, 27, 17) #. Stocke les valeurs d’encodage des segments permettant d’afficher les chiffres 0–9 sur l’afficheur à sept segments. .. code-block:: python number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90) #. Efface l’affichage en envoyant huit bits « 1 » dans le registre à décalage. .. code-block:: python def clearDisplay(): for _ in range(8): GPIO.output(SDI, 1) GPIO.output(SRCLK, GPIO.HIGH) GPIO.output(SRCLK, GPIO.LOW) GPIO.output(RCLK, GPIO.HIGH) GPIO.output(RCLK, GPIO.LOW) #. Envoie un octet de données de segments au 74HC595 pour mettre à jour le chiffre affiché. .. code-block:: python def hc595_shift(data): for i in range(8): GPIO.output(SDI, (0x80 & (data << i))) GPIO.output(SRCLK, GPIO.HIGH) GPIO.output(SRCLK, GPIO.LOW) GPIO.output(RCLK, GPIO.HIGH) GPIO.output(RCLK, GPIO.LOW) #. Sélectionne l’une des quatre positions de chiffres (milliers, centaines, dizaines, unités) pour le multiplexage. .. code-block:: python def pickDigit(digit): for pin in placePin: GPIO.output(pin, GPIO.LOW) GPIO.output(placePin[digit], GPIO.HIGH) #. Lit l’heure actuelle depuis l’horloge système. .. code-block:: python now = time.localtime() hour = now.tm_hour minute = now.tm_min #. Met à jour chaque chiffre de l’afficheur à sept segments pour afficher l’heure au format HH:MM. .. code-block:: python hc595_shift(number[minute % 10]) hc595_shift(number[(minute // 10) % 10]) hc595_shift(number[hour % 10]) hc595_shift(number[(hour // 10) % 10]) #. Prononce l’heure actuelle une fois au début de chaque heure grâce à la synthèse vocale. .. code-block:: python if minute == 0 and status == 0: tts.say(f'The time is now {hour} hours and {minute} minutes') status = 1 elif minute != 0: status = 0 #. Lit un message de bienvenue lorsque le programme démarre. .. code-block:: python tts.say("Clock system started. Welcome!") #. Libère toutes les broches GPIO lorsque l’utilisateur interrompt le programme avec ``Ctrl+C``. .. code-block:: python GPIO.cleanup() Image de Phénomène ------------------------ .. image:: ../img/4.1.3speech_clock.JPG :align: center