Home Code Knightrider LED effect using Micropython

Knightrider LED effect using Micropython

by rp2040guy71

In this example we use LEDs connected to the following pins – 16,17,18,19,20,21,22,26,27,28 to create the knight rider effect

I tested this with cytron maker pi pico as it has LEDs connected to all the outputs

Schematic

If you want to build its fairly simple, you just need the pico, 10 LEDs and 10 resistors.

We forgot the resistor and led for pin 28

Parts

RP2040 Aliexpress
Electronics Components Basic Starter Kit Aliexpress

Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Knight Rider LED
from machine import Pin
import utime
#LED Pins to be used
LEDs = [16,17,18,19,20,21,22,26,27,28]
#create an empty list to assing pins in pico
led_pins = []
for x in range(0,10):
led_pins.append(Pin(LEDs[x], Pin.OUT, Pin.PULL_UP))
button_fast = Pin(11,Pin.IN, Pin.PULL_DOWN)
button_slow = Pin(13,Pin.IN, Pin.PULL_DOWN)
while True:
# Left to Right #
for x in range(len(led_pins)-2):
led_pins[x+1].value(1)
utime.sleep(0.010)
led_pins[x].value(1)
utime.sleep(0.010)
led_pins[x+1].value(1)
utime.sleep(0.010)
led_pins[x+2].value(1)
utime.sleep(0.010)
led_pins[x].value(0)
utime.sleep(0.010)
led_pins[x+1].value(1)
utime.sleep(0.010)
led_pins[x+2].value(0)
# Left to Right #
for x in reversed(range(len(led_pins)-2)):
led_pins[x+1].value(1)
utime.sleep(0.010)
led_pins[x].value(1)
utime.sleep(0.010)
led_pins[x+1].value(1)
utime.sleep(0.010)
led_pins[x+2].value(1)
utime.sleep(0.010)
led_pins[x].value(0)
utime.sleep(0.010)
led_pins[x+1].value(1)
utime.sleep(0.010)
led_pins[x+2].value(0)
# Knight Rider LED from machine import Pin import utime #LED Pins to be used LEDs = [16,17,18,19,20,21,22,26,27,28] #create an empty list to assing pins in pico led_pins = [] for x in range(0,10): led_pins.append(Pin(LEDs[x], Pin.OUT, Pin.PULL_UP)) button_fast = Pin(11,Pin.IN, Pin.PULL_DOWN) button_slow = Pin(13,Pin.IN, Pin.PULL_DOWN) while True: # Left to Right # for x in range(len(led_pins)-2): led_pins[x+1].value(1) utime.sleep(0.010) led_pins[x].value(1) utime.sleep(0.010) led_pins[x+1].value(1) utime.sleep(0.010) led_pins[x+2].value(1) utime.sleep(0.010) led_pins[x].value(0) utime.sleep(0.010) led_pins[x+1].value(1) utime.sleep(0.010) led_pins[x+2].value(0) # Left to Right # for x in reversed(range(len(led_pins)-2)): led_pins[x+1].value(1) utime.sleep(0.010) led_pins[x].value(1) utime.sleep(0.010) led_pins[x+1].value(1) utime.sleep(0.010) led_pins[x+2].value(1) utime.sleep(0.010) led_pins[x].value(0) utime.sleep(0.010) led_pins[x+1].value(1) utime.sleep(0.010) led_pins[x+2].value(0)
# Knight Rider LED
from machine import Pin
import utime

#LED Pins to be used
LEDs = [16,17,18,19,20,21,22,26,27,28]

#create an empty list to assing pins in pico
led_pins = []

for x in range(0,10):
    led_pins.append(Pin(LEDs[x], Pin.OUT, Pin.PULL_UP))
    
button_fast = Pin(11,Pin.IN, Pin.PULL_DOWN)
button_slow = Pin(13,Pin.IN, Pin.PULL_DOWN)

    
while True:
  # Left to Right #  
    for x in range(len(led_pins)-2):     
        led_pins[x+1].value(1)
        utime.sleep(0.010)
        led_pins[x].value(1)
        utime.sleep(0.010)
        led_pins[x+1].value(1)
        utime.sleep(0.010)
        led_pins[x+2].value(1)
        utime.sleep(0.010)
        led_pins[x].value(0)
        utime.sleep(0.010)
        led_pins[x+1].value(1)
        utime.sleep(0.010)
        led_pins[x+2].value(0)
            
  # Left to Right #  
    for x in reversed(range(len(led_pins)-2)):   
        led_pins[x+1].value(1)
        utime.sleep(0.010)
        led_pins[x].value(1)
        utime.sleep(0.010)
        led_pins[x+1].value(1)
        utime.sleep(0.010)
        led_pins[x+2].value(1)
        utime.sleep(0.010)
        led_pins[x].value(0)
        utime.sleep(0.010)
        led_pins[x+1].value(1)
        utime.sleep(0.010)
        led_pins[x+2].value(0)

 

Video

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More