Creating a Simple Menu System Using Arduino, OLED and Encoder

14 May 2023 Balmiki Mandal 0 µC - µP

How to Create a Simple Menu System with Arduino, OLED and Encoder

Do you want to create a simple menu system for your Arduino-based project? Then this guide is perfect for you! We'll show you how to create a simple three-button menu system using the Arduino, an OLED display and a rotary encoder. This guide is for beginners and those new to programming – we’ll walk you through the process step-by-step.

Step 1: Gather the components

Before you get started with this project, you’ll need to gather the components. We’re going to be using an Arduino board, an OLED display, and a rotary encoder. You can find these components at any electronics store, or online.

Step 2: Connect the components

Now that you have all the components, let’s move on to connecting them together. Connect the OLED display to the Arduino board, then connect the rotary encoder to the Arduino. Make sure the connections are secure before continuing.

Step 3: Program the Arduino

Next, we’ll need to program the Arduino board. We’ll be using the Arduino IDE, so make sure you have it installed on your computer. We’ll also need a few libraries – the Adafruit_SSD1306 library is necessary for communicating with the OLED display, and the RotaryEncoder library is required for the rotary encoder. With the libraries installed, we can now write our code.

Step 4: Create a simple menu

Now that the components are connected and the Arduino is programmed, let’s create the menu. We’ll be using a “switch” statement to define different functions for each button. When one of the buttons is pressed, a corresponding function will be executed. We’ll also add some logic to the code, so that when one of the buttons is pressed multiple times in a row, the command will only be executed once.

Step 5: Test it out!

Our menu system is now set up and ready to go. To test it out, upload the code to the Arduino board and turn it on. You should now have a working menu system! If everything is working as expected, you can start adding your own commands and customizing the menus. Creating a simple menu system with Arduino, OLED and encoder hardware is a great way to add user interaction to your projects. With just a few components and some basic programming knowledge, you can easily create a simple menu system with these components. Try it out and see what you can create!

Here's an example source code for creating a simple menu system with an Arduino, OLED display, and rotary encoder:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Encoder.h>

#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);

Encoder myEnc(2, 3);
int menu_index = 0;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
}

void loop() {
  int val = myEnc.read();
  if (val != 0) {
    menu_index += val;
    if (menu_index < 0) menu_index = 0;
    if (menu_index > 2) menu_index = 2;
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("Menu Item ");
    display.println(menu_index);
    display.display();
    delay(50);
  }
}

In the setup() function, we initialize the OLED display and the rotary encoder. We set the menu_index variable to 0, which will be used to keep track of the current menu item.

In the loop() function, we read the rotary encoder value using the myEnc.read() function. If the value is not zero, we increment or decrement the menu_index variable based on the rotation direction. We also check that the menu_index value is within the range of available menu items (in this case, 0 to 2).

We then clear the OLED display using the display.clearDisplay() function, set the cursor position to (0,0) using the display.setCursor() function, print the current menu item number using the display.print() and display.println() functions, and update the display using the display.display() function. We add a small delay of 50 milliseconds between updates to prevent the display from flickering.

You can modify this basic code to add more menu items, sub-menus, and actions to each menu item by adding conditional statements based on the menu_index value. You can also use switch-case statements instead of if-else statements for better readability and maintainability.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.