Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Last active July 15, 2023 15:54
Show Gist options
  • Save ypelletier/1b5df378f3804fbcd17ef752257b31c2 to your computer and use it in GitHub Desktop.
Save ypelletier/1b5df378f3804fbcd17ef752257b31c2 to your computer and use it in GitHub Desktop.
Codeur rotatif KY-040 contrôle la luminosité de la LED embarquée d'un Raspberry Pi Pico
'''
On varie l'intensité lumineuse de la LED embarquée du Raspberry Pi Pico
au moyen d'un codeur rotatif.
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 du codeur rotatif
codeur = RotaryIRQ(pin_num_clk = 13 ,
pin_num_dt = 14 ,
min_val = 0 , # rapport cyclique de 0 %
max_val = 65535 , # rapport cyclique de 100%
incr = 4000 ,
reverse = True,
range_mode=RotaryIRQ.RANGE_BOUNDED)
# la LED embarquée du Raspberry Pi Pico
pwm = machine.PWM(machine.Pin("LED"))
pwm.freq(1000) # fréquence de 1000 Hz
vieille_valeur = codeur.value()
while True:
nouvelle_valeur = codeur.value()
if vieille_valeur != nouvelle_valeur:
vieille_valeur = nouvelle_valeur
pwm.duty_u16(nouvelle_valeur) # rapport cyclique de la LED = valeur du codeur rotatif
time.sleep_ms(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment