Skip to content

Beginner’s Guide to Programming NeoPixel RGB LEDs with Pico & MicroPython

Neopixel LED

Table of Contents

Abstract

In this beginner-friendly tutorial, you’ll learn how to connect and program NeoPixel RGB LEDs using the Raspberry Pi Pico. With the help of MicroPython and Wokwi simulation, we’ll walk through step-by-step code examples to control colors, patterns, and simple animations. By the end, you’ll be ready to add vibrant lighting effects to your own projects with ease.

In this article, a comprehensive step-by-step guide to interface Analog data (Potentiometer) with Raspberry Pi Pico board using MicroPython. Raspberry Pi Pico has 4 different variants (Pico, Pico 2, Pico W, Pico 2W) supporting micro-python. This articles lays the foundation for more advanced embedded system and IoT projects.

🧭 Pre-Request

  • OS : Windows / Linux / Mac / Chrome
  • Thonny IDE.
  • MicroPython firmware in Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.

Hardware Required

  • Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
  • NeoPixel / WS2812B.
  • BreadBoard.
  • Micro USB Cable.
  • Connecting wires.
  • 5V DC power supply (optional)
Components Purchase Link
Raspberry Pi Pico link
Raspberry Pi Pico 2 link
Raspberry Pi Pico W link
Raspberry Pi Pico 2W link
NeoPixel 16 LED : 8 LED
BreadBoard large : small
Connecting Wires link
Micro USB Cable link

Don't own a hardware 😢

No worries,

Still you can learn using simulation. check out simulation part 😃.

Connection Table

NeoPixel GPIO Remarks
GND GND
Vcc +5 V +5V of Board or External 5V DC Supply
Din 21 Any GPIO pin can be used.
Dout -- No Connection

Note

  • While using External 5 V supply, make sure ground pin of External Supply and Pico board is connected.

Circuit Diagram

fig-Connection Diagram

📂 Code

from machine import Pin
import time
from neopixel import NeoPixel

pin = Pin(21, Pin.OUT)   # set GPIO 21 to output to drive NeoPixels
np = NeoPixel(pin, 16)   # create NeoPixel driver on GPIO 21 for 16 pixels

colors = [(255, 0 , 0), (0, 255, 0), (0, 0, 255), (255, 169, 0), (255, 255, 255)]

for color in colors:
    for i in range(16):
        np[i] = color 
        np.write()
        time.sleep_ms(100)

    time.sleep(1)


for color in colors:
    for i in range(15, -1, -1):
        np[i] = color 
        np.write()
        time.sleep_ms(100)

    time.sleep(1)

Code Explanation

👉 Imports

1
2
3
from machine import Pin
import time
from neopixel import NeoPixel
  • time module for creating delay.
  • neopixel module for interacting with WS2812 / NeoPixel LED.

👉 Initialize NeoPixel LED.

pin = Pin(21, Pin.OUT)   # set GPIO 21 to output to drive NeoPixels
np = NeoPixel(pin, 16)   # create NeoPixel driver on GPIO 21 for 16 pixels
  • GPIO 21 is connected to Din of NeoPixel LED.
  • 16 NeoPixel LED's are used in the strip (np = NeoPixel(pin, 16))

👉 Initializing list of Color values.

colors = [(255, 0 , 0), (0, 255, 0), (0, 0, 255), (255, 169, 0), (255, 255, 255)]
  • Color values are configured in RGB mode
  • Each tuple represents a RGB color code
    • (R, G, B)
    • (255, 0, 0) --> Red 255, Green 0, Blue 0
  • Each color value range from 0 to 255

👉 Setting the color.

np[i] = color 
np.write()
  • np.fill(<color_r_g_b>) : fills the color to all the led.
  • np[i] : Set the color to the i ith LED
  • np.write() : Use write method to observe the effect on the LED.
    • Only after execution of the write method, you can observe the changes in the LED color.
  • for loop in the code is used to loop through the color on individual led in clockwise and anti-clockwise direction.

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page

Extras

Components details

Modules / Libraries Used

  • machine
    • machine module contains specific attributes and methods related to hardware on a particular board. Here class ADC is imported to configure the Analog pins.
    • More Details
  • time
    • time module provides functions related to date & time, measuring time intervals and generating delays.
    • More Details