LED noodles can use over 100 milliamps.
Most digital outputs are rated at only 20 milliamps.
So to get enough current, we need to use a transistor
as a switch to turn the LED noodle on and off.
Connect the output of GPIO 15 to the 10k base resistor
Connect the other end of the base resistor to the middle pin of the 2N2222 transistor
Connect the emitter to ground
Connect the collector to the 15 ohm current limiting resistor
Connect the other end of the 15 ohm current limiting resistor to the cathode end (no hole) of the LED noodle
Connect the other end of the LED noodle (anode with hole) to the 5 volt rail
Blink
1 2 3 4 5 6 7 8 91011121314151617
frommachineimportPin,PWMfromtimeimportsleep# lower left corner of the PicoLED_PIN_1=15led_1=machine.Pin(LED_PIN_1,machine.Pin.OUT)delay=1.03# one second on and one second off# Main loop: Repeat the forever...whileTrue:print('high')led_1.high()# turn on the LEDsleep(delay)# leave it on for 1 secondprint('low')led_1.low()# Turn off the LEDsleep(delay)# leave it off for 1 second
Fade In and Out
1 2 3 4 5 6 7 8 91011121314151617
frommachineimportPin,PWMfromtimeimportsleep# lower left corner of the PicoLED_PIN_1=15pwm=PWM(Pin(LED_PIN_1))pwm.freq(1000)MAX_BRIGHTNESS=65025whileTrue:fordutyinrange(MAX_BRIGHTNESS):pwm.duty_u16(duty)sleep(0.0001)fordutyinrange(MAX_BRIGHTNESS,0,-1):pwm.duty_u16(duty)sleep(0.0001)
Note that the brightness does not change much
for the second half of the brightness. To
get a more even change, you can change the
MAX_BRIGHTNESS = 32000.
Sequential Fade In and Out
Now, let's connect five different LED noodles of different colors up
and have them turn on in a sequence, one after another.
We can just create five copies of the lines that define the LEDs and repeat this pattern over and over.
frommachineimportPin,PWMfromtimeimportsleep,sleep_ms# lower left corner of the PicoLED_PIN_1=11LED_PIN_2=12LED_PIN_3=13LED_PIN_4=14LED_PIN_5=15# led_1 = machine.Pin(LED_PIN_1, machine.Pin.OUT)pwm_1=PWM(Pin(LED_PIN_1))pwm_2=PWM(Pin(LED_PIN_2))pwm_3=PWM(Pin(LED_PIN_3))pwm_4=PWM(Pin(LED_PIN_4))pwm_5=PWM(Pin(LED_PIN_5))pwm_1.freq(1000)pwm_2.freq(1000)pwm_3.freq(1000)pwm_4.freq(1000)pwm_5.freq(1000)MAX_BRIGHTNESS=32000DELAY_MS=1whileTrue:# turn up the brightnessfordutyinrange(0,MAX_BRIGHTNESS,100):pwm_1.duty_u16(duty)sleep_ms(DELAY_MS)# turn down the brightnessfordutyinrange(MAX_BRIGHTNESS,0,-100):pwm_1.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(0,MAX_BRIGHTNESS,100):pwm_2.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(MAX_BRIGHTNESS,0,-100):pwm_2.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(0,MAX_BRIGHTNESS,100):pwm_3.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(MAX_BRIGHTNESS,0,-100):pwm_3.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(0,MAX_BRIGHTNESS,100):pwm_4.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(MAX_BRIGHTNESS,0,-100):pwm_4.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(0,MAX_BRIGHTNESS,100):pwm_5.duty_u16(duty)sleep_ms(DELAY_MS)fordutyinrange(MAX_BRIGHTNESS,0,-100):pwm_5.duty_u16(duty)sleep_ms(DELAY_MS)
Heartbeat
We can add a one second sleep to the off period to simulate
the beating of a heart.
1 2 3 4 5 6 7 8 91011121314151617
frommachineimportPin,PWMfromtimeimportsleep,sleep_ms# lower left corner of the PicoLED_PIN_1=12pwm=PWM(Pin(LED_PIN_1))pwm.freq(1000)MAX_BRIGHTNESS=20000whileTrue:fordutyinrange(0,MAX_BRIGHTNESS,200):pwm.duty_u16(duty)sleep_ms(2)fordutyinrange(MAX_BRIGHTNESS,0,-200):pwm.duty_u16(duty)sleep_ms(2)sleep(1)