Digital (LED et Boutons)
Source : https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
Contrôler une LED
L'ESP32-S2 n'a pas de LED intégré comme sur les Arduino
ON/OFF
5
1
import board
2
import digitalio
3
led = digitalio.DigitalInOut(board.IO0)
4
led.direction = digitalio.Direction.OUTPUT
5
led.value = True
PWM
14
1
import time
2
import board
3
import pulseio
4
5
led = pulseio.PWMOut(board.IO0, frequency=5000, duty_cycle=0)
6
7
while True:
8
for i in range(100):
9
# PWM LED up and down
10
if i < 50:
11
led.duty_cycle = int(i * 2 * 65535 / 100) # Up
12
else:
13
led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100) # Down
14
time.sleep(0.01)
Contrôler la LED avec un bouton
18
1
import time
2
import board
3
from digitalio import DigitalInOut, Direction, Pull
4
5
led = DigitalInOut(board.IO0)
6
led.direction = Direction.OUTPUT
7
8
switch = DigitalInOut(board.IO1)
9
switch.direction = Direction.INPUT
10
switch.pull = Pull.UP
11
12
while True:
13
if switch.value:
14
led.value = False
15
else:
16
led.value = True
17
18
time.sleep(0.01) # debounce delay