| |
| python:micropython:es [2023/08/19 17:44] – [2.1 Sortie numérique] phil | python:micropython:es [Date inconnue] (Version actuelle) – supprimée - modification externe (Date inconnue) 127.0.0.1 |
|---|
| [[:python:micropython:accueil|{{ :iconemaison.jpg?nolink&30|Sommaire MicroPython, CircuitPython}}]] | |
| |
| ===== MicroPython - Entrées, Sorties (GPIO) ===== | |
| {{ :micropython:logomicropython.png?nolink&120|}} | |
| |
| [Mise à jour le : 1/6/2023] <html><span style="color:red">En cours de rédaction</span></html> | |
| |
| * **Ressources** | |
| * <html><a href="https://micropython.org/" target="_blank">MicroPython.org</a></html> | |
| * <html><a href="https://docs.micropython.org/en/latest/index.html#" target="_blank">MicroPython documentation</a></html> | |
| * <html><a href="https://thonny.org/" target="_blank">IDE Thonny</a></html> | |
| |
| * **Lectures connexes** | |
| * [[python:micropython:raspypico2040|MicroPython - Les modules Raspberry Pi Pico et Pico W]] | |
| * <html><a href="https://randomnerdtutorials.com/esp32-esp8266-digital-inputs-digital-outputs-micropython/" target="_blank">ESP32/ESP8266 Digital Inputs and Digital Outputs with MicroPython</a></html> | |
| * <html><a href="https://randomnerdtutorials.com/micropython-gpios-esp32-esp8266/" target="_blank">MicroPython with ESP32 and ESP8266: Interacting with GPIOs</a></html> | |
| * <html><a href="https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/" target="_blank">ESP32/ESP8266 PWM with MicroPython – Dim LED</a></html> | |
| * <html><a href="https://randomnerdtutorials.com/esp32-esp8266-analog-readings-micropython/" target="_blank">ESP32/ESP8266 Analog Readings with MicroPython</a></html> | |
| * <html><a href="https://randomnerdtutorials.com/micropython-interrupts-esp32-esp8266/" target="_blank">MicroPython: Interrupts with ESP32 and ESP8266</a></html> | |
| |
| |
| ==== 2. Entrées, sorties numériques ==== | |
| |
| {{ :python:micropython:materiel:led.jpg?nolink&100|LED}} | |
| {{ :python:micropython:pmod_btn_25707.png?nolink&90|Digilent Pmod BTN: 4 User Pushbuttons}} | |
| === 2.2 Entrée numérique === | |
| * **Matériels** : [[arduino:uc:esp32|ESP32 Feather Huzzah]] ou [[python:micropython:raspypico2040|Raspberry Pi Pico]], <html><a href="https://fr.rs-online.com/web/p/modules-de-developpement-d-interface-homme-machine-ihm/1368059/" target="_blank" title="Distributeur">Digilent Pmod BTN: 4 User Pushbuttons</a></html> <html><a href="https://reference.digilentinc.com/_media/reference/pmod/pmodbtn/pmodbtn_sch.pdf" target="_blank">[Schéma]</a></html> | |
| |
| == 2.2.1 Présentation == | |
| <callout type="danger" icon="true">Les entrées numériques sont **fragiles**. Elles ne supportent ni les **décharges électrostatiques** ni les **surtensions**. Il ne faut ni les toucher ni leur appliquer une tension supérieure à 5V ou inférieure à 0V. \\ Une entrée numérique utilisée dans un programme **ne doit pas être laissée "en l’air"** (non connectée) car elle prendra alors un état logique aléatoirement et le comportement du programme deviendra imprévisible. \\ </callout> | |
| |
| {{ :tinyclros:gpio:resistancerappel.jpg?nolink&600 |}} | |
| |
| <callout type="info" icon="true">Les microcontrôleurs disposent de **résistances de rappel internes** pouvant être connectées par le logiciel.</callout> | |
| |
| {{ :materiels:capteurs:environnement:code.png?nolink|}} | |
| == 2.2.2 Exemples de code == | |
| <tabs> | |
| * [[#tab-pico_2|RPi Pico]] | |
| * [[#tab-esp32_2|ESP32]] | |
| |
| <pane id="tab-pico_2"> | |
| * **Ressources** sur Micropython.org. | |
| * <html><a href="https://docs.micropython.org/en/latest/library/machine.html?highlight=machine#module-machine" target="_blank">module machine</a></html> | <html><a href="https://docs.micropython.org/en/latest/library/time.html?highlight=time#module-time" target="_blank">module time</a></html> | <html><a href="https://docs.micropython.org/en/latest/library/machine.Pin.html" target="_blank">class Pin – control I/O pins</a></html> | |
| * <html><a href="https://docs.micropython.org/en/latest/rp2/quickref.html#pins-and-gpio" target="_blank">Quick reference for the RP2, Pins and GPIO </a></html> | |
| |
| //Exemple de code pour un **Raspberry Pi Pico**// | |
| <code Python *.py> | |
| # Configuration (en entrée) des broches connectées à deux boutons-poussoirs | |
| # Bibliothèques à installer | |
| from machine import Pin | |
| | |
| # Configuration (en entrée) des broches connectées à deux boutons-poussoirs | |
| button_min = Pin(20, Pin.IN) | |
| button_hr = Pin(21, Pin.IN) | |
| ... | |
| </code> | |
| </pane> | |
| |
| <pane id="tab-esp32_2"> | |
| * **Ressources** sur Micropython.org. | |
| * <html><a href="https://docs.micropython.org/en/latest/library/machine.html?highlight=machine#module-machine" target="_blank">module machine</a></html> | <html><a href="https://docs.micropython.org/en/latest/library/time.html?highlight=time#module-time" target="_blank">module time</a></html> | <html><a href="https://docs.micropython.org/en/latest/library/machine.Pin.html" target="_blank">class Pin – control I/O pins</a></html> | |
| * <html><a href="https://docs.micropython.org/en/latest/esp32/quickref.html#pins-and-gpio" target="_blank">Quick reference for the ESP32, Pins and GPIO</a></html> | |
| |
| //Exemple de code pour un **ESP32 Feather Huzzah**// | |
| <code python *.py> | |
| # Configuration (en entrée) des broches connectées à deux boutons-poussoirs | |
| button_min = Pin(25, Pin.IN) | |
| button_hr = Pin(26, Pin.IN) | |
| ... | |
| </code> | |
| </pane> | |
| |
| </tabs> | |
| |
| |
| |
| |
| |
| === 2.3 Interruption === | |
| {{ :python:micropython:deroulement_interruption.gif?nolink&300|}} | |
| * **Matériel** : [[arduino:uc:esp32|ESP32 Feather Huzzah]] ou [[python:micropython:raspypico2040|Raspberry Pi Pico]], <html><a href="https://fr.rs-online.com/web/p/modules-de-developpement-d-interface-homme-machine-ihm/1368059/" target="_blank" title="Distributeur">Digilent Pmod BTN: 4 User Pushbuttons</a></html> <html><a href="https://reference.digilentinc.com/_media/reference/pmod/pmodbtn/pmodbtn_sch.pdf" target="_blank">[Schéma]</a></html> | |
| |
| == 2.3.1 Présentation == | |
| Une **interruption** est un **arrêt temporaire** de l'exécution normale d'un programme par le processeur afin d'exécuter un autre programme (appelé **service d'interruption**). \\ | |
| L’interruption est provoquée par une cause externe (action sur un bouton-poussoir, mesure réalisée par un capteur, horloge temps réel, etc.). \\ | |
| On utilise les interruptions afin de permettre des **communications non bloquantes** avec des périphériques externes. \\ | |
| Une interruption tient compte de l’état logique présent sur une broche. Couramment, on la déclenchera sur **le front montant, le front descendant, ou chacun des fronts** d’un signal logique. | |
| |
| <callout type="warning" icon="true"> | |
| Une interruption sera reconnue si le signal présente des fronts "propres". Il faudra donc s’assurer de la qualité du signal. | |
| Les figures ci-dessous représentent un signal transmis à la fermeture du contact d’un anémomètre. Le signal de gauche n’est pas utilisable à cause du rebondissement du contact. En effet, il contient quatre fronts montants au lieu d’un seul comme dans le cas du signal de droite.</callout> | |
| |
| {{ :tinyclros:gpio:antirebong.png?nolink |}} | |
| |
| == 2.3.2 Configuration == | |
| La configuration en entrée de la broche destinée à recevoir un évènement est identique à celle du paragraphe précédent. \\ \\ | |
| |
| == 2.3.3 Evènement et gestionnaire d'évènement == | |
| Un évènement est attaché à un gestionnaire (service d'interruption) . \\ \\ | |
| |
| {{ :materiels:capteurs:environnement:code.png?nolink|}} | |
| == 2.3.4 Exemples de code == | |
| <tabs> | |
| * [[#tab-pico_3|RPi Pico]] | |
| * [[#tab-esp32_3|ESP32]] | |
| |
| <pane id="tab-pico_3"> | |
| <html><p style="color:red">A faire</a></html> | |
| |
| //Exemple de code pour un **Raspberry Pi Pico**// | |
| <code python *.py> | |
| </code> | |
| |
| </pane> | |
| |
| <pane id="tab-esp32_3"> | |
| * **Ressource** : <html><a href="https://docs.micropython.org/en/latest/esp8266/tutorial/pins.html?highlight=interrupt" target="_blank"><strong>Quick reference for ESP32</strong>, GPIO Pins, External interrupts</a></html> sur Micropython.org. | |
| |
| //Exemple de code pour un **ESP32 Feather Huzzah**// | |
| <code python *.py> | |
| # Code partiel du programme HORLOGE | |
| |
| # Réglage de l'heure à la mise sous tension | |
| time_offset=12*3600+0*60+0 # hh+mm+ss | |
| |
| # Routines de service d'interruption (ISR) | |
| def handle_interrupt_min(pin): | |
| global time_offset | |
| time_offset+=60 | |
| time.sleep(.2) | |
| |
| def handle_interrupt_hr(pin): | |
| global time_offset | |
| time_offset+=3600 | |
| time.sleep(.2) | |
| |
| # Réglage des minutes | |
| # Ajout de 60s à l'heure initiale | |
| button_min = Pin(25, Pin.IN) | |
| # Gestionnaire d'interruption | |
| button_min.irq(trigger=Pin.IRQ_RISING,handler=handle_interrupt_min) | |
| |
| # Réglage des heures | |
| # Ajout de 3600s à l'heure initiale | |
| button_hr = Pin(26, Pin.IN) | |
| # Gestionnaire d'interruption | |
| button_hr.irq(trigger=Pin.IRQ_RISING,handler=handle_interrupt_hr) | |
| {{ :python:micropython:materiel:thonny.png?nolink&70|}} | |
| </code> | |
| <callout type="tip" icon="true"><html><strong><a href="https://webge.fr/doc/wikis/code/micropython/MICROPYTHON_ESP32_HORLOGE.zip" target="_blank">Télécharger</a></strong></html> le projet MICROPYTHON_ESP32_HORLOGE pour Thonny. </callout> | |
| </pane> | |
| </tabs> | |
| {{ :python:micropython:youtube.png?nolink&50|}} | |
| <callout type="info" icon="true">A voir : la vidéo de démonstration sur <html><a href="https://youtu.be/FGXCdyn_A9A" target="_blank">Youtube</a></html></callout> | |
| |
| {{ :materiels:capteurs:potentiometre.png?nolink&80|Potentiomètre}} | |
| ==== 3. Entrées analogiques ==== | |
| * **Ressource** | |
| * <html><a href="https://docs.micropython.org/en/latest/rp2/quickref.html#pins-and-gpio" target="_blank"><strong>Quick reference for the RP2</strong>, Pins and GPIO </a>sur Micropython.org.</html>, potentiomètre 10kOhm. | |
| |
| === 3.1 Présentation === | |
| * **Ressource** | |
| * <html><a href="https://zestedesavoir.com/tutoriels/686/arduino-premiers-pas-en-informatique-embarquee/745_les-grandeurs-analogiques/3430_les-entrees-analogiques-de-larduino/" target="_blank">Un signal analogique : petits rappels</a></html> sur le site Zeste de savoir. | |
| {{ :materiels:capteurs:environnement:code.png?nolink|}} | |
| === 3.2 Exemples de code === | |
| <tabs> | |
| * [[#tab-pico_4|RPi Pico]] | |
| * [[#tab-esp32_4|ESP32]] | |
| |
| <pane id="tab-pico_4"> | |
| * **Ressource** | |
| * <html><a href="https://docs.micropython.org/en/latest/esp32/quickref.html#adc-analog-to-digital-conversion" target="_blank">ADC (analog to digital conversion)</a></html> | <html><a href="https://docs.micropython.org/en/latest/library/machine.Pin.html" target="_blank">class Pin – control I/O pins</a></html> sur Micropython.org. | |
| //Exemple de code pour un **Raspberry Pi Pico**// | |
| <code python *.py> | |
| # ------------------------------------------------------------------------------- | |
| # Lecture et affichage dans la console de la tension issue d'un potentiomètre | |
| # Date : 22/5/2023 | |
| # Matériels : Raspberry Pi Pico, Shield Grove, pot. 10k | |
| # ADC accessibles sur le shield Grove pour RP2 : | |
| # Connecteur: ADC : GPIO | |
| # A0 : ADC0 : 26 | |
| # A1 : ADC0,ADC1: 26,27 | |
| # A2 : ADC1,ADC2: 27,28 | |
| # IDE : Thonny | |
| # ------------------------------------------------------------------------------- | |
| from machine import ADC, Pin | |
| import time | |
| |
| # Le potentiomètre 10kOhm est connecté à l'entrée analogique A0 du shield. | |
| # Attention : La tension doit être comprise entre 0 - 3,3V (3,6V max !) | |
| # sur une entrée analogique. | |
| # Configuration | |
| pot = ADC(Pin(26)) | |
| |
| while (True): | |
| val=pot.read_u16() # lecture de l'ADC | |
| U = val*3.3/65535 # Calcul de la tension | |
| print("%.2f" % U) # Affichage dans la console (formaté à 2 décimales) | |
| time.sleep(1) | |
| </code> | |
| </pane> | |
| |
| <pane id="tab-esp32_4"> | |
| * **Ressource** | |
| * <html><a href="https://docs.micropython.org/en/latest/esp32/quickref.html#adc-analog-to-digital-conversion" target="_blank">ADC (analog to digital conversion)</a></html> sur Micropython.org. | |
| //Exemple de code pour un **ESP32 Feather Huzzah**// | |
| <code python *.py> | |
| # ADC accessibles en Python sur la carte ESP32 Feather Huzzah : | |
| # ADC:GPIO | |
| # A2 : 34 | |
| # A3 : 39 | |
| # A4 : 36 | |
| # A7 : 32 | |
| # A9 : 33 | |
| |
| from machine import ADC, Pin | |
| |
| # Le potentiomètre 10kOhm est connecté à l'entrée analogique A2 de l'ESP32. | |
| # Configuration | |
| adc = ADC(Pin(34)) | |
| # Sur une entrée analogique, la tension doit | |
| # être comprise entre 0 - 3,3V (3,6V max !) | |
| adc.atten(ADC.ATTN_11DB) # voir doc | |
| # Mesure | |
| value = adc.read() | |
| |
| print(value) # affichage dans la console | |
| </code> | |
| </pane> | |
| </tabs> | |
| |
| |