Arduino - Encoder Rotativo LED
Neste tutorial, vamos aprender a programar o Arduino para controlar o brilho de um LED de acordo com o valor de saída do encoder rotativo.
Hardware Necessário
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Divulgação: Alguns dos links desta seção são links de afiliado da Amazon. Podemos receber uma comissão pelas compras feitas por meio desses links, sem custo adicional para você. Agradecemos o seu apoio.
Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.
Sobre o LED e o Encoder Rotativo
Se você ainda não conhece o LED e o encoder rotativo (pinagem, como funcionam, como programar ...), aprenda sobre eles nos seguintes tutoriais:
Diagrama de Ligação

This image is created using Fritzing. Click to enlarge image
Código Arduino
/*
* Este código de Arduino foi desenvolvido por pt.newbiely.com
* Este código de Arduino é disponibilizado ao público sem nenhuma restrição.
* Para tutoriais completos e diagramas de ligação, visite:
* https://pt.newbiely.com/tutorials/arduino/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;
}
Passos Rápidos
- Conecte o Arduino ao PC via cabo USB
- Abra o Arduino IDE, selecione a placa e a porta corretas
- Copie o código acima e abra-o no Arduino IDE
- Clique no botão Upload no Arduino IDE para carregar o código no Arduino
- Abra o Serial Monitor
- Gire o encoder rotativo
- Observe o brilho do LED
- Veja o resultado no Serial Monitor
8
Serial.println("Hello World!");
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
COUNTER: 1 | BRIGHTNESS: 135
COUNTER: 2 | BRIGHTNESS: 145
COUNTER: 3 | BRIGHTNESS: 155
COUNTER: 4 | BRIGHTNESS: 165
COUNTER: 5 | BRIGHTNESS: 175
COUNTER: 6 | BRIGHTNESS: 185
COUNTER: 7 | BRIGHTNESS: 195
Explicação do Código
Leia a explicação linha por linha nas linhas de comentário do código-fonte!