Interfacing Push Button With Oled

Written By - Nidhi Rajput
Interfacing an OLED (Organic Light Emitting Diode) display with a push button and an Arduino Uno opens up a world of possibilities for creating interactive projects. By combining the visual feedback of the OLED display with the tactile input of the push button, you can create user interfaces, interactive games, data loggers, and much more. In this project, we'll explore how to wire an OLED display and a push button to an Arduino Uno and write code to control them.
The OLED display will serve as the output device, providing visual feedback to the user, while the push button will act as an input, allowing the user to interact with the system. Throughout this project, we'll cover the theory behind interfacing these components, the hardware connections required, and the programming concepts needed to control them. By the end, you'll have a solid understanding of how to integrate OLED displays and push buttons into your Arduino projects, opening up a world of creative possibilities.
Overview of Push Button Oled and Arduino
When interfacing an OLED display with a push button and an Arduino Uno, you'll primarily be working with the Arduino programming language, which is a variant of C/C++. Here's an overview of the process. Arduino IDE (Integrated Development Environment) provides a simplified interface for writing, compiling, and uploading code to Arduino boards. Arduino code is written in a subset of C/C++, making it accessible to beginners while retaining the flexibility and power of the language.In this project, we'll explore how to wire an OLED display and a push button to an Arduino Uno and write code to control them. The OLED display will serve as the output device, providing visual feedback to the user, while the push button will act as an input, allowing the user to interact with the system. Overall, Arduino programming language, based on C/C++, is used to interface an OLED display with a push button and an Arduino Uno, allowing you to create interactive projects with ease.

Oled Display

Circuit Diagram

Steps
Code
1
2#include <Wire.h>
3#include <Adafruit_GFX.h>
4#include <Adafruit_SSD1306.h>
5
6#define BUTTON_PIN 2 // Push button pin
7#define SCREEN_WIDTH 128
8#define SCREEN_HEIGHT 64
9#define OLED_RESET -1
10
11Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
12
13void setup() {
14 // Initialize the button pin
15 pinMode(BUTTON_PIN, INPUT_PULLUP); // Using internal pull-up resistor
16
17 // Initialize the OLED display
18 if (!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
19 Serial.println(F("SSD1306 allocation failed"));
20 for (;;);
21 }
22
23 display.display(); // Initialize the display
24 delay(2000); // Pause for 2 seconds
25}
26
27void loop() {
28 int buttonState = digitalRead(BUTTON_PIN); // Read button state
29
30 // Clear the display before updating
31 display.clearDisplay();
32
33 if (buttonState == LOW) {
34 // Button pressed
35 display.setTextSize(2);
36 display.setTextColor(SSD1306_WHITE);
37 display.setCursor(10, 30);
38 display.print("Button Pressed");
39 } else {
40 // Button not pressed
41 display.setTextSize(1);
42 display.setTextColor(SSD1306_WHITE);
43 display.setCursor(10, 30);
44 display.print("Press the Button");
45 }
46
47 display.display(); // Update the OLED display
48 delay(100); // Short delay to debounce the button
49}
50
51