Control an RGB light from the Blynk app

Mix any colour with three sliders in the Blynk app, and switch on a smooth rainbow effect that runs on the ESP32.
  • Level:Advanced
  • Duration: 50 min
  • Ages 13+
  • Platform: ESP32

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Control an RGB light from the Blynk app – EEMENTOR STEM™ lesson

In this lesson the Blynk app becomes a remote for a colour light. Three sliders mix red, green and blue on the RGB LED module, and a switch starts a rainbow effect that the ESP32 runs by itself. The colour you choose is saved in the Blynk cloud, so the light comes back in the same colour after a power cut.

What you'll learn

  • How to handle several app controls at once
  • How PWM mixes colours on an RGB LED
  • How to run an animation with a timer while staying connected
  • How the cloud remembers values with Blynk.syncVirtual()

What you need

  • ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
  • RGB LED module (45-in-1 Sensor Kit)
  • 3 resistors, 220 Ω (ESP32 IoT Starter Kit)
  • Breadboard and jumper wires (ESP32 IoT Starter Kit)
  • A free Blynk account and the Blynk IoT app on a phone
  • A 2.4 GHz Wi-Fi network

Wiring

Breadboard wiring diagram: Control an RGB light from the Blynk app
Breadboard view. Check each connection against the wiring table below.
From To
ESP32 GPIO 27 220 Ω resistor, then module R
ESP32 GPIO 26 220 Ω resistor, then module G
ESP32 GPIO 25 220 Ω resistor, then module B
Module − GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Set up Blynk

In the Blynk console, create a new template called RGB light (hardware ESP32, connection type Wi-Fi) and add these datastreams. Lesson 1 shows each step in detail.

Virtual pin Name Data type Range Phone widget
V0 Red Integer 0 – 255 Slider
V1 Green Integer 0 – 255 Slider
V2 Blue Integer 0 – 255 Slider
V3 Rainbow Integer 0 – 1 Button (switch mode)

Save the template, create a new device from it and copy its three BLYNK_… lines. In the phone app, open the device, tap the wrench icon and add three Sliders (V0, V1, V2) and a Button in switch mode (V3). Link each widget to its datastream.

Example Blynk web dashboard and phone app for: Control an RGB light from the Blynk app
Example dashboard with sample values. Your layout may look different.

The sketch

This sketch uses analogWrite(), which needs ESP32 board package version 3.0 or newer (Tools → Board → Boards Manager).

Replace the three BLYNK_… lines at the top with the ones from your device's Device info page, and add your Wi-Fi name and password (2.4 GHz only). The BLYNK_… lines must stay above #include <BlynkSimpleEsp32.h>.

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "RGB light"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#define BLYNK_PRINT Serial          // show connection messages in the Serial Monitor

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

const int RED_PIN = 27;
const int GREEN_PIN = 26;
const int BLUE_PIN = 25;

BlynkTimer timer;
int red = 255, green = 80, blue = 0;
bool rainbow = false;
int hue = 0;

void show(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

BLYNK_WRITE(V0) { red = param.asInt();   if (!rainbow) show(red, green, blue); }
BLYNK_WRITE(V1) { green = param.asInt(); if (!rainbow) show(red, green, blue); }
BLYNK_WRITE(V2) { blue = param.asInt();  if (!rainbow) show(red, green, blue); }
BLYNK_WRITE(V3) {
  rainbow = param.asInt();
  if (!rainbow) show(red, green, blue);   // back to your own colour
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V0, V1, V2, V3);     // restore the last colour and mode
}

// One small step around the colour wheel (hue 0 ... 359)
void rainbowStep() {
  if (!rainbow) return;
  hue = (hue + 1) % 360;
  int part = hue / 60;
  int rise = (hue % 60) * 255 / 60;     // 0 ... 255 within each sixth
  int fall = 255 - rise;
  switch (part) {
    case 0: show(255, rise, 0); break;  // red to yellow
    case 1: show(fall, 255, 0); break;  // yellow to green
    case 2: show(0, 255, rise); break;  // green to cyan
    case 3: show(0, fall, 255); break;  // cyan to blue
    case 4: show(rise, 0, 255); break;  // blue to magenta
    default: show(255, 0, fall);        // magenta to red
  }
}

void setup() {
  Serial.begin(115200);
  show(red, green, blue);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(30L, rainbowStep);   // about 11 seconds per rainbow
}

void loop() {
  Blynk.run();
  timer.run();
}

How it works

Each slider has its own virtual pin and its own BLYNK_WRITE() function, which saves the new value and updates the LED. PWM switches each colour on and off very fast; analogWrite(pin, 128) keeps it on about half the time, and your eye mixes the three colours into one. The rainbow runs on the ESP32 itself: every 30 ms the timer moves one degree around a colour wheel, fading one colour up or down at a time. Because it runs from a timer, Blynk.run() keeps running between steps, and the switch still responds instantly.

Try this next

  • Add a Slider on V4 for the rainbow speed and change the timer interval with timer.changeInterval().
  • Replace the three sliders with Blynk's zeRGBa colour picker widget.
  • Turn the light on automatically at sunset with the photoresistor module from lesson 1.

Troubleshooting

  • The device shows "Offline" in the app: check the Serial Monitor at 115200 baud. "Invalid auth token" means the BLYNK_AUTH_TOKEN line is wrong; if it never connects, check the Wi-Fi name and password, and that your network is 2.4 GHz.
  • A colour doesn't light: check that resistor and wire; the module's pin order may differ from the table.
  • Red and blue are swapped: swap the pin numbers in the sketch.
  • Compile error about analogWrite: update the ESP32 board package to version 3.0 or newer.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

ESP32 IoT Starter Kit | Arduino IDE Compatible | Free Tutorials $57.00

Shop the kit