Arduino rotary encoder for controlling leds.

Mr.ElectroUino
0

Arduino Rotory Encoder.
Rotory Encoder projects

Arduino rotary encoder projects for controlling leds.

This blog will teach you how to control LEDs with a rotary encoder and an Arduino Uno. So basically, what we are going to do here is turn on/off the LEDs by the menu, which is displayed on a 16 x 2 LCD, and control the menu by a Rotary encoder.

By rotating the rotary encoder clockwise or anticlockwise, you will see that the menu changes accordingly. To turn the LEDs on or off, select the ones you want to turn on/off. After you have selected the LEDs you want to turn on or off, simply press the rotary encoder.

 Component requirements: 

1. Arduino Uno
2. Rotary encoder.
3. 16 x 2 LCD display.
4. LED     x     4
5. Resistor (220om)     x     4
6. Breadboard.
7. Jumper Wires.

 Circuit Diagram: 

arduino rotary encoder wiring
Circuit Diagram
 Pin Connection: 

1. Start by connecting an Arduino Uno to a breadboard.
  • Connect Arduino 5v pin to breadboard positive side.
  • Connect Arduino GND to the breadboard GND side.
2. Take a led and connect its GND pin to the GND side of the breadboard.
3. A 220-ohm resistor should be connected to each of the positive pins of the LEDs to protect them from high currents.
  • Connect the first resistor end to pin 8 of the Arduino.
  • Connect the second resistor end to pin 7 of the Arduino.
  • Connect the third resistor end to pin 6 of the Arduino.
  • Connect the fourth resistor end to pin 5 of the Arduino.
4. Make the connection between the rotary encoder and the Arduino uno.
  • Connect the rotary encoder GND to pin GND of the Arduino.
  • Connect the rotary encoder VCC to pin 5v of the Arduino
  • Connect the rotary encoder SW to pin 4 of the Arduino
  • Connect the rotary encoder DT to pin 3 of the Arduino
  • Connect the rotary encoder CLK to pin 3 of the Arduino
5. Next, connect the 16 x 2 LCD display to the Arduino uno.
  • You should connect the GND of the LCD to Arduino GND pin.
  • You should connect the VCC of the LCD to Arduino 5v pin.
  • You should connect the SDA of the LCD to Arduino A4 pin.
  • You should connect the SCL of the LCD to Arduino A5 pin.
Now the circuit is completed.
6. let's jump to the coding part
7. open Arduino IDE

 Coding: 

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2);

// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4

int currentStateCLK;
int lastStateCLK;
unsigned long lastButtonPress = 0;

int leds [] = {5, 6, 7, 8};

unsigned int menu = 0;
int ledStatus[5] = {0};
int timer = 1000;

void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
  lcd.clear();

  // Set encoder pins as input
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);

  for (int i = 0; i < 4; i++) {
    pinMode(leds[i], OUTPUT);
  }

  menu = 0;
  updateMenu();
}

void loop() {
  currentStateCLK = digitalRead(CLK);

  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1) {
    if (digitalRead(DT) != currentStateCLK) {
      menu --;
      updateMenu();
    } else {
      menu ++;
      updateMenu();
    }
  }
  lastStateCLK = currentStateCLK;

  int btnState = digitalRead(SW);
  if (btnState == LOW) {
    if (millis() - lastButtonPress > 50) {
      executeAction();
      updateMenu();
    }
    lastButtonPress = millis();
  }
  delay(1);
}

void printlcd(){
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("LED Controller");
      lcd.setCursor(0, 1);
  }
void updateMenu() {
  
  switch (menu) {
    case 0:
      menu = 1;
      break;
    case 1:
      printlcd();
      lcd.print("     Led 1>"); 
      break;
    case 2:
      printlcd();
      lcd.print("    <Led 2>");
      break;
    case 3:
      printlcd();
      lcd.print("    <Led 3>");
      break;
    case 4:
      printlcd();
      lcd.print("    <Led 4");
      break;
    case 5:
      menu = 4;
      break;
  }
}
void executeAction() {
  switch (menu) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
  }
}

void action1() {
  if (ledStatus[menu] != 0) {
    ledStatus[menu] = 0;
    lcd.clear();
    lcd.print("> LED 1 OFF...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  } else {
    ledStatus[menu] = 1;
    lcd.clear();
    lcd.print("> LED 1 ON...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  }
}
void action2() {
  if (ledStatus[menu] != 0) {
    ledStatus[menu] = 0;
    lcd.clear();
    lcd.print("> LED 2 OFF...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  } else {
    ledStatus[menu] = 1;
    lcd.clear();
    lcd.print("> LED 2 ON...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  }
}
void action3() {
  if (ledStatus[menu] != 0) {
    ledStatus[menu] = 0;
    lcd.clear();
    lcd.print("> LED 3 OFF...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  } else {
    ledStatus[menu] = 1;
    lcd.clear();
    lcd.print("> LED 3 ON...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  }
}
void action4() {
  if (ledStatus[menu] != 0) {
    ledStatus[menu] = 0;
    lcd.clear();
    lcd.print("> LED 4 OFF...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  } else {
    ledStatus[menu] = 1;
    lcd.clear();
    lcd.print("> LED 4 ON...");
    digitalWrite(leds[menu - 1], ledStatus[menu]);
    delay(timer);
  }
}

8. Connect the Arduino Uno to pc via USB cable.
9. Goto tool --> Board --> Arduino AVR board -- Select your Arduino board.
10. Goto tool --> port --> select your port
11. Now everything is completed, so upload the code
12. Goto serial monitor and check the values.

Watch This Tutorial: 


Feel free to comment down if you have any problems and also tell me what next tutorial you would like to see?

Post a Comment

0Comments

Post a Comment (0)