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

1×Arduino Uno R3
1×Cabo USB 2.0 tipo A/B (para PC com USB-A)
1×Cabo USB 2.0 tipo C/B (para PC com USB-C)
1×Encoder Rotativo
1×LED Kit
1×LED (red)
1×LED Module
1×(Alternativa) Button and LED Kit
1×220Ω Resistor
1×Protoboard
1×Fios Jumper
1×(Recomendado) Shield com Bornes de Parafuso para Arduino Uno
1×(Recomendado) Sensors/Servo Expansion Shield for Arduino Uno
1×(Recomendado) Shield de Protoboard para Arduino Uno
1×(Recomendado) Case para Arduino Uno
1×(Recomendado) Placa Base de Prototipagem e Kit de Protoboard para Arduino Uno

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

Diagrama de Ligação do Encoder Rotativo e LED com Arduino

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
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
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
Ln 11, Col 1
Arduino Uno on COM15
2

Explicação do Código

Leia a explicação linha por linha nas linhas de comentário do código-fonte!

Vídeo

※ NOSSAS MENSAGENS

  • Fique à vontade para compartilhar o link deste tutorial. Porém, por favor, não use nosso conteúdo em outros sites. Investimos muito esforço e tempo para criar este conteúdo, por favor respeite nosso trabalho!