Introduction
This tutorial is an overview of how to use a NeoPixel LED strip in conjunction with a feed of data such that specific numeric inputs result in corresponding lights on the strip being turned on. The sketch uses a random number generator to create a stream of inputs, but in practice, the stream of randomly selected integers can be replaced by any similar live data. The integers in the data stream represent individual LEDs on the strip to be activated. The random number generator in the sketch creates up to five integers per second, but the stream can include up to 60 integers—one for each LED on the strip.
Parts List
- Arduino UNO Board & Starter Kit
- Adafruit NeoPixel Digital RGBW LED Strip (with at least 60 LEDs)
- Adafruit Female DC Power Adapter
- Adafruit 5V 2A wall charger
Also recommended: Radial Leaded 1000uF 10volts Electrolytic Capacitor
Wiring and Circuit Diagram
This sketch requires two different power sources. The Arduino board itself is powered by the USB connection to the computer being used to run the sketch, but the LED strip itself needs a current level beyond what the Arduino board is capable of supplying.
First, connect the wall charger to the Female DC Power Adapter. Then, connect the front end of the LED strip (the side from which the small black arrows on strip flow away) to the female adapter by inserting the red 5V Power wire into the square hole marked “+” and the black ground wire into the square hole marked “-“. By untightening the screws on the adapter, inserting the wires, then re-tightening the screws, the wires can be securely connected to the adapter without the need for soldering.
Before retightening, you may also wish to insert the Electrolytic Capacitor to the female power adapter. This is recommended, as the capacitor helps to reduce voltage pulsation from the 5V 2A power source coming from the wall outlet, providing a more consistent charge to the LED strip. Connect the capacitor to the female adapter such that its (longer) positive end is inserted into the square hole marked “+” and the negative end is inserted into the square hole marked “-“.
In addition to the 5V power wire and ground wire, there are connected white and black wires coming from the front of the LED strip. Connect these to the Arduino board such that the black one is connected to the GND Pin and the white one is connected to PIN 6.
See the diagram below for a breadboard circuit diagram:
See the image below for an image of the setup:
Code
Run the sketch below with the Arduino board and LED strip powered as described in the previous section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// Tutorial: Data Stream input to NeoPixel LED Strip // Requires Adafruit NeoPixel library // Will Friedrichs 3/30/22 // Include Adafruit NeoPixel library. // This can be downloaded from the Library Manager (Tools -> Manage // Libraries). #include <Adafruit_NeoPixel.h> // The PIN defined here is the pin from which information will be sent // to the LED Strip. Confirm that the PIN number specified here matches // the PIN number connected to the white wire at the front end of the // LED strip. #define PIN 6 // The number below sqecifies the quantity of NeoPixels are attached to // the strip. This tutorial calls for a strip with at least 60. #define NUMPIXELS 60 // Popular NeoPixel ring size // The NUMPIXELS variable, together with the PIN, and additional // information listed below must be included as parameters to the // Neopixel library setup. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 500 // Time (in milliseconds) to pause between pixels // In the setup section, which only runs once, initalize the pixel // strip object. Ensure no lights are on to start. void setup() { pixels.begin(); pixels.show(); } // In the loop function that runs contiuously: // 1. Create randomly generated data stream in a 60-item array // 2. For each of the 60 array locations: // a. Turn light off if value = 1 // b. Turn on light if value = 0 // ledArray will take the role of a datastream, regularly updating // its values as if it were getting them from some outside source. void loop() { pixels.clear(); // Set all pixel colors to 'off' // Randomly generate up to five values of lights to switch on // In ledArray, 0 means off and 1 means on int ledArray[NUMPIXELS] = { 0 }; int numberOfLights; numberOfLights = random(6); for(int i=0; i<numberOfLights; i++) { ledArray[random(NUMPIXELS)] = 1; } // The first NeoPixel in a strand is #0, second is 1, all the way up // to the count of pixels minus one. for(int i=0; i<NUMPIXELS; i++) { // For each pixel... if (ledArray[i] == 1) { // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255 // Here we're using a moderately bright green color: pixels.setPixelColor(i, pixels.Color(0, 150, 0)); pixels.show(); // Send the updated pixel colors to the hardware. } } delay(DELAYVAL); // Pause before next pass through loop } |
Use Cases
The tutorial and accompanying sketch may be useful for anyone wanting to take on a project involving LEDs reacting to a real-time flow of data. One could imagine a wide variety of projects involving sensors in conjunction with an LED strip involving real-time data. An interactive art piece made of LED lights could change based on the location of individual people in a public space. Motion detectors could light up specific parts of an LED strip to match the location of individuals to brighten a space while conserving energy. The data stream could also come from real-time, regularly updating, publicly available databases such as weather forecasts, creating the opportunity for the presentation of useful information with the added visual flair of LEDs.
Citations
https://github.com/adafruit/Adafruit_NeoPixel
Shae Erisson, 2013