Built-in project · Arduino Uno R3

12 V RGB strip color fade on an Arduino Uno

A 12 V RGB LED strip behind three IRLZ44N MOSFETs: the Uno fades it around the color wheel with PWM on pins 9, 10 and 11. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • MOSFETs as switches for high power loads
  • PWM through a MOSFET sets brightness
  • Common anode strips
  • Sharing ground between the Uno and 12 V supply
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

A 12 V RGB LED strip is too much for Uno pins, so three IRLZ44N MOSFETs act as switches. The Uno's pins 9, 10 and 11 drive the gates, and each MOSFET connects one color of the strip to ground. The 12 V supply powers the strip.

The strip is common anode, with +12V shared. A color lights when its MOSFET pulls that pad to ground, and PWM on the gate sets how much of the time that happens.

The sketch fades red to green, green to blue and blue back to red, in steps of 5 every 15 ms. One color fades in as the last fades out.

Parts list

7 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
MOSFET (1) pin SArduino Uno R3 pin GND
MOSFET (2) pin SArduino Uno R3 pin GND
MOSFET (3) pin SArduino Uno R3 pin GND
MOSFET (3) pin GArduino Uno R3 pin 11
MOSFET (2) pin GArduino Uno R3 pin 10
MOSFET (1) pin GArduino Uno R3 pin 9
DC voltage source pin V+LED strip, 12 V RGB pin +12V
MOSFET (1) pin DLED strip, 12 V RGB pin R
MOSFET (2) pin DLED strip, 12 V RGB pin G
MOSFET (3) pin DLED strip, 12 V RGB pin B
DC voltage source pin V-Ground

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A 12 V RGB strip through three MOSFETs, faded around the color wheel.
//
//   strip +12V -> the 12 V supply
//   strip R, G, B -> the drains of three IRLZ44Ns; their sources to GND
//   the gates -> pins 9 (red), 10 (green) and 11 (blue)
//
// The strip is common anode: a color lights while its MOSFET pulls that pad
// to ground, and PWM on the gate sets how much of the time it does.

const uint8_t RED = 9, GREEN = 10, BLUE = 11;

void color(int r, int g, int b) {
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
}

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

void loop() {
  // Red to green, green to blue, blue to red: one color fades in as the last fades out.
  for (int i = 0; i <= 255; i += 5) { color(255 - i, i, 0); delay(15); }
  for (int i = 0; i <= 255; i += 5) { color(0, 255 - i, i); delay(15); }
  for (int i = 0; i <= 255; i += 5) { color(i, 0, 255 - i); delay(15); }
}

Code walkthrough

The key functions of the sketch, and what each one does.

color sketch.ino

Takes three values and writes them with analogWrite to the red, green and blue pins. Every step of the fade calls it, so the rest of the code stays short.

void color(int r, int g, int b) {
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
}

setup sketch.ino

Makes the three gate pins outputs.

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

loop sketch.ino

Runs three for loops. Each moves one color down from 255 and the next up from 0 in steps of 5, waiting 15 ms between steps: red to green, green to blue, then blue to red.

void loop() {
  // Red to green, green to blue, blue to red: one color fades in as the last fades out.
  for (int i = 0; i <= 255; i += 5) { color(255 - i, i, 0); delay(15); }
  for (int i = 0; i <= 255; i += 5) { color(0, 255 - i, i); delay(15); }
  for (int i = 0; i <= 255; i += 5) { color(i, 0, 255 - i); delay(15); }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Nothing lights This project
    Join the supply negative to the Uno GND and the MOSFET sources.
  • The colors are in the wrong order This project
    Pin 9 is red, 10 is green and 11 is blue. Check which MOSFET goes to which strip pad.
  • The strip does not light, or the Arduino pin gets hot. LED strip
    A 12 V strip draws too much for a pin. Use a logic-level MOSFET on each color, with the strip's common wire on +12 V and the MOSFETs switching the low side.
  • Colors are wrong or nothing changes. LED strip
    Join the grounds of the 12 V supply and the Arduino. Check the wire order (R, G, B) on the strip. A long strip can draw several amps, so size the supply to match.
  • The load stays partly on or flickers when the pin is not driving, such as at boot or in input mode. MOSFET
    A floating gate picks up noise. Add a pull-down of about 10 k from gate to source on an N-channel part, or pull a P-channel gate up to its source voltage.
  • The N-channel load is always on or never turns on. Source, gate, and drain legs were mixed up. MOSFET
    Use the N-channel as a low-side switch: load to the supply, drain to the load, source to ground. Check your datasheet pinout, since 2N7000 and BS250 differ. Share the grounds.
  • The circuit sees no voltage or the wrong amount. DC voltage source
    Make sure V+ and V- go to the right nodes. The source floats, so tie one side to your circuit ground. Check the set voltage matches what your parts allow.
  • A part gets too much voltage. DC voltage source
    Check the rating of every part before raising the supply. Use series resistors with LEDs, and do not connect the source straight across a short.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.