Arduino - Rotary Encoder LED

by DIYables in Circuits > Arduino

7 Views, 0 Favorites, 0 Comments

Arduino - Rotary Encoder LED

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-rotary-encoder-led

This build uses a rotary encoder to control the brightness of an LED. Turning the knob clockwise increases brightness, turning it counter-clockwise decreases it, and the current counter and brightness values are printed to the Serial Monitor so you can follow along while you turn the encoder.

Supplies

Wiring

Connect the rotary encoder's CLK pin to Arduino pin 2, the DT pin to pin 3, and the SW pin to pin 4. Connect the LED to pin 9. Using an LED Module is recommended since it has a built-in resistor, which simplifies the wiring. If you use a plain LED instead, wire it through a 220 ohm resistor on the breadboard.

How It Works

The Arduino continuously reads the CLK pin of the encoder. When CLK changes from LOW to HIGH, a pulse has occurred. At that rising edge, the code checks the DT pin: if DT is HIGH the encoder turned counter-clockwise, if DT is LOW it turned clockwise. Each pulse changes the brightness value by 10, clockwise increases it and counter-clockwise decreases it. The brightness is clamped between 0 and 255 and then written to the LED pin with analogWrite, which uses PWM to dim or brighten the LED.

Code

The sketch sets CLK and DT as inputs and LED_PIN as an output, then reads the initial CLK state. In the loop it compares the current CLK state to the previous one, reacting only on the rising edge to avoid counting the same pulse twice. Depending on the DT pin it updates a direction variable and a counter, adjusts the brightness variable by plus or minus 10, clamps it to the 0-255 range, writes it to the LED with analogWrite, and prints the counter and brightness to the Serial Monitor.

arduino-rotary-encoder-led.ino - the sketch from the tutorial, unchanged.

/*
* 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 direction

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
int brightness = 125; // middle value

void setup() {
Serial.begin(9600);

// configure encoder pins as inputs
pinMode(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);
}

void loop() {

// 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 count
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
// if the DT state is HIGH
// the encoder is rotating in counter-clockwise direction => decrease the counter
if (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 counter
direction = DIRECTION_CW;
counter++;
brightness += 10; // you can change this value
}

if (brightness < 0)
brightness = 0;
else if (brightness > 255)
brightness = 255;

// sets the brightness of LED according to the counter
analogWrite(LED_PIN, brightness);

Serial.print("COUNTER: ");
Serial.print(counter);
Serial.print(" | BRIGHTNESS: ");
Serial.println(brightness);
}

// save last CLK state
prev_CLK_state = CLK_state;
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-rotary-encoder-led