Rainbow animation on built-in neopixel LED

Rainbow animation on built-in neopixel LED

Main Diagram

Hardware Required

  • PICUNO Microcontroller board (built-in NeoPixel LED on GPIO 17)
  • USB cable (for power and programming)

Description

The program continuously cycles through hue angles from 0° to 360°. Each hue is converted to an RGB value, which is then displayed on the NeoPixel LED. As the hue changes, the color transitions smoothly through the rainbow spectrum. This is ideal for testing HSV to RGB conversion and creating visually appealing light effects.

Circuit Diagram

[Fritzing image to be added here]

Circuit

  • Connect the PICUNO board to the computer using a USB cable.
  • No external wires or breadboard are required.
  • The built-in NeoPixel is already wired to GPIO 17.
  • The Arduino sketch uses the Adafruit NeoPixel library for control.
  • The Thonny IDE uses picuno library for control.

Schematic

No external wiring is required. The NeoPixel LED is internally connected to GPIO 17 on the PicUNO board.

Code - C

#include <Adafruit_NeoPixel.h>

#define PIN 17
#define NUMPIXELS 1

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pixels.begin();
  pixels.setBrightness(100);
}

void loop() {
  for (int hue = 0; hue < 360; hue += 3) {
    uint32_t color = pixels.ColorHSV(hue * 182); // Map 0-360 to 16-bit HSV
    color = pixels.gamma32(color); // Optional: smooth brightness
    pixels.setPixelColor(0, color);
    pixels.show();
    delay(30);
  }
}
ColorHSV(hue * 182) - Converts 0-360° to the 16-bit HSV format NeoPixel uses.

gamma32() - Smoothens perceived brightness.

setPixelColor() and show() - Update LED color frame by frame.

Code - Micropython

from picuno import Neopixel
import time
import math

pixels = Neopixel(1, 0, 17, "GRB")
pixels.brightness(100)

def hsv_to_rgb(h):
    h = float(h)
    s = 1
    v = 1
    h60 = h / 60.0
    h60f = math.floor(h60)
    f = h60 - h60f
    p = int(255 * v * (1 - s))
    q = int(255 * v * (1 - s * f))
    t = int(255 * v * (1 - s * (1 - f)))
    v = int(255 * v)

    if h60f == 0:
        return (v, t, p)
    elif h60f == 1:
        return (q, v, p)
    elif h60f == 2:
        return (p, v, t)
    elif h60f == 3:
        return (p, q, v)
    elif h60f == 4:
        return (t, p, v)
    elif h60f == 5:
        return (v, p, q)

while True:
    for hue in range(0, 360, 3):
        r, g, b = hsv_to_rgb(hue)
        pixels.set_pixel(0, (r, g, b))
        pixels.show()
        time.sleep(0.03)
hsv_to_rgb() - Converts a hue angle (0-360°) into an RGB colour tuple.

fade loop - Iterates hue values and converts them into colour transitions.

set_pixel() and show() - Apply the colour to the NeoPixel LED.