Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » RGB LED Color Control using Rotary Encoder and Arduino
Arduino Projects

RGB LED Color Control using Rotary Encoder and Arduino

Mamtaz AlamBy Mamtaz AlamUpdated:August 22, 20221 Comment3 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
RGB LED Rotary Encoder Arduino
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

RGB LED Color Control using Rotary Encoder and Arduino

In this post, we will learn about RGB LED Color Control using Rotary Encoder and Arduino. We will be rotating the Rotary Encoder to assign the value. On the basis of that value, the red, green, and blue colors will combine to give a new color. Here I am using only RG Color LED that has only red and green color but code is designed to control all the 3 color LEDs. You can use the RGB Color LED according to this code.
Rotary Encoder

A rotary encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to analog or digital output signals. There are two main types of rotary encoder: absolute and incremental. The output of an absolute encoder indicates the current shaft position, making it an angle transducer. The output of an incremental encoder provides information about the motion of the shaft, which typically is processed elsewhere into information such as position, speed, and distance.

To learn about Rotary Encoder and its types with application, advantage, and working visit here:

What is a Rotary Encoder? Construction & Working of Rotary Encoder


Bill of Materials

S.N.Components NameQuantityPurchase Links
1Arduino UNO Board1Amazon | AliExpress
2Rotary Encoder1Amazon | AliExpress
3RGB LED1Amazon | AliExpress
5Connecting Wires10Amazon | AliExpress
6Breadboard1Amazon | AliExpress

Circuit Diagram & Connection:

The circuit diagram below is a simple demonstration of how to control RGB LED Color using Rotary Encoder and Arduino. Assemble the same circuit on a breadboard or PCB.

RGB LED Arduino Circuit rotary encoder


How Rotary Encoder Works?

The encoder has a disk with evenly spaced contact zones that are connected to the common pin C and two other separate contact pins A and B, as illustrated below.

Rotary Encoder Working

When the disk will start rotating step by step, pins A and B will start making contact with the common pin and the two square wave output signals will be generated accordingly.


Any of the two outputs can be used for determining the rotated position if we just count the pulses of the signal. However, if we want to determine the rotation direction as well, we need to consider both signals at the same time.

Rotary Encoder Waveform

We can notice that the two output signals are displaced at 90 degrees out of phase from each other. If the encoder is rotating clockwise the output A will be ahead of output B.

So if we count the steps each time the signal changes, from High to Low or from Low to High, we can notice at that time the two output signals have opposite values. Vice versa, if the encoder is rotating counter-clockwise, the output signals have equal values. So considering this, we can easily program our controller to read the encoder position and the rotation direction.


Source Code/Program:

rgb led arduino



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
int encPin1 = 2; //Left turn of encoder to digital pin 2
int encPin2 = 3; //Right turn of encoder to digital pin 3
int SencPin = 4; //Push Encoder switch to digital pin 4
int ledPin = 13; // onboard LED for validation
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11
// Define Variables
int stateNum = 0; // current State number
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
int RGBVal = 0; //Value for RGB Blend
static boolean moving = false;
volatile int encVal = 0;
unsigned int lastEncVal = 1;
boolean enc1 = false;
boolean enc2 = false;
 
