0

So i have started to play with the pi pico w and using them to light up some of my lego sets. this is the first time doing this, so i'm very new to coding in Python. i have all a total of 11 lights i would like to light up, and then randomly dim one off and then back on while it is on. the lights are connected to pins 21,22,24,25,26,27,29,30,31,32,34. i have been looking online to see how i can randomize within the array of pins, but i have not been able to find what i'm looking for. i have found this bit of code from the website Random Nerds Tutorial:

# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-pwm-micropython/
from machine import Pin, PWM
from time import sleep

# Set up PWM Pin
led = machine.Pin(20)
led_pwm = PWM(led)
duty_step = 129  # Step size for changing the duty cycle

#Set PWM frequency
frequency = 5000
led_pwm.freq (frequency)

try:
    while True:
      # Increase the duty cycle gradually
      for duty_cycle in range(0, 65536, duty_step):
        led_pwm.duty_u16(duty_cycle)
        sleep(0.005)
        
      # Decrease the duty cycle gradually
      for duty_cycle in range(65536, 0, -duty_step):
        led_pwm.duty_u16(duty_cycle)
        sleep(0.005)

i figure if i can get all the pins on at first, then switch the Increase and Decrease that would dim a light randomly. but then to run this on a random pin within a range of pins i have no idea. any help would be great

3
  • So update as i have been playing around i discovered the pins are incorrect it should be for pins: 15,16,17,18,19,20,21,22,26,27,28. still have not figured out a simple way to turn them all on at first and then randomly fade one out and then back in Commented Mar 14 at 7:32
  • so, those numbers are physical pin or GPIO number? Because in the question, they were clearly physical pin numbers (since GPIO 24 and 25 and 29 are not available) - but this list can't be physical pins, since physical pins 18 and 28 are not GPIO pins, they are ground pins Commented Mar 14 at 8:31
  • @JaromandaX thank you for getting back to me. Yes still very new to using and understanding the hardware side of it. as i later found out this was for GP15-22 and then GP26-28. sorry to confuse everyone. Commented Mar 15 at 0:04

2 Answers 2

0

You can modify your code like this:

from machine import Pin, PWM
from time import sleep
import random

# Define the pins connected to your lights
pins = [21, 22, 24, 25, 26, 27, 29, 30, 31, 32, 34]

# Set up PWM Pin
def setup_pwm(pin):
    led = Pin(pin, Pin.OUT)
    led_pwm = PWM(led)
    frequency = 5000
    led_pwm.freq(frequency)
    return led_pwm

# Function to randomly select a pin and dim it
def random_dim():
    random_pin = random.choice(pins)
    led_pwm = setup_pwm(random_pin)
    duty_step = 129  # Step size for changing the duty cycle
    try:
        while True:
            # Increase the duty cycle gradually
            for duty_cycle in range(0, 65536, duty_step):
                led_pwm.duty_u16(duty_cycle)
                sleep(0.005)

            # Decrease the duty cycle gradually
            for duty_cycle in range(65536, 0, -duty_step):
                led_pwm.duty_u16(duty_cycle)
                sleep(0.005)
    finally:
        led_pwm.deinit()  # Cleanup PWM after use

# Run the random_dim function
random_dim()


If you need some more detail info about Implementing PWM with Raspberry Pi Pico using MicroPython, you can see this article:

https://www.theengineeringprojects.com/2022/05/implementing-pwm-with-raspberry-pi-pico-using-micropython.html

0

Put your pins in a list (Pin 20 is PWM). Use a for statement to set the pins in an array. In your main loop random select pin from the list with random.randrange. See: https://docs.micropython.org/en/latest/rp2/quickref.html# and https://docs.micropython.org/en/latest/library/random.html

# Example:
import random
from machine import Pin, PWM
from time import sleep

a = []  # Array        V changed from 20 (PWM pin)
mlst = [15,16,17,18,19,14,21,22,26,27,28]
for x in mlst:
    p = Pin (x, Pin.OUT, value=1)
    a.append(p)
    #print(p)

# Main Loop
for x in range(10):
    y = random.randrange(len(mlst))
    print(mlst[y], "--", a[y], "--", a[y].value())
    if a[y].value() == 1: a[y].value(0)
    else: a[y].value(1)

Note: It is nice to edit your question with updated information as others looking for answers to understand the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.