Skip to main content

7.4 RGB LED Controller

placeholder
RGB LED Controller project demonstration

The goal of this project is to create a smart RGB LED controller that reacts to ambient light using a photoresistor (LDR).
As the light level changes, the LED color smoothly transitions across the spectrum — from red in darkness, through green and blue, and finally to white in bright light.
This project combines analog input and PWM output, demonstrating how sensors and lighting can be linked for dynamic visual effects.

In this documentation you will learn:

  • How to use a photoresistor (LDR) to read ambient light levels.
  • How to use map() to scale analog readings to LED brightness.
  • How to create smooth color transitions using PWM outputs.
  • How to build a full-spectrum RGB LED controller with only one analog sensor.

Hardware required

  • 1× Soldered NULA MINI board
  • 1× RGB LED
  • 1× Photoresistor
  • 3× 339 Ω resistors
  • 1× 10 kΩ resistor
  • Breadboard
  • Jumper wires
  • USB-C cable
placeholder
All components required for RGB LED Controller project

How it works

The photoresistor changes its resistance depending on the light level:

  • Bright light → low resistance → higher analog reading.
  • Dim light → high resistance → lower analog reading.

The NULA MINI reads this analog signal (0–4095) using analogRead(), then maps it to RGB LED intensity values using the map() function.
Depending on the light level, the LED smoothly transitions between red, green, blue, and finally white — showing a dynamic “light mood” effect.

placeholder
Concept: single LDR controlling RGB LED color and brightness

Wiring and connections

ComponentNULA MINI PinDescription
LDR (Photoresistor)IO5Analog input (connect to a 10kΩ voltage divider)
RGB LED Red legIO2PWM output for red channel
RGB LED Green legIO3PWM output for green channel
RGB LED Blue legIO4PWM output for blue channel
Common cathode (LED GND)GNDShared ground connection
LDR with 10kΩ resistor3.3V -> LDR -> pin > resistor -> GNDForms analog voltage divider
placeholder
Wiring diagram for RGB LED Controller

Full code

Below is the full example code for this project:

#define LDR_PIN   5   // Analog input pin for photoresistor
#define RED_PIN 2 // PWM pin for red LED channel
#define GREEN_PIN 3 // PWM pin for green LED channel
#define BLUE_PIN 4 // PWM pin for blue LED channel

int ldrValue = 0; // Photoresistor reading (0–4095)
int r = 0, g = 0, b = 0;

void setup() {
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);

Serial.println("RGB LED Controller with full-spectrum color mapping started");
}

void loop() {
// --- Read light level from photoresistor ---
ldrValue = analogRead(LDR_PIN);
Serial.print("LDR value: ");
Serial.println(ldrValue);

// --- Map LDR value (0–4095) to RGB channels ---
if (ldrValue <= 1365) {
// Dark → transition from Red (255,0,0) to Green (0,255,0)
r = map(ldrValue, 0, 1365, 255, 0);
g = map(ldrValue, 0, 1365, 0, 255);
b = 0;
}
else if (ldrValue <= 2730) {
// Medium → transition from Green (0,255,0) to Blue (0,0,255)
r = 0;
g = map(ldrValue, 1366, 2730, 255, 0);
b = map(ldrValue, 1366, 2730, 0, 255);
}
else {
// Bright → transition from Blue (0,0,255) to White (255,255,255)
r = map(ldrValue, 2731, 4095, 0, 255);
g = map(ldrValue, 2731, 4095, 0, 255);
b = 255;
}

// --- Write PWM values to RGB pins ---
analogWrite(RED_PIN, r);
analogWrite(GREEN_PIN, g);
analogWrite(BLUE_PIN, b);

// --- Debug output ---
Serial.print("RGB: ");
Serial.print(r); Serial.print(", ");
Serial.print(g); Serial.print(", ");
Serial.println(b);

delay(100);
}

Full example

placeholder
Full RGB LED Controller demonstration video

Check out the full example code on the link below:

7.4_RGB_LED_Controller.ino

Project that uses a photoresistor to control an RGB LED, smoothly changing color from red to white based on ambient light intensity.