Using Neopixels

 


Adafruit Neopixel UberGuide
https://learn.adafruit.com/adafruit-neopixel-uberguide/neopixel-strips?embeds=allow




Basic Connections

To get started, let’s consider the example of an Arduino microcontroller connected to the computer’s USB port (though it works with many other microcontroller boards as well). We’ll elaborate on the finer points of powering NeoPixels later, but in general you’ll usually be using a 5V DC power supply (e.g. “wall wart”) or — for wearable projects — a 3.7 Volt lithium-polymer battery.

Identify the “input” end of your NeoPixel strip, pixel(s) or other device. On some, there will be a solder pad labeled “DIN” or “DI” (data input). Others will have an arrow showing the direction that data moves. The data input can originate from any digital pin on the Arduino, but all the example code is set up for digital pin 6 by default. The NeoPixel shield comes wired this way.

Some NeoPixel strips have extra wires so they can connect to both a microcontroller and power source, but these will always correspond to one of the same three inputs: +5V, ground or data. If not using the extra wires, you can clip off any exposed tips and/or insulate with some heat-shrink tube.

If there are four distinct wires, then it’s probably a DotStar strip, or maybe LPD8806 (less common nowadays).

NeoPixels powered by 5V ideally require a 5V data signal. If using a 3.3V microcontroller, it’s wise to use a logic level shifter such as a 74AHCT125 or 74HCT245. If you are powering your NeoPixels with 3.7v directly from a LiPoly cell, a 3.3v data signal is OK. See the “Logic Level Shifting” page for further information.

If using a Flora, Feather or other microcontroller board with an attached lithium-polymer battery: connect the +5V input on the strip to the pad labeled VBAT or BAT on the board, GND from the strip to any GND pad on the microcontroller board, and DIN to Flora pin D6. If the board doesn’t have a pin #6, you’ll need to modify the example code to change the pin number.

For other Arduino boards with a separate +5V DC power supply for the NeoPixels: connect the +5V input on the strip to the + (positive) terminal on the power supply (don’t connect to the Arduino), DIN to digital pin 6 on the Arduino, and – (minus or GND) on the strip must connect to both the minus (–) terminal on the DC supply and a GND pin on the Arduino (there are usually several — any will do).


-------------------------------

1. Wiring the Circuit

To control our NeoPixel strip with the ESP32, we need three primary lines: power (+5V), ground (GND), and data (DIN or Data In).

Safety & Protection Components

  • Common Ground (Shared GND): The ESP32 and the 5V power supply must share a single ground wire. Ground serves as the $0\text{ V}$ reference point. Without a shared ground, the ESP32 cannot reliably send digital signals to the NeoPixels.

  • 330 to 470 $\Omega$ Resistor on the Data Line: Place this resistor directly between your chosen ESP32 pin (GPIO 18) and the strip's DIN pin. This dampens voltage spikes and ringing on the signal wire that could damage the first pixel's sensitive input chip.

  • Logic-Level Shifter (3.3V to 5V): The ESP32 outputs signals at $3.3\text{ V}$, but WS2812B NeoPixels run on $5\text{ V}$ power and officially look for logic signals of at least $0.7 \times 5\text{ V} = 3.5\text{ V}$. While a short wire often works directly, an inline 3.3V-to-5V level shifter (such as a 74AHCT125) cleanly boosts the signal so the strip reads every color command without flickering.

  • 1000 µF (Microfarad) Capacitor: Place this large capacitor directly across the strip's $+5\text{ V}$ and $\text{GND}$ power pads (observing polarity: stripe goes to $\text{GND}$). NeoPixels pull sudden bursts of electric current when changing colors. The capacitor acts like a small local electrical reservoir, buffering voltage dips and absorbing initial power surges when plugged in.



2. Power Supply Requirements

Addressable LEDs contain three tiny internal LEDs (Red, Green, Blue) inside every single pixel.

Current Calculation




Recommended Power Supply

