Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Created July 15, 2023 15:37
Codeur rotatif KY-040 branché à un Raspberry Pi Pico
'''
Codeur rotatif KY-040 branché à un Raspberry Pi Pico
L'article complet se trouve ici:
https://electroniqueamateur.blogspot.com/2023/07/utilisation-dun-codeur-rotatif-avec-le.html
Ce script nécessite le pilote micropython réalisé par Mike Teachman:
https://github.com/miketeachman/micropython-rotary/tree/master
'''
import sys
from rotary_irq_rp2 import RotaryIRQ
import time
# paramètres de notre codeur rotatif
codeur = RotaryIRQ(pin_num_clk = 13 , # CLK branchée à GP13
pin_num_dt = 14 , # DT branchée à GP 14
min_val = 0 , # valeur minimale
max_val = 12 , # valeur maximale
reverse = True , # augmentation dans le sens horaire
range_mode=RotaryIRQ.RANGE_BOUNDED) # on ne dépasse pas les limites
# bouton poussoir du KY-040 (sortie SW) reliée à la broche GP15
# on active la résistance pull up interne de cette broche.
bouton = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
vieille_valeur = codeur.value()
vieux_bouton = bouton.value()
while True:
nouvelle_valeur = codeur.value()
nouveau_bouton = bouton.value()
if vieille_valeur != nouvelle_valeur:
vieille_valeur = nouvelle_valeur
print('resultat =', nouvelle_valeur)
if vieux_bouton != nouveau_bouton:
vieux_bouton = nouveau_bouton
print('bouton =', nouveau_bouton)
time.sleep_ms(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment