Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Volume Control with Rotary Encoder and I2C LCD

View and Edit Project In Cirkit DesignerImage of Arduino Volume Control with Rotary Encoder and I2C LCD

Circuit Documentation

Summary

This circuit is designed for volume control using a rotary encoder and a 16x2 I2C LCD. The rotary encoder allows the user to adjust the volume level between 0 and 100, starting at 50. The current volume level and the state of the button on the rotary encoder are displayed on the LCD. The circuit utilizes an Arduino UNO as the microcontroller to read inputs from the rotary encoder and control the LCD display.


Component List

Arduino UNO

16x2 I2C LCD

Rotary Encoder with Knob - Simulate


Wiring Details

Arduino UNO


Documented Code

/*
 * Arduino Sketch for Volume Control using a Rotary Encoder and 16x2 I2C LCD
 * 
 * This code reads input from a rotary encoder to adjust a volume level between
 * 0 and 100, starting at 50. The current volume and button state are displayed
 * on a 16x2 I2C LCD. The rotary encoder's CLK and DT pins are connected to
 * digital pins D2 and D3, respectively, and the SW pin is connected to D4.
 * The LCD is connected via I2C to pins A4 (SDA) and A5 (SCL).
 * 
 * Clockwise rotation of the encoder increases the volume, while counter-
 * clockwise rotation decreases it. The button press is also detected and
 * displayed on the LCD.
 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Rotary Encoder Pins
const int clkPin = 2;
const int dtPin = 3;
const int swPin = 4;

// LCD Address and Dimensions
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Volume Control Variables
int volume = 50;
int lastClkState;
bool buttonPressed = false;

void setup() {
  // Initialize Serial Monitor
  Serial.begin(9600);

  // Initialize LCD
  lcd.begin(16,2);
  lcd.backlight(); 
  
  // Initialize Rotary Encoder Pins
  pinMode(clkPin, INPUT);
  pinMode(dtPin, INPUT);
  pinMode(swPin, INPUT_PULLUP);

  // Read initial state of CLK
  lastClkState = digitalRead(clkPin);

  // Display initial volume
  updateDisplay();
}

void loop() {
  // Read current state of CLK
  int currentClkState = digitalRead(clkPin);

  // Check for rotation
  if (currentClkState != lastClkState) {
    if (digitalRead(dtPin) == currentClkState) {
      // Clockwise rotation
      if (volume < 100) volume++;
    } else {
      // Counter-clockwise rotation
      if (volume > 0) volume--;
    }
    updateDisplay();
  }
  lastClkState = currentClkState;

  // Check button state
  if (digitalRead(swPin) == LOW) {
    if (!buttonPressed) {
      buttonPressed = true;
      updateDisplay();
    }
  } else {
    if (buttonPressed) {
      buttonPressed = false;
      updateDisplay();
    }
  }
}

void updateDisplay() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Volume: ");
  lcd.print(volume);
  lcd.setCursor(0, 1);
  lcd.print("Button: ");
  lcd.print(buttonPressed ? "Pressed" : "Released");
}

This documentation provides a comprehensive overview of the circuit, detailing the components used, their connections, and the code that drives the functionality of the system.