Neopixel fade in and out of multiple colours

Hardware Required
- PICUNO Microcontroller board (built-in NeoPixel LED on GPIO 17)
- USB cable (for power and programming)
Description
The NeoPixel LED gradually fades in to full brightness with one colour and then fades out back to zero. This is repeated for red, green, and blue colours in sequence. The colour intensity is calculated step-by-step using a loop to simulate smooth brightness changes.
Circuit Diagram
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
This project uses the built-in LED on GPIO 14 of the PicUNO board. No additional circuit components are required.
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 fadeColor(uint8_t r, uint8_t g, uint8_t b) {
for (int i = 0; i <= 255; i += 5) {
pixels.setPixelColor(0, (r * i) / 255, (g * i) / 255, (b * i) / 255);
pixels.show();
delay(10);
}
for (int i = 255; i >= 0; i -= 5) {
pixels.setPixelColor(0, (r * i) / 255, (g * i) / 255, (b * i) / 255);
pixels.show();
delay(10);
}
}
void loop() {
fadeColor(255, 0, 0); // Red
fadeColor(0, 255, 0); // Green
fadeColor(0, 0, 255); // Blue
}
#define PIN 17
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pixels.begin();
pixels.setBrightness(100);
}
void fadeColor(uint8_t r, uint8_t g, uint8_t b) {
for (int i = 0; i <= 255; i += 5) {
pixels.setPixelColor(0, (r * i) / 255, (g * i) / 255, (b * i) / 255);
pixels.show();
delay(10);
}
for (int i = 255; i >= 0; i -= 5) {
pixels.setPixelColor(0, (r * i) / 255, (g * i) / 255, (b * i) / 255);
pixels.show();
delay(10);
}
}
void loop() {
fadeColor(255, 0, 0); // Red
fadeColor(0, 255, 0); // Green
fadeColor(0, 0, 255); // Blue
}
Adafruit_NeoPixel() - Initializes NeoPixel object.
setBrightness(100) - Sets LED brightness.
fadeColor() - Scales RGB values in a loop to simulate fading.
delay(10) - Controls the speed of fading transition.
setBrightness(100) - Sets LED brightness.
fadeColor() - Scales RGB values in a loop to simulate fading.
delay(10) - Controls the speed of fading transition.
Code - Micropython
from picuno import Neopixel
import time
pixels = Neopixel(1, 0, 17, "GRB")
pixels.brightness(100)
def fade_color(color_tuple):
# Fade in
for i in range(0, 256, 5):
scaled = tuple((i * c) // 255 for c in color_tuple)
pixels.set_pixel(0, scaled)
pixels.show()
time.sleep(0.01)
# Fade out
for i in range(255, -1, -5):
scaled = tuple((i * c) // 255 for c in color_tuple)
pixels.set_pixel(0, scaled)
pixels.show()
time.sleep(0.01)
while True:
fade_color((255, 0, 0)) # Red
fade_color((0, 255, 0)) # Green
fade_color((0, 0, 255)) # Blue
import time
pixels = Neopixel(1, 0, 17, "GRB")
pixels.brightness(100)
def fade_color(color_tuple):
# Fade in
for i in range(0, 256, 5):
scaled = tuple((i * c) // 255 for c in color_tuple)
pixels.set_pixel(0, scaled)
pixels.show()
time.sleep(0.01)
# Fade out
for i in range(255, -1, -5):
scaled = tuple((i * c) // 255 for c in color_tuple)
pixels.set_pixel(0, scaled)
pixels.show()
time.sleep(0.01)
while True:
fade_color((255, 0, 0)) # Red
fade_color((0, 255, 0)) # Green
fade_color((0, 0, 255)) # Blue
Neopixel(1, 0, 17, "GRB") - Initializes 1 NeoPixel on GPIO 17.
brightness(100) - Sets brightness to 100%.
fade_color() - Function that scales RGB values using a loop to fade in and out.
pixels.set_pixel() + show() - Sends the color update to the LED.
brightness(100) - Sets brightness to 100%.
fade_color() - Function that scales RGB values using a loop to fade in and out.
pixels.set_pixel() + show() - Sends the color update to the LED.