Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you. Additionally, some links direct to products from our own brand, DIYables .
Buy Note: To simplify the wiring process, we recommend using the LED Module, which comes with a built-in resistor.
About LED and Rotary Encoder
If you do not know about LED and rotary encoder (pinout, how it works, how to program ...), learn about them in the following tutorials:
This image is created using Fritzing. Click to enlarge image
Arduino Code
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rotary-encoder-led */#include <Servo.h>#define CLK_PIN 2#define DT_PIN 3#define SW_PIN 4#define LED_PIN 9#define DIRECTION_CW 0 // clockwise direction#define DIRECTION_CCW 1 // counter-clockwise directionint counter = 0;intdirection = DIRECTION_CW;int CLK_state;int prev_CLK_state;int brightness = 125; // middle valuevoidsetup() {Serial.begin(9600);// configure encoder pins as inputspinMode(CLK_PIN, INPUT);pinMode(DT_PIN, INPUT);// read the initial state of the rotary encoder's CLK pin prev_CLK_state = digitalRead(CLK_PIN);pinMode(LED_PIN, OUTPUT);}voidloop() {// read the current state of the rotary encoder's CLK pin CLK_state = digitalRead(CLK_PIN);// If the state of CLK is changed, then pulse occurred// React to only the rising edge (from LOW to HIGH) to avoid double countif (CLK_state != prev_CLK_state && CLK_state == HIGH) {// if the DT state is HIGH// the encoder is rotating in counter-clockwise direction => decrease the counterif (digitalRead(DT_PIN) == HIGH) {direction = DIRECTION_CCW; counter--; brightness -= 10; // you can change this value } else {// the encoder is rotating in clockwise direction => increase the counterdirection = DIRECTION_CW; counter++; brightness += 10; // you can change this value }if (brightness < 0) brightness = 0;elseif (brightness > 255) brightness = 255;// sets the brightness of LED according to the counteranalogWrite(LED_PIN, brightness);Serial.print("COUNTER: ");Serial.print(counter);Serial.print(" | BRIGHTNESS: ");Serial.println(brightness); }// save last CLK state prev_CLK_state = CLK_state;}
Quick Steps
Connect Arduino to PC via USB cable
Open Arduino IDE, select the right board and port
Copy the above code and open with Arduino IDE
Click Upload button on Arduino IDE to upload code to Arduino
Open Serial Monitor
Rotate the rotary encoder
See the LED's brightness
See the result on Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
Read the line-by-line explanation in comment lines of source code!
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!