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

Arduino Leonardo with OLED Display and Pushbutton Interface

View and Edit Project In Cirkit DesignerImage of Arduino Leonardo with OLED Display and Pushbutton Interface

Circuit Documentation

Summary of the Circuit

This circuit consists of an Arduino Leonardo microcontroller, a pushbutton, a resistor, and a 128x64 OLED display. The Arduino Leonardo is the central processing unit of the circuit, interfacing with the OLED display via I2C communication and monitoring the state of the pushbutton through one of its digital pins. The resistor is used to provide a pull-down for the pushbutton, ensuring a stable LOW signal when the button is not pressed. The OLED display is used to show various information and changes its content based on the pushbutton's state.

Component List

Pushbutton

Resistor

Arduino Leonardo (Rev3b)

128x64 OLED Display (I2C IIC SPI Serial)

Wiring Details

Pushbutton

Resistor

Arduino Leonardo (Rev3b)

128x64 OLED Display (I2C IIC SPI Serial)

Documented Code

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SH1106_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/SCL, /* data=*/SDA, /* reset=*/U8X8_PIN_NONE); 

int button = 10; // Button connected to D10
int menu = 1; // Variable to track the menu state

void setup() {
  u8g2.begin(); // Initialize the OLED display
  pinMode(button, INPUT); // Set the button pin as input
  Serial.begin(9600); // Start serial communication
  drawOverview(u8g2); // Draw the initial screen
}

void loop() {
  int pinState = digitalRead(button); // Read the state of the button

  if (pinState == 1) { // If the button is pressed
    menu++; // Increment the menu state
    if (menu > 2) { // If the menu state exceeds the number of menus, reset to 1
      menu = 1;
    }

    // Update the display based on the current menu state
    if (menu == 1) {
      drawOverview(u8g2);
    }
    if (menu == 2) {
      drawPlayerStats(u8g2);
    }
    delay(500); // Debounce delay
  }
}

// Function to draw the overview screen on the OLED display
void drawOverview(U8G2 & u8g2) {
  u8g2.clearBuffer(); // Clear the display buffer
  u8g2.setFont(u8g2_font_ncenB08_tr); // Set font
  u8g2.drawStr(10, 20, "current speed:"); // Draw text
  u8g2.drawStr(10, 35, "12 km/h"); // Draw text

  u8g2.setFont(u8g2_font_baby_tr); // Set font
  u8g2.drawStr(10, 55, "EXP:"); // Draw text
  u8g2.drawFrame(10, 60, 100, 4); // Draw a frame (for a progress bar)
  u8g2.drawBox(10, 60, 30, 4); // Draw a filled box (to represent progress)

  u8g2.sendBuffer(); // Send the buffer to the display
}

// Function to draw the player stats screen on the OLED display
void drawPlayerStats(U8G2 & u8g2) {
  u8g2.clearBuffer(); // Clear the display buffer
  u8g2.setFont(u8g2_font_baby_tr); // Set font
  u8g2.drawStr(10, 20, "Logged in as:"); // Draw text
  u8g2.drawStr(70, 20, "DasSaffe"); // Draw text
  u8g2.drawStr(10, 30, "Profile:"); // Draw text
  u8g2.drawStr(70, 30, "Cycling"); // Draw text
  u8g2.drawStr(10, 40, "Connection:"); // Draw text
  u8g2.drawStr(70, 40, "connected"); // Draw text
  u8g2.sendBuffer(); // Send the buffer to the display
}

This code is designed to run on the Arduino Leonardo microcontroller. It initializes an OLED display and reads the state of a pushbutton to toggle between different display screens. The display shows a speedometer and experience bar on one screen and user stats on another. The button's state is read, debounced, and used to switch between these screens.