Fade example script (arduino.py) Fades the on-board LED up and down with PWM, about once every three seconds. analogWrite() takes 0 (off) to 255 (fully on); the brightness goes up in 51 steps of 5, 30 ms apart, then back down.
- Uses
- analogWrite(), delay()
from arduino import *
STEP = 5
brightness = 0
fade_amount = STEP
def setup():
pass
def loop():
global brightness, fade_amount
analogWrite(LED_BUILTIN, brightness)
brightness += fade_amount
if brightness <= 0 or brightness >= 255:
fade_amount = -fade_amount
delay(30)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()