In this tutorial, I will demonstrate how to control an Addressable LED Strip using any arduino sensor. The examples I will show below will use a potentiometer and a photo cell.
Needed parts include:
- An Arduino
- A breadboard
- An addressable LED strip
- Any sensor
- About 8 wires
- External power supply (if using a long portion of the LED strip)
The board set up is as follows:
Keep in mind that you could replace the potentiometer with virtually any other sensor which returns an analog range of numeric values back to the arduino (light cell, distance sensor, dust sensor, etc.)
Creating a “Moving Light”
Creating a moving light is easy, thanks to the FastLED library. Copy and Paste the code below into a sketch:
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 |
//Import the needed FastLED library #include <FastLED.h> //These variables allow you to specify how the strip will behave #define NUM_LEDS 50 //This number decides how many of the strip's LEDs the code will use const int ledLength = 8; //This number decides how many LEDs will be turned on at once //Define and initiate global variables CRGB leds[NUM_LEDS]; #define DATA_PIN 5 int sensorPin = 0; int ledValue; int sensorValue; void setup() { // initiate the LED strip FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); } void loop() { //Read value from any sensor sensorValue = analogRead(sensorPin); //Using the map function, we can take any sesor input and align it to a location on the LED strip ledValue = map(sensorValue, 0, 1023, 0, NUM_LEDS); //Clear previous configuration FastLED.clear(); //Set and turn on desired LED leds[ledValue] = CRGB::White; FastLED.show(); } |
Upload the code to your arduino, and viola! You can now control a traveling light on your LED strip with a potentiometer or any other sensor you may have chosen.
Next, let’s make the code a bit more complicated by transforming our traveling light into a traveling pack of lights. The more lights the better, everybody loves lights!
Harry’s Pal: “No! Harry, no! Don’t look at the light!”
Harry: “I can’t help it! It’s so beautiful!” [Gets electrocuted] “Waaaahooooo-ow!”
Creating a “Traveling Light Pack”
The code for creating a light pack is slightly more complex, and we will use for loops to decide the position of every pixel in the strip. Copy the code below into your sketch:
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 |
//Import the needed FastLED library #include <FastLED.h> //These variables allow you to specify how the strip will behave #define NUM_LEDS 50 //This number decides how many of the strip's LEDs the code will use const int ledLength = 8; //This number decides how many LEDs will be turned on at once //Define and initiate global variables CRGB leds[NUM_LEDS]; #define DATA_PIN 5 int sensorPin = 0; int ledValue; int sensorValue; void setup() { // initiate the LED strip FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); } void loop() { //Read value from any sensor sensorValue = analogRead(sensorPin); //Using the map function, we can take any sesor input and align it to a location on the LED strip ledValue = map(sensorValue, 0, 1023, 0, NUM_LEDS); //Clear past LED configuration and reset fade FastLED.clear(); //This code makes the light appear one at a time in the beginning of the strip. if(ledValue <= ledLength){ for(int led = 0; led <= ledValue; led++) { leds[led] = CRGB::White; } FastLED.show(); } //This code turns on the lights which fall in the values specified by the user. else { for(int led = ledValue-ledLength; led < ledValue; led++) { leds[led] = CRGB::White; } FastLED.show(); } } |
Great! Now let’s add a little more bells and whistles to the code.
Controlling Colors
In this example, we will add an additional sensor, a button, to control the color of our lights.
Notice that I switched the potentiometer with a photo cell for kicks. For this more advanced set up, you will also need:
- One 330 ohm resistor
- One 10k ohm resistor
- Photo cell (or any sensor that returns analog numeric values)
- Button
The new wiring scheme is as follows:
Once you have your board wired correctly. Copy and paste this code into your sketch:
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 67 68 69 |
//Import the needed FastLED library #include <FastLED.h> //These variables allow you to specify how the strip will behave #define NUM_LEDS 50 //This number decides how many of the strip's LEDs the code will use const int ledLength = 8; //This number decides how many LEDs will be turned on at once //Define and initiate global variables CRGB leds[NUM_LEDS]; #define DATA_PIN 5 int sensorPin = 0; int buttonPin = 3; int ledValue; int sensorValue; int buttonState; CRGB color; //initiate our color variables and an extra variable as a placeholder int r = 255; int g = 0; int b = 0; int x = 0; void setup() { // initiate the LED strip FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); //initiate button's pin pinMode(buttonPin, INPUT); } void loop() { //Read value from any sensor sensorValue = analogRead(sensorPin); //Read button state buttonState = digitalRead(buttonPin); //Cycle through the colors when the button is pressed if (buttonState == LOW) { x = r; r = b; b = g; g = x; delay(100); } //Using the map function, we can take any sesor input and align it to a location on the LED strip ledValue = map(sensorValue, 0, 1023, 0, NUM_LEDS); //Clear past LED configuration and reset fade FastLED.clear(); // use our loops and color assignments to operate the lights if(ledValue <= ledLength){ for(int led = 0; led <= ledValue; led++) { leds[led] = CRGB(r, g, b); } FastLED.show(); } else { for(int led = ledValue-ledLength; led < ledValue; led++) { leds[led] = CRGB(r, g, b); } FastLED.show(); } } |
Your LED strip now reacts to light levels in the room (ironic??) and can switch colors at a touch of a button.
Thanks for reading!
3 replies on “Controlling an “Addressable” LED Strip with Any Sensor”
Can you this with nano, not usin sensor and with normal leds?
Hi, i try to make a simple game where a player must push a button rapidly to lighten up each led on a strip one after one… If the player slows down, the lights goes back to the first one (to win: light up the led strip until the last led)
Could you help me? Thanks.
Could you please send a wiring diagram for a capacitive sensor (like a metal plate) controlling an addressable LED strip? And the programm…