Welcome to MattzoBricks Forums MattzoBricks General Forum NeoPixel Fully addressable multicolour LED

Viewing 0 reply threads
  • Author
    • #5870
      David GambleDavid Gamble


      These are a very good alternative to RGB LED. They are available in many formats including 5mm LED, strips, panels and sticks. They are very cheap, $1 each for a 5mm LED. Any number can be wired in series (often up to 60), require only one pin (cf 3 per RGB LED) and use VCC or Vin so that high current will not damage an ESP6822.

      Coding is very simple as shown below.
      Any pixel on a string can be addressed simply by pixel number and colour. The progam shows 2 Neopixels, nominally front and tail lights. The front and reverse swap depending on diection of travel and then flash yellow as if waiting.

      //Program to demonstrate Neopixels for model trains
      //David Gamble Feb 2023

      // NEOPIXEL BEST PRACTICES for most reliable operation:
      // – Add 1000 uF CAPACITOR between NeoPixel strip’s + and – connections.
      // – MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
      // – NeoPixel strip’s DATA-IN should pass through a 300-500 OHM RESISTOR.
      // – AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
      // connect GROUND (-) first, then +, then data.
      // – When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
      // (Skipping these may work OK on your workbench but can fail in the field)

      #include <Adafruit_NeoPixel.h>
      // Which pin on the Arduino is connected to the NeoPixels?
      // On a Trinket or Gemma we suggest changing this to 1:
      #define LED_PIN 2 // D4 on board
      // How many NeoPixels are attached to the Arduino?
      #define LED_COUNT 2

      // Declare our NeoPixel strip object:
      Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
      // Argument 1 = Number of pixels in NeoPixel strip
      // Argument 2 = Arduino pin number (most are valid)
      // Argument 3 = Pixel type flags, add together as needed:
      // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
      // NEO_KHZ400 400 KHz (classic ‘v1’ (not v2) FLORA pixels, WS2811 drivers)
      // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
      // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
      // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

      // setup() function — runs once at startup ——————————–

      void setup() {
      strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
      strip.show(); // Turn OFF all pixels ASAP
      strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

      // loop() function — runs repeatedly as long as board is on —————

      void loop() {

      uint32_t red = strip.Color(0, 150, 0);
      uint32_t green = strip.Color(150, 0, 0);
      uint32_t blue = strip.Color(0, 0, 150);
      uint32_t white = strip.Color(150, 150, 150);
      uint32_t yellow = strip.Color(150, 175, 0);
      uint32_t magenta = strip.Color(150, 0, 150);
      uint32_t cyan = strip.Color(0, 150, 150);
      uint32_t off = strip.Color(0, 0, 0);

      //Forward – headlight (0) Taillight (1)
      strip.setPixelColor(0,white); //white
      strip.setPixelColor(1,red); //red
      strip.setPixelColor(1,white); //white
      strip.setPixelColor(0,red); //red
      // Wait – flashing
      for (int i=0; i<10; i++) {
      strip.setPixelColor(1,yellow); //yellow
      strip.setPixelColor(0,off); //yellow
      strip.setPixelColor(1,off); //off
      strip.setPixelColor(0,yellow); //off
      delay (200);

Viewing 0 reply threads
  • You must be logged in to reply to this topic.