# 20x4 LCD i2c menu with rotary encoder

**URL:** <https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708>\
**Category:** General Guidance\
**Created:** [February 21, 2024, 3:00pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708 "2024-02-21T15:00:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 3:00pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/1 "2024-02-21T15:00:32Z")

</div>

I need help with the code, I've asked chatgpt, it still doesn't work.  
So I'm making a temp based watering system using max6675 and rotary encoder  
This is the code I'm using rn for encoder and LCD

```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <rotary.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the I2C address to 0x27 and specify the columns and rows

#define PINA A0
#define PINB A1
#define PUSHB A2

Rotary r = Rotary(PINA, PINB, PUSHB);

int cursorLine = 1;
int displayFirstLine = 1;

#define MainMenu 0

const int maxItemSize = 11;

char startMenu[][maxItemSize] = {"Volume", "Reset"};

int Summary[] = {0, 0};

int menuItems;
char *menuSelected = NULL;
bool volumeHandler = false;
int volume = 0;
int menuOption = 0;

int backlightPin = 10;
int brightness = 255;
int fadeAmount = 5;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000;

void setup()
{
  pinMode(PINA, INPUT);
  pinMode(PINB, INPUT);
  pinMode(PUSHB, INPUT);
  digitalWrite(PINA, HIGH);
  digitalWrite(PINB, HIGH);
  digitalWrite(PUSHB, HIGH);

  pinMode(backlightPin, OUTPUT);
  digitalWrite(backlightPin, HIGH);

  Wire.begin();
  lcd.begin(20, 4);
  welcome();
  lcd.clear();

  menuItems = sizeof(startMenu) / sizeof(startMenu[0]);
  menuSelected = &startMenu[0][0];
  menuOption = MainMenu;

  startMillis = millis();
  display_menu();
}

void loop()
{
  volatile unsigned char result = r.process();

  currentMillis = millis();
  lcd.setCursor(18, 0);
  lcd.print((currentMillis - startMillis) / 1000);
  if (currentMillis - startMillis >= period)
  {
    LCDfadeOut();
  }

  if (result)
  {
    init_backlight();
    if (result == DIR_CCW)
    {
      if (volumeHandler == true)
      {
        volume--;
        lcd.setCursor(0, 1);
        lcd.print("Volume: ");
        lcd.print(volume);
      }
    }
    else
    {
      if (volumeHandler == true)
      {
        volume++;
        lcd.setCursor(0, 1);
        lcd.print("Volume: ");
        lcd.print(volume);
      }
    }
  }

  if (r.buttonPressedReleased(25))
  {
    init_backlight();
    if (menuOption == MainMenu)
    {
      if (cursorLine == 1)
      {
        volumeHandler = !volumeHandler;
        lcd.clear();
        if (volumeHandler)
        {
          lcd.setCursor(0, 1);
          lcd.print("Volume: ");
          lcd.print(volume);
        }
        else
        {
          display_menu();
        }
      }
      else if (cursorLine == 2)
      {
        Reset();
        display_menu(); // added to display the menu after Reset
      }
    }
  }
}

void welcome()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Demo Menu ");
  lcd.setCursor(0, 2);
  lcd.print(" Carlos Siles ");
  lcd.setCursor(0, 3);
  lcd.print(" Version 1.0 ");
  delay(2000);
}

void display_menu()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Menu ");

  for (int i = 0; i < menuItems; ++i)
  {
    lcd.setCursor(1, i + 1);
    lcd.print(startMenu[i]);

    if (i + 1 == cursorLine)
    {
      lcd.setCursor(0, i + 1);
      lcd.print("<");
    }
  }
}

void Reset()
{
  volume = 0;
  lcd.setCursor(0, 1);
  lcd.print("Volume: ");
  lcd.print(volume);
  menuOption = MainMenu; // set menuOption to MainMenu after Reset
}

void LCDfadeOut()
{
  while (brightness > 0)
  {
    lcd.setBacklight(brightness);
    brightness -= fadeAmount;
    delay(20);
  }
  lcd.noBacklight();
  lcd.clear();
}

void init_backlight()
{
  lcd.setBacklight(255);
  startMillis = millis();
  brightness = 255;
}

```

My plan is that there would be temp showing at the top after the menu then options named Threshold, duration, and interval, so I tried a sketch by someone named Carlos siles and only retained the volume option. Also other options like reset and summary are options I want to add. If you can, could you also add the rest of the code like the temp and relay mod and small water pump. Another prob I want to say is that the encoder increases the value by random like 1-2 or even more.  
This is due tomorrow and I would be sad if I don't finish, this actually for research subject

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 3:07pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/2 "2024-02-21T15:07:51Z")

</div>

> [@vzlatte](#):
>
> I tried a sketch by someone named Carlos siles

> [@vzlatte](#):
>
> This is due tomorrow and I would be sad if I don't finish, this actually for research subject

> [@vzlatte](#):
>
> I've asked chatgpt, it still doesn't work.

so in a nutshell, this is a school project, you did not write anything yourself, you found a sketch online that does not work, you waited for the last minute and hoped chatGPT would solve it for you but it did not ?

did I get that right?

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 3:14pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/3 "2024-02-21T15:14:25Z")

</div>

Ive actually finished this without the rotary encoder but they said I had to add buttons to change the temp thresholds and stuff, so that they didn't have to change the code. I've been trying for almost a month now and still not working. I have group mates but they don't know about code stuff so I was the one doing it

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 3:22pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/4 "2024-02-21T15:22:55Z")

</div>

I would suggest you throw away that code if you copied it from someone so that you build something that you fully understand and are ready to explain to your teacher.

use the [encoder library](https://www.pjrc.com/teensy/td_libs_Encoder.html) for the rotary  
use [Toggle](https://github.com/Dlloydev/Toggle/blob/main/src/Toggle.h) for the button  
use [hd44780](https://github.com/duinoWitchery/hd44780#) for the LCD screen

here is a basic sketch showing how to put those together and update the screen when the rotary encoder is used or the button pressed

> **[Encoder, Button and LCD - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/390353236396127233)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

you might also benefit from studying state machines. Here is a small introduction to the topic: [Yet another Finite State Machine introduction](https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997)

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 3:25pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/5 "2024-02-21T15:25:16Z")

</div>

I'll try this out. Thanks

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 3:55pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/6 "2024-02-21T15:55:05Z")

</div>

ok so I tried the code and whenever i try to move or press the encoder, it doesn't do anything, connections are accurate.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 3:58pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/7 "2024-02-21T15:58:38Z")

</div>

show a picture (in focus, clear) of your project  
did you use the exact same code I gave you and the same wiring?

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 4:01pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/8 "2024-02-21T16:01:40Z")

</div>

![IMG20240221190041](https://europe1.discourse-cdn.com/arduino/original/4X/d/6/3/d63f0526cf499c83793b3b4368722647f1d59dcb.jpeg)  
Is this good enough?

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 4:10pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/9 "2024-02-21T16:10:03Z")

</div>

is there any issue with it?

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 4:12pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/10 "2024-02-21T16:12:28Z")

</div>

ok so kept unplugging and plugging it back in and it finally worked. But I wanted to ask if you could help me with my project

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 4:12pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/11 "2024-02-21T16:12:48Z")

</div>

so the LCD does turn on correctly - that's already a good sign

can you make sure the power reaches your rotary encoder?

try to move the rotary power pins next to the others  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/7/3/573721140b8e222f6caf89c462e6177758addd04.jpeg)

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 4:13pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/12 "2024-02-21T16:13:48Z")

</div>

yes it works now

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 4:14pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/13 "2024-02-21T16:14:00Z")

</div>

good 🙂

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 4:22pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/14 "2024-02-21T16:22:14Z")

</div>

Could you maybe help with setting up a menu in the lcd?

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 4:48pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/15 "2024-02-21T16:48:40Z")

</div>

```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <rotary.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the I2C address to 0x27 and specify the columns and rows

#define PINA 3
#define PINB 2
#define PUSHB 4

Rotary r = Rotary(PINA, PINB, PUSHB);

int cursorLine = 1;
int displayFirstLine = 1;

#define MainMenu 0

const int maxItemSize = 11;

char startMenu[][maxItemSize] = {"Volume", "Reset"};

int Summary[] = {0, 0};

int menuItems;
char *menuSelected = NULL;
bool volumeHandler = false;
int volume = 0;
int menuOption = 0;

int backlightPin = 10;
int brightness = 255;
int fadeAmount = 5;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000;

void setup()
{
  pinMode(PINA, INPUT);
  pinMode(PINB, INPUT);
  pinMode(PUSHB, INPUT);
  digitalWrite(PINA, HIGH);
  digitalWrite(PINB, HIGH);
  digitalWrite(PUSHB, HIGH);

  pinMode(backlightPin, OUTPUT);
  digitalWrite(backlightPin, HIGH);

  Wire.begin();
  lcd.begin(20, 4);
  welcome();
  lcd.clear();

  menuItems = sizeof(startMenu) / sizeof(startMenu[0]);
  menuSelected = &startMenu[0][0];
  menuOption = MainMenu;

  startMillis = millis();
  display_menu();
}

void loop()
{
  volatile unsigned char result = r.process();

  currentMillis = millis();
  lcd.setCursor(18, 0);
  lcd.print((currentMillis - startMillis) / 1000);
  if (currentMillis - startMillis >= period)
  {
    LCDfadeOut();
  }

  if (result)
  {
    init_backlight();
    if (result == DIR_CCW)
    {
      if (volumeHandler == true)
      {
        volume--;
        lcd.setCursor(0, 1);
        lcd.print("Volume: ");
        lcd.print(volume);
      }
      else // Update cursorLine for menu navigation
      {
        cursorLine--;
        if (cursorLine < 1) {
          cursorLine = menuItems;
        }
        display_menu();
      }
    }
    else
    {
      if (volumeHandler == true)
      {
        volume++;
        lcd.setCursor(0, 1);
        lcd.print("Volume: ");
        lcd.print(volume);
      }
      else // Update cursorLine for menu navigation
      {
        cursorLine++;
        if (cursorLine > menuItems) {
          cursorLine = 1;
        }
        display_menu();
      }
    }
  }

  if (r.buttonPressedReleased(25))
  {
    init_backlight();
    if (menuOption == MainMenu)
    {
      if (cursorLine == 1)
      {
        volumeHandler = !volumeHandler;
        lcd.clear();
        if (volumeHandler)
        {
          lcd.setCursor(0, 1);
          lcd.print("Volume: ");
          lcd.print(volume);
        }
        else
        {
          display_menu();
        }
      }
      else if (cursorLine == 2)
      {
        Reset();
        display_menu(); // added to display the menu after Reset
      }
    }
  }
}

void welcome()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Demo Menu ");
  lcd.setCursor(0, 2);
  lcd.print(" Carlos Siles ");
  lcd.setCursor(0, 3);
  lcd.print(" Version 1.0 ");
  delay(2000);
}

void display_menu()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Menu ");

  for (int i = 0; i < menuItems; ++i)
  {
    lcd.setCursor(1, i + 1);
    lcd.print(startMenu[i]);

    if (i + 1 == cursorLine)
    {
      lcd.setCursor(0, i + 1);
      lcd.print("<");
    }
  }
}

void Reset()
{
  volume = 0;
  lcd.setCursor(0, 1);
  lcd.print("Volume: ");
  lcd.print(volume);
  menuOption = MainMenu; // set menuOption to MainMenu after Reset
}

void LCDfadeOut()
{
  while (brightness > 0)
  {
    lcd.setBacklight(brightness);
    brightness -= fadeAmount;
    delay(20);
  }
  lcd.noBacklight();
  lcd.clear();
}

void init_backlight()
{
  lcd.setBacklight(255);
  startMillis = millis();
  brightness = 255;
}

```

I tried this and it worked, now I'm thinking on how to add 2 more options that function like Volume, but don't know how to do it

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 5:20pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/16 "2024-02-21T17:20:01Z")

</div>

you might benefit from studying state machines. Here is a small introduction to the topic: [Yet another Finite State Machine introduction](https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997)

---

<div class="post-metadata">

**Author:** ![vzlatte](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vzlatte](https://forum.arduino.cc/u/vzlatte)\
**Post date:** [February 21, 2024, 5:36pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/17 "2024-02-21T17:36:07Z")

</div>

ok, um what is a good max6675 library? and is this code needed or can it be replaced?  
`thermocouple.begin();`  
because the library made by adafruit doesn't have it

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 5:55pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/18 "2024-02-21T17:55:18Z")

</div>

I found another example that I had shared on wokwi with a menu

> **[menuSimple - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/389086128960482305)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

part of the code is in French but I'm sure your chatGPT friend can translate the variable names and comments in English

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 21, 2024, 5:56pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/19 "2024-02-21T17:56:40Z")

</div>

> [@vzlatte](#):
>
> t is a good max6675 library? and is this code needed or can it be replaced?  
> `thermocouple.begin();`  
> because the library made by adafruit doesn't have it

look at their examples

> <https://github.com/adafruit/MAX6675-library/blob/master/examples/lcdthermocouple/lcdthermocouple.ino>

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [February 21, 2024, 5:58pm UTC](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708/20 "2024-02-21T17:58:24Z")

</div>

> [@vzlatte](#):
>
> I've asked chatgpt, it still doesn't work.

Ask ChatGPT for the schematic it used. How long did you wait until starting the project? I believe the answers to most of your questions were in the class lecture but you missed them. Hint: You can spend an hour on ChatGPT and get your code. You then spend about 6 to 8 days debugging it if you are lucky. If you start from scratch and write it yourself you have it running in less then 3 days and know why it works.

[Next page](https://forum.arduino.cc/t/20x4-lcd-i2c-menu-with-rotary-encoder/1226708.md?page=2)