void setup()
{
//Define Inputs
pinMode(ledPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
pinMode(encPin1, INPUT);
pinMode(encPin2, INPUT);
pinMode(SencPin, INPUT);
//Turn on resistors for encoder/switch
digitalWrite(encPin1, HIGH);
digitalWrite(encPin2, HIGH);
digitalWrite(SencPin, HIGH);
Serial.begin(9600); //Start Logging
//encoder interrupts
attachInterrupt(0, intrEncChange1, CHANGE);
attachInterrupt(1, intrEncChange2, CHANGE);
}
 
void intrEncChange1() //Read on interrupt Right turn - Fast +4
{
if(moving)
delay(1);
if(digitalRead(encPin1) == enc1)
return;
enc1 = !enc1;
if(enc1 && !enc2)
encVal += 4;
moving = false;
}
 
void intrEncChange2() //Read on interrupt Left turn - Slow -2
{
if(moving)
delay(1);
if(digitalRead(encPin2) == enc2)
return;
enc2 = !enc2;
if(enc2 && !enc1)
encVal -= 2;
moving = false;
}
 
void loop()
{
{
buttonState = digitalRead(SencPin); //Read Button state
if (buttonState != lastButtonState) //Compare to last state
{
if (buttonState == HIGH) //If button is pushed, incriment stateNum and blink onboard LED
{
stateNum++;
if (stateNum > 8) stateNum = 0; //Defines and loops Number of possible states
Serial.println("on");
Serial.print("State Number: ");
Serial.println(stateNum);
digitalWrite(ledPin, HIGH);
}
else
{
Serial.println("off");
digitalWrite(ledPin, LOW);
}
 
lastButtonState = buttonState;
} //update button state to loop
//delay(20); //Delay to avoid bounce
}
if (encVal > 255) encVal = 0; //Loop Values
if (encVal < 0) encVal = 255;
if (encVal != lastEncVal) //Compare to previous pot value
{
lastEncVal = encVal; //Update encVal to reflect new value
Serial.print("Encoder Value: ");
Serial.println(encVal);
digitalWrite(ledPin, HIGH); //Blink onboard LED
}
else
{
digitalWrite(ledPin, LOW);
//delay(100); //Delay to avoid bounce
}
 
//Begin States section
 
if (stateNum == 0) //All LEDs make white, off to brightest
{
Serial.println("0 White");
grnVal = 255 - encVal; //Common Anode LED means inverse values
bluVal = 255 - encVal;
redVal = 255 - encVal;
}
else if (stateNum == 1) //Blend Green to Blue
{
Serial.println("1 Green to Blue");
grnVal = encVal;
bluVal = 255 - encVal;
redVal = 255;
}
else if (stateNum == 2) //Blend Blue to Red
{
Serial.println("2 Blue to Red");
bluVal = encVal;
redVal = 255 - encVal;
grnVal = 255;
}
else if (stateNum == 3) //Blend Red to Green
{
Serial.println("3 Red to Green");
redVal = encVal;
grnVal = 255 - encVal;
bluVal = 255;
}
else if (stateNum == 4) //Blend RGB
{
Serial.println("4 Blend All R-G-B-R");
if (encVal < 86) // Lowest third of range (0-85)
{
RGBVal = (encVal * 3) ; // Normalize to 0-255
 
redVal = RGBVal; // Red from full to off
grnVal = 255 - RGBVal; // Green from off to full
bluVal = 255; // Blue off
}
else if (encVal < 171) // Middle third of range (86-170)
{
RGBVal = ( (encVal - 86) * 3); // Normalize to 0-255
 
redVal = 255; // Red off
grnVal = RGBVal; // Green from full to off
bluVal = 255 - RGBVal; // Blue from off to full
}
else // Upper third of range (171-255)
{
RGBVal = ( (encVal - 171) * 3); // Normalize to 0-255
 
redVal = 255 - RGBVal; // Red from off to full
grnVal = 255; // Green off
bluVal = RGBVal; // Blue from full to off
}
}
else if (stateNum == 5) //Red Dim
{
Serial.println("5 Red Dim");
redVal = 255 - encVal;
grnVal = 255;
bluVal = 255;
}
else if (stateNum == 6) //Green Dim
{
Serial.println("6 Green Dim");
redVal = 255;
grnVal = 255 - encVal;
bluVal = 255;
}
else if (stateNum == 7) //Blue Dim
{
Serial.println("7 Blue Dim");
redVal = 255;
grnVal = 255;
bluVal = 255 - encVal;
}
else if (stateNum == 8) //Purple Dim
{
Serial.println("8 Purple Dim");
redVal = 255 - encVal;
grnVal = 255;
bluVal = 255 - encVal;
}
 
//Send results to LEDs
analogWrite(redPin, redVal);
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
}


Video Tutorial & Explanation:

Rotary Encoder Tutorial | How Rotary Encoder works & How to use it with Arduino
Watch this video on YouTube.

You can check the application of Rotary Encoder here: DC Motor Speed Control with NRF24L01 Rotary Encoder & Arduino

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleHow to use a Rotary Encoder with Arduino – Full Guide
Next Article Arduino Ultrasonic Range Finder with HC-SR04 on OLED Display

Related Posts

DC Energy Meter using Arduino

Build a DC Energy Meter using Arduino – 32V/5A

Updated:August 26, 20252K
Interfacing ADXL375 Accelerometer with Arduino

Interfacing ADXL375 Accelerometer with Arduino (±200g)

Updated:June 28, 2025
PZEM-004T Arduino Energy Meter

DIY AC Energy Meter using PZEM-004T & Arduino

Updated:March 6, 20258K
Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Updated:February 2, 20259K
Password Based Door Lock Security System Using Arduino & Keypad

Password Based Door Lock Security System Using Arduino & Keypad

Updated:February 2, 20252436K
Earthquake Detector Alarm with with Accelerometer & Arduino

Earthquake Detector Alarm with Accelerometer & Arduino

Updated:February 2, 2025661K
View 1 Comment

1 Comment

  1. meierjoe on October 30, 2021 2:56 PM

    Hi I get this error ” ‘analogWrite’ was not declared in this scope”

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • Designing of MPPT Solar Charge Controller using Arduino
    Designing of MPPT Solar Charge Controller using Arduino
  • How to use INA226 DC Current Sensor with Arduino
    How to use INA226 DC Current Sensor with Arduino
  • ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
    ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
  • IoT Based Electricity Energy Meter using ESP32 & Blynk
    IoT Based Electricity Energy Meter using ESP32 & Blynk
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.