Use a dedicated 5V DC regulated power supply rated for at least 2 Amps (2000 mA).

  • A 2A rating provides overhead so the power brick never runs near its maximum limit.

  • Why not power the strip directly from the ESP32 board?

  • An ESP32 DevKit board receives power via a USB port or an onboard voltage regulator. Typical computer USB ports deliver only 500 mA, and onboard regulators are designed to run the microcontroller chip, not dozens of bright external LEDs. Drawing more than 1 A through the development board can cause the ESP32 to brown out, reboot, or permanently damage its traces.

3. Arduino Code

Open the Arduino IDE, go to Sketch > Include Library > Manage Libraries..., and install the Adafruit NeoPixel library.

Why use millis() instead of delay()?

The delay(2000) command halts the microcontroller entirely, freezing the processor for 2 full seconds so it cannot read buttons, check sensors, or receive serial data. By using millis() (an internal clock tracking milliseconds since boot), the board continuously checks whether 2 seconds have passed while remaining free to perform other tasks.

C++

#include <Adafruit_NeoPixel.h>

// Pin on the ESP32 connected to the NeoPixel data input resistor
#define LED_PIN 18

// Number of NeoPixels in our string
#define LED_COUNT 24

// Brightness level (0 to 255). 60 is plenty bright for indoor classroom use!
#define BRIGHTNESS 60

// Declare the NeoPixel strip object
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// Timing variables for non-blocking color transitions
unsigned long previousMillis = 0;
const unsigned long interval = 2000; // Hold each color for 2000 ms (2 seconds)

// State machine index: 0 = Red, 1 = Blue, 2 = Green
int colorState = 0;

void setup() {
strip.begin();
strip.setBrightness(BRIGHTNESS);
// Set the first color immediately at startup
applyColor(colorState);
strip.show();
}

void loop() {
unsigned long currentMillis = millis();

// Check if 2000 milliseconds have elapsed
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

// Advance to the next color state: 0 (Red) -> 1 (Blue) -> 2 (Green) -> repeat
colorState = (colorState + 1) % 3;

applyColor(colorState);
strip.show(); // Push updated color data to the pixels
}
}

// Helper function to fill all 24 pixels with the selected color
void applyColor(int state) {
uint32_t chosenColor;

if (state == 0) {
chosenColor = strip.Color(255, 0, 0); // Pure Red (R, G, B)
} else if (state == 1) {
chosenColor = strip.Color(0, 0, 255); // Pure Blue
} else {
chosenColor = strip.Color(0, 255, 0); // Pure Green
}

for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, chosenColor);
}
}


How to Test

  1. Upload the Code: Select your ESP32 board and COM port in the Arduino IDE, then click Upload.

  2. Observe Transitions: Verify all 24 LEDs illuminate together in the sequence: Solid Red (2s) $\rightarrow$ Solid Blue (2s) $\rightarrow$ Solid Green (2s) $\rightarrow$ Repeat.

  3. Troubleshooting Steps:

    • Strip stays dark: Verify external 5V power is plugged in and that the capacitor is installed with the correct polarity. Check that the ESP32 and external supply share a common GND.

    • Only the first pixel lights up or flickers: Ensure the data wire connects to the DIN end of the strip, not DOUT (data arrows on the strip point away from the ESP32). Check that GPIO 18 matches your wiring.

    • Colors appear mixed or swapped (e.g., Red and Green inverted): Change NEO_GRB in the constructor to NEO_RGB.

Extension Challenges for Advanced Makers

  1. Add a 4th Color: Increase colorState = (colorState + 1) % 4; and add an entry for Yellow (strip.Color(255, 200, 0)) or Purple (strip.Color(180, 0, 255)).

  2. Chaser/Rainbow Effect: Instead of coloring all 24 pixels the same hue, write a loop that shifts colors across the strip index (i) to create a moving pattern.

  3. Input Interaction: Connect a push-button to an unused GPIO pin and use the non-blocking loop to toggle color modes instantly on press.



No comments:

Post a Comment

Nov 3rd Class- Experimenting with Neopixels; MIDI Prototyping

Above: Kerem makes calculations...  The planned projects seemed to have coalesced into two projects: Project 1(Stanley-Kexin-Rohan) is a clo...