Advertisement
Python253

pico_dutycycle_pulse

Mar 1st, 2024 (edited)
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: pico_dutycycle_pulse.py
  3. # Author: Jeoi Reqi
  4. # Pulse and Fade the LED on a Raspberry Pi Pico Microcontroller running Adafruit Circuit Python & Asyncio using Duty Cycle.
  5.  
  6. from time import sleep
  7. from board import *
  8. import pwmio
  9. import asyncio
  10. import microcontroller
  11.  
  12. led = pwmio.PWMOut(LED, frequency=5000, duty_cycle=0)
  13.  
  14. def led_pwm_up(led):
  15.     for i in range(100):
  16.         # PWM LED up and down
  17.         if i < 50:
  18.             led.duty_cycle = int(i * 2 * 65535 / 100)  # Up
  19.         sleep(0.001)
  20.  
  21. def led_pwm_down(led):
  22.     for i in range(101):
  23.         # PWM LED up and down
  24.         if i >= 50:
  25.             led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100)  # Down
  26.         sleep(0.002)
  27.  
  28. async def pulse_pico_led(led):
  29.     print("\n\t~Starting Duty Cycle Test With LED Pulse~\n")
  30.     led_state = True
  31.     counter = 0
  32.     result_count = 0
  33.     while True:
  34.         if led_state:
  35.             print("\n\n\tFading LED On:\n\n")
  36.             for i in range(101):
  37.                 brightness_pct = i
  38.                 duty_cycle = int(i * 65535 / 100)
  39.                 led.duty_cycle = duty_cycle
  40.                 await asyncio.sleep(0.025)
  41.                 print("{:6d}".format(led.duty_cycle), end="")
  42.                 result_count += 1
  43.                 if result_count >= 12:
  44.                     print("\n")
  45.                     result_count = 0
  46.             led_state = False
  47.         else:
  48.             print("\n\n\n\tFading LED Off:\n\n")
  49.             for i in range(100, -1, -1):
  50.                 brightness_pct = i
  51.                 duty_cycle = int(i * 65535 / 100)
  52.                 led.duty_cycle = duty_cycle
  53.                 await asyncio.sleep(0.025)
  54.                 print("{:6d}".format(led.duty_cycle), end="")
  55.                 result_count += 1
  56.                 if result_count >= 12:
  57.                     print("\n")
  58.                     result_count = 0
  59.             led_state = True
  60.         counter += 1
  61.         if counter >= 2:
  62.             print("\n\n\t\t\t\t\t\t\t\t   Done!")
  63.             led.duty_cycle = 0
  64.             break
  65.         await asyncio.sleep(0)
  66.  
  67. asyncio.run(pulse_pico_led(led))
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement