Home Arduino Projects A Practical Introduction: Controlling LEDs With Arduino

A Practical Introduction: Controlling LEDs With Arduino

by Tutorial45

LED strips are one of the most popular light sources in the world today. From car modding, interior design, to cosplay, it’s hard to find a place where LED strips haven’t been used. Their size, ease of use, and fairly low price led them to become a staple for makers everywhere. They also make an unbeatable combination when combined with the versatility of the Arduino platform. As a beginner, using LED strips may seem like a daunting task, but not to fear! This guide serves as a practical introduction to using and integrating LED strips into your projects.

Knowledge Prerequisites

A basic understanding of circuits is needed. If you want to make your own controller, you’ll also need to have some experience programming with Arduino.

Background

At their core, LED strips are just surface mount LEDs attached to a flexible substrate. The three most common types of strips are single color, RGB, and addressable. Whichever you decide to use entirely depends on your project.

Single Color LED

Single color strips are the least complex LED strip. True to their name, they display only a single color, pre-determined by the manufacturer. With only two terminals, they operate similarly to a normal LED. Due to their low complexity, they are often cheaper than their RGB and addressable counterparts.

A Practical Introduction: Controlling LEDs With Arduino

While the lack of customization may not be appealing, remember that requirements drive what is done in the project. Perhaps the project only needs a single color. Maybe you don’t want to bother with the added complexity of a controller. Whatever it is, if the single color strip fits your needs, you don’t have to use more. 

RGB

An individual RGB LED is essentially a red, green, and blue LED combined into one compact package. By adjusting the brightness of each component LED, a multitude of colors can be reproduced. An RGB LED strip is composed of many RGB LEDs in parallel. We can immediately note that there are a few more pads than the single color strip mentioned above. 

A Practical Introduction: Controlling LEDs With Arduino

The extra pads shown above correspond to the extra terminals of each RGB LED. The usage of RGB strips is a bit more involved, since a controller is needed to mix the color you want. You can buy a controller or, with the proper knowledge and materials, make one yourself. This increased complexity may be more of a hassle, but it will give you access to over 1.6 million different color combinations. For most people, a standard RGB strip is all they need. But for those seeking complex patterns and animations, an addressable strip is sure to fulfill that itch.

Addressable

While an addressable strip may look like an average RGB strip, each LED can be individually lit up or “addressed” (hence the name). This means that each LED can have a different color and brightness from its neighbor, a feature not found on standard RGB strips. This feature is achieved by a small IC built into each of the LEDs on the strip. The fact that each LED can be individually addressed opens up a world of possibility for complex patterns and animations.

A Practical Introduction: Controlling LEDs With Arduino

Examining the pads of the strip, we can see that there is a DO/DIN pad. This is the data line, which is used to tell the strip which colors should be displayed. You only need a single data line to communicate to hundreds (or even thousands!) of addressable LEDs. The most common addressable is the WS2812, also known as the NeoPixel, compatible with most Arduino boards.

Powering LED Strips

A single LED poses no problem, but when many are used, the power draw can quickly exceed the limits of the controller. Problems can arise such as LEDs becoming redder near the end of the strip, wires becoming uncomfortably hot, or even the controller just outright frying. 

First and foremost, always make sure to select a power supply that can support the strip’s power draw. To determine the anticipated power draw, you can look at the datasheet for the LED it’s using. For example, when R,G, and B are set at maximum brightness for a WS2812, each WS2812 pulls around 60 mA. This may not seem like a lot, but when compounded across a whole strip, the current draw can become significant. Inspect the power supply you have on hand to determine its maximum rated current, usually indicated on a label or sticker. To minimize the chances of something breaking, make sure that the rated current is above the expected current draw. 

A Practical Introduction: Controlling LEDs With Arduino

Always be sure to install a bypass capacitor close to the supply if driving a significant amount of LEDs. A bypass capacitor is simply a decently sized capacitor placed parallel to the strip. This helps soften the blow of sudden current draw increases by acting as a reservoir for charge.

Redness and dimming near the ends is a common effect seen in long strips. As you go down the strip, there is less available voltage to drive the next LED. This happens due to energy wasted from the resistance of the traces and joints of the strip. The redness comes from the higher forward voltage of blue and green LEDs, which causes them to slowly drop out near the end. This issue can be solved with voltage injectors. Don’t be intimidated by the fancy name! This only involves soldering a +V wire from the supply to the end or middle of the strip (or wherever you decide to put it). 

A Practical Introduction: Controlling LEDs With Arduino

The injector “re-injects” the original supply voltage into the strip, giving that needed final push for the LEDs near the ends. Another solution for reddening/dimming is using a higher voltage strip (12V, 24V, etc.). This higher voltage gives a little more leeway for lost voltage and less waste heat. No injectors are needed for higher voltages, but this comes at the cost of needing to support more than one voltage in your project.

Last but not least, brightness limiting is also a good practice. A strip at full brightness can be excessively bright and rarely needed, wasting energy. Setting a maximum brightness in the code of your controller is a good way to limit peak current. However, if you really need maximum brightness out of your strip, make sure to use some kind of diffuser to reduce the harshness of the light.

Simple Demo Projects

The best way to learn how to use these strips is by making some basic examples. We will be making a Basic RGB Strip Controller, a Basic Addressable Strip Controller, and, to show off an application of addressable strips, A Distance Indicator.

Technical Prerequisites

An understanding of how to use a breadboard is needed. The ability to solder is also useful, but not necessary. 

Basic RGB Controller

Overview

This mini project is just skeleton code for an RGB controller. It alternates between displaying red, green, blue, and gold. Try modifying the RGB values to change what colors it outputs!

Required Materials

  • Standard 5V RGB LED Strip
  • Arduino (of your choice)
  • 20 or 22 AWG wire or RGB Strip Connectors
  • Breadboard

Steps

  1. If needed, trim the RGB strip to a suitable length by cutting at the copper pads with scissors or wire cutters. Do not cut anywhere else, as it may damage the strip.
A Practical Introduction: Controlling LEDs With Arduino

Make sure that you don’t make the strip too long! If too there are too many LEDs, the power draw could fry the Arduino.

  1. If not already attached, attach a strip connector to your trimmed strip. If you have the tools and the skill, you can solder on some jumper wires instead.
  2. Attach the R,G,B pins of the strip to the corresponding pins according to the code below. Feel free to change the pin values in the predefine.
  3. Upload code and observe.

Code

#define red_pin 9
#define green_pin 6
#define blue_pin 5
void setup() {
  pinMode(red_pin, OUTPUT);
  pinMode(green_pin, OUTPUT);
  pinMode(blue_pin, OUTPUT);
}
void loop() {
  RGB_color(255, 0, 0); // Red.
  delay(400);
  RGB_color(0, 255, 0); // Green.
  delay(400);
  RGB_color(0,0,255); // Blue.
  delay(400);
  RGB_color(255,215,0); // Gold.
  delay(400);
}
void RGB_color(int red_value, int green_value, int blue_value)
 {
  analogWrite(red_pin, red_value);
  analogWrite(green_pin, green_value);
  analogWrite(blue_pin, blue_value);
}

Notes

As you can see, controlling RGB strips is similar to controlling just a single RGB LED. The RGB_color() function here uses 3 analogWrite statements to output PWM signals to the red, green, and blue component LEDs. If driving more LEDs or at higher voltages, outputting this PWM signal to MOSFETs would be advisable, as shown below.

A Practical Introduction: Controlling LEDs With Arduino

The Yellow and black cables are coming from a 12v power supplier where the yellow cable is Live and the black cable is Ground.


Basic Addressable Strip Controller

Overview

This mini project is skeleton code for an addressable LED controller, showing how the strip is treated in code. It displays an alternating red and green.

Required Materials

  • WS2812 LED Strip
  • Arduino (of your choice)
  • 20 or 22 AWG wire or Addressable Strip Connectors

Steps

  1. Follow steps 1 and 2 again from the RGB strip demo. The same basic practices follow for all LED strips.
  2. Attach the strip’s data pin to the corresponding pin on the Arduino, as set in the code below. If you have a different number of LEDs or want to use a different LED data pin, feel free to change the values in the predefine.
  3. Upload code and observe.
A Practical Introduction: Controlling LEDs With Arduino

Code

#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 8 // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 100; // timing delay in milliseconds
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
void setup() {
  // Initialize the NeoPixel library.
  pixels.begin();
}
void loop() {
  setColor();
  for (int i=0; i < NUMPIXELS; i++) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
    // This sends the updated pixel color to the hardware. pixels.show(); // Delay for a period of time (in milliseconds). delay(delayval);
  }
}
// setColor()
// picks random values to set for RGB
void setColor(){
  redColor = random(0, 255);
  greenColor = random(0,255);
  blueColor = random(0, 255);
}

Notes

The library being used here is called FastLED, the definitive Arduino library for controlling addressable LEDs. In the FastLED library, the strip is treated as an array, instantiated by the addLeds function. Remember that changes to the strip are only displayed when you call the FastLED.show() function!

This is a very simple example of what FastLED can do. The library has many more features, such as special math functions and HSV colorspace functions. Check out their documentation and examples for more!


Distance Indicator

Overview

Combining sensors and a form of visual output is a classic combination. In this mini-project, we will be combining an ultrasonic distance sensor and an addressable LED strip to make a distance indicator. The distance indicator will be effective between the MIN_DISTANCE and the MAX_DISTANCE values. The further away an object is, the more LEDs are lit up.

Required Materials

  • HC SR04 Ultrasonic Sensor
  • Arduino Uno
  • WS2812 LED Strip
  • Breadboard

Steps

  1. Like before, follow steps 1 and 2 of the RGB controller mini-project to prepare your LED strip.
  2. Link the pins of the strip and the ultrasonic sensor to the pins specified in the code. Feel free to change the values in the predefine to fit your needs.
  3. Upload code and observe.
A Practical Introduction: Controlling LEDs With Arduino

Code

#include <FastLED.h>

#define TRIG_PIN 2;
#define ECHO_PIN 3;

#define LED_COUNT 8;
#define LED_OUT 4;

//In centimeters
#define MIN_DISTANCE 10
#define MAX_DISTANCE 40

CRGB leds[LED_COUNT];

float duration, distance;
float dist_div = (MAX_DISTANCE-MIN_DISTANCE)/LED_COUNT;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  FastLED.addLeds<WS2812,LED_OUT,GRB>(leds,LED_COUNT);
}

void loop() {

  //Determine distance
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration*.0343)/2;
  
  //Set the LEDs
  for (int i = 0; i < LED_COUNT; ++i)
  {
   if (distance > MIN_DISTANCE+i*dist_div)
   {
     leds[i] = CRGB(100,0,0);
   }
   else
   {
     leds[i] = CRGB(0,0,0);
   }
  }
  FastLED.show();
  delay(100);
}

Notes

There are many ways to integrate visual output into a project. While it cannot display results as precisely as 16×2 LCDs or OLEDs, using an addressable strip is definitely one of the easiest ways. In this mini-project, the strip acted as a meter, but the possibilities for visual feedback are endless.

Conclusion

LED strips are one of the most popular light sources being used today. Cheap, flexible, and easy to use, they have found themselves a home in almost every maker’s toolbox. With the wide variety of strips available, you are sure to find something that will fit your needs.

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy