Neopixel random colors generation

Neopixel random colors generation

Main Diagram

Hardware Required

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

Description

The RGB LED changes colour every second by generating new random values for red, green, and blue channels. This demonstrates how to apply random values in LED control and visualize endless colour combinations.

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);
  randomSeed(analogRead(0)); // Seed for randomness
}

void loop() {
  byte r = random(0, 256);
  byte g = random(0, 256);
  byte b = random(0, 256);

  pixels.setPixelColor(0, pixels.Color(r, g, b));
  pixels.show();
  delay(1000);
}
random(0, 256) - Returns a random number between 0 and 255.

setPixelColor(0, pixels.Color(r, g, b)) - Assigns the random color to the LED.

pixels.show() - Updates the LED display.

delay(1000) - Adds a 1-second delay between color changes.

Code - Micropython

from picuno import Neopixel
import time
import urandom

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

while True:
    r = urandom.getrandbits(8)
    g = urandom.getrandbits(8)
    b = urandom.getrandbits(8)

    pixels.set_pixel(0, (r, g, b))
    pixels.show()
    time.sleep(1)
urandom.getrandbits(8) - Generates a random number between 0 and 255 for each color channel.

set_pixel(0, (r, g, b)) - Sets the RGB color on the NeoPixel.

pixels.show() - Updates the LED to show the new color.

time.sleep(1) - Waits 1 second before changing to the next color.