Pinterest Stumbleupon Whatsapp
Advertisement

The rise of LED lighting has been stratospheric, and it’s easy to see why. They are cheap to produce, consume significantly less power than other lighting options, and in most cases don’t get hot, making them safe for a variety of uses.

One of the most common LED products is the LED strip. In this article, we will cover how to set up the two most common types with an Arduino. These projects are very simple, and even if you are a beginner with Arduino Getting Started With Arduino: A Beginner's Guide Getting Started With Arduino: A Beginner's Guide Arduino is an open-source electronics prototyping platform based on flexible, easy-to use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. Read More or DIY electronics Beginner's Electronics: 10 Skills You Need to Know Beginner's Electronics: 10 Skills You Need to Know Many of us have never even touched a soldering iron - but making things can incredibly rewarding. Here's ten of the most basic DIY electronics skills to help you get started. Read More , you will be able to do this.

We will also use the Arduino IDE to control them. This project uses an Arduino Uno, though you could use almost any compatible board (such as the NodeMCU Meet the Arduino Killer: ESP8266 Meet the Arduino Killer: ESP8266 What if I told you a there's an Arduino-compatible dev board with built-in Wi-Fi for less than $10? Well, there is. Read More ).

Choose Your Strip

When shopping for LED strips there are a few things to consider. First is functionality. If you are planning to use the strips mostly for ambient lighting, then a simple 12v RGB LED strip (SMD5050) would be the right choice.

Many of these strips come with an infrared remote to control them, though in this project we will be using an Arduino to instead. Spend a little time shopping around, at the time of writing it was possible to get these strips for as little as $1 per meter.

LED Strip Lights
Image Credit: phanu suwannarat via Shutterstock

If you want something a little higher tech, consider the WS2811/12/12B. These strips (sometimes referred to as Neopixels) have integrated chipsets which allow them to be addressed individually. This means they are capable of more than just ambient lighting.

You can use them to build a cheap LED pixel display from scratch. You can even use them to make your own personal indoor storm cloud.

LED Pixel Display

These strips only require 5v to power them. While it is possible to power small amounts of them directly from an Arduino board, it is generally a good idea to use a separate 5V power supply to save yourself from the smell of fried Arduino. If you are looking for individually programmable LEDs, these are for you. At the time of writing, they are available for around $4 per meter.

Another thing to consider is where these strips are likely to be used. Both of these types of strip come in varying lengths, LED densities (the number of LEDs per meter), and differing degrees of weatherproofing.

When looking at LED strips, pay attention to the numbers on the listing. Usually, the first number will be the number of LEDs per meter, and the letters IP followed by numbers will be its weatherproofing. For example, if the listing says 30 IP67, this means there will be 30 LEDs per meter. The 6 denotes it is completely sealed from dust, and the 7 means it is protected against temporary submersion in water. Learn about IP Ratings if you are interested in knowing more.

Once you have your chosen LED strip, it’s time to link it up with an Arduino. Let’s start with the SMD5050.

Getting Connected

In order to connect up a 12v LED strip to an Arduino, you will need a few components:

  • 12v RGB LED strip (SMD5050)
  • 1 x Arduino Uno (any compatible board will do)
  • 3 x 10k Ohm Resistors
  • 3 x Logic Level N-channel MOSFETs
  • 1 x Breadboard
  • Hookup wires
  • 12v Power Supply

Electronic Components Required

Before setting up the circuit, let’s talk about MOSFETs.

Whenever you are controlling something which is higher voltage than your microcontroller, you need something in between to stop your board getting fried. One of the simpler ways to do this is to use a MOSFET. By sending pulse width modulation (PWM) signals to the gate leg, it’s possible to control how much power passes between the drain and source legs. By passing each of the LED strip’s colors through the MOSFET, you can control the brightness of each individual color on the LED strip.

When using microcontrollers, it is important to use logic level components in order to ensure things work the way you want them to. Make sure your MOSFETs are logic level and not standard.

Set up your circuit like this:

Arduino MOSFET Circuit

  1. Connect Arduino pins 9, 6, and 5 to the gate legs of the three MOSFETs, and connect a 10k resistor in line with each to the ground rail.
  2. Connect the Source legs to the ground rail.
  3. Connect the Drain legs to the Green, Red, and Blue connectors on the LED strip.
  4. Connect the power rail to the +12v connector of the LED strip (note that in this image the power wire is black to match the colors of the connectors on my LED strip).
  5. Connect the Arduino ground to the ground rail.
  6. Connect your 12v power supply to the power rails.

Most LED strips have Dupont connectors, which are easy to connect to. If yours don’t you may need to solder wires to the LED strip. Don’t panic if you are fairly new to soldering, it’s an easy job, and we have a guide to getting started with soldering Learn How to Solder, with These Simple Tips and Projects Learn How to Solder, with These Simple Tips and Projects Are you a little intimidated by the thought of a hot iron and molten metal? If you want to start working with electronics, you're going to need to learn to solder. Let us help. Read More should you need it.

We will be powering our Arduino board by USB for this project. You could choose to power your board using the VIN pin, but make sure you know the power limitations for your board before doing this.

When your circuit is complete it should look something like this:

Arduino MOSFETs on Breadboard

Now that you have connected everything, it’s time to make a simple Arduino sketch to control it.

Fade It Up

Connect your Arduino board to your computer via USB and open up the Arduino IDE. Make sure you have the correct board and port number selected for your board in the Tools > Board and Tools > Port menus. Open a new sketch and save it with an appropriate name.

This sketch will fade the lights in one color at a time, keep them on for a few seconds, then fade them out until they are off again. You can follow through here and make the sketch yourself, or simply download the complete code from GitHub.

Begin by defining which pins will be used to control the MOSFETs.

#define RED_LED 6
#define BLUE_LED 5
#define GREEN_LED 9

Next you need some variables. Create an overall brightness variable, along with a variable for each individual color’s brightness. We will only be using the main brightness variable for turning the LEDs off, so set it to the maximum brightness value of 255 here.

you will also need to create a variable to control how fast the fading will happen.

int brightness = 255;

int gBright = 0;
int rBright = 0;
int bBright = 0;

int fadeSpeed = 10;

In your setup function we will set our Arduino pins to output. We will also call a couple of functions with a 5 second delay in between. These functions don’t exist yet, but don’t worry, we’ll get to them.

void setup() {
   pinMode(GREEN_LED, OUTPUT);
   pinMode(RED_LED, OUTPUT);
   pinMode(BLUE_LED, OUTPUT);

   TurnOn();
   delay(5000);
   TurnOff();
}

Now create the TurnOn() method:

void TurnOn() { 
   for (int i = 0; i < 256; i++) {
       analogWrite(RED_LED, rBright);
       rBright +=1;
       delay(fadeSpeed);
   }
 
   for (int i = 0; i < 256; i++) {
       analogWrite(BLUE_LED, bBright);
       bBright += 1;
       delay(fadeSpeed);
   } 

   for (int i = 0; i < 256; i++) {
       analogWrite(GREEN_LED, gBright);
       gBright +=1;
       delay(fadeSpeed);
   } 
}

These three for loops take each color up to its full brightness over a time specified by the fadeSpeed value.

Finally you need to create the TurnOff() method:

void TurnOff() {
   for (int i = 0; i < 256; i++) {
       analogWrite(GREEN_LED, brightness);
       analogWrite(RED_LED, brightness);
       analogWrite(BLUE_LED, brightness);
 
       brightness -= 1;
       delay(fadeSpeed);
   }
}

void loop() {

}

This method applies our brightness variable to all three color pins and reduces them to zero over a period of time. We need an empty loop method here too, in order to avoid compilation errors.

Once you have completed this sketch, save it. Verify the sketch and upload it to your Arduino board. If you are getting errors, check through the code again for any pesky typos or missing semicolons.

Now you should see your LED strip ramp up each color individually, holding the white color for 5 seconds, and then uniformly fade to nothing:

LED Strips Flashing

If you are having any difficulties, double check your wiring and code again.

This project is a simple way to get started, but the ideas covered in it can be expanded on to make really effective lighting. With just a few more components you could create your own sunrise alarm. If you got a starter kit with your Arduino you could use any button or sensor to trigger your LEDs when you enter the room, for example:

Arduino Motion Controlled Lighting

Now that we have covered the SMD5050s, let’s move on to the WS2812B strips.

Bright Ideas

These strips require fewer components to get them running, and there is some leeway as to exactly what values of components you can use. The capacitor in this circuit makes sure that the 5v LEDs get a steady power supply. The resistor ensures the data signal received from the Arduino is free from any interference.

You will need:

  • WS2811/12/12B 5v LED strip (all three models have integrated chips and work much the same way)
  • 1 x Arduino Uno (or similar compatible board)
  • 1 x 220-440 Ohm Resistor (anything between these two values is fine)
  • 1 x 100-1000 microFarad Capacitor (anything between these two values is fine)
  • Breadboard and hook up wires
  • 5V power supply

Set up your circuit as shown in the diagram:

Arduino Circuit

Take note that the capacitor must be the correct orientation. You can tell which side attaches to the ground rail by looking for the minus (-) sign on the body of the capacitor.

This time round we are powering the Arduino using the 5v Power supply. This makes the project stand alone once we are done, though there are important things to note here.

Firstly, make sure that your board can take 5v power in before attaching it to the power source. Almost all development boards run at 5v through the USB port, but the power input pins on some can sometimes skip the voltage regulators and turn them into toast.

Also, it is good practice to make sure that multiple separate power sources are not connected to the Arduino — disconnect the USB cable whenever you are using an external power supply.

Once you are plugged in it should look like this:

Arduino Completed Circuit

Now that our LED strip is wired in, let’s move on to the code.

Dancing Lights

In order to safely program our board, disconnect the VIN line from the power line. You’ll reattach it later.

Attach your Arduino to the computer and open the Arduino IDE. Check that you have the correct board and port number selected in the Tools > Board and Tools > Port menus.

We will be using the FastLED library to test out our setup. You can add the library by clicking on Sketch > Include Library > Manage Libraries and searching for FastLED. Click install, and the library will be added to the IDE.

Under File > Examples > FastLED select the DemoReel100 sketch. This sketch cycles various things which can be done with the WS2812 LED strips, and is incredibly easy to set up.

All you need to change is the DATA_PIN variable so that it matches pin 13, and the NUM_LEDS variable to define how many LEDs are in the strip you are using. In this case, I am using only a small line of 10 LEDS cut from a longer strip. Use more for a bigger light show!

Arduino Code

That’s it! Upload the sketch to your board, disconnect the USB cable and turn on your 5v power supply. Finally, reattach the Arduino’s VIN to the power line and watch the show!

Arduino Finished Circuit

If nothing happens, check over your wiring and that you specified the correct Arduino pin in the demo sketch.

Endless Possibilities

The demo sketch shows off some of the many possible combinations of effects that can be achieved with the WS2812 strips. Alongside being a step up from regular LED strips, they can be put to practical use too. A good next project would be building your own ambilight for your media center.

While these strips are definitely more functional than the SMD5050s, don’t discount the standard 12v LED strips quite yet. They are unbeatable in terms of price, and there are a huge amount of applications for them. Here are 22 22 Ways to Light Up Your Life With RGB LED Strips 22 Ways to Light Up Your Life With RGB LED Strips You can buy five meters of RGB LEDs strip lighting for under $10 -- so here's some great project ideas to use them around your home. Read More to get you started.

Learning to work with LED strips is a good way to get familiar with basic programming on the Arduino, but the best way to learn is by tinkering. Modify the above code and see what you can do!

This is a great introduction into learning about electronics, and there are many great free resources So You Just Bought an Arduino Starter Kit. What Now? So You Just Bought an Arduino Starter Kit. What Now? So you just bought your first Arduino starter kit -- or you're planning on buying one soon -- but you don't know where to begin? Don't worry! Read More out there to help you learn more.

Have you been using LED strips in your Arduino projects? What are you using them for? Let us know in the comment section below!

Image Credits: mkarco/Shutterstock

Leave a Reply

Your email address will not be published. Required fields are marked *