Interactive LED Control with Arduino Uno R4 Wi-Fi and OLED Display

This project demonstrates an interactive LED control experiment using an Arduino Uno R4 Wi-Fi board, two buttons, and a 0.96 inch OLED display. It's an excellent project for beginners to understand basic programming, circuit setup, and user interaction with Arduino.

Project Introduction

This project introduces an interactive way to control an LED using an Arduino board, two buttons, and an OLED display. The user can turn the LED on and off using the buttons, and the current state of the LED is displayed on the OLED screen.

Project Principle

The principle behind this project is to use the Arduino's digital input pins to read the state of two buttons and a digital output pin to control an LED. The OLED display is used to show the current state of the LED. The Arduino reads the button states and updates the LED and the display accordingly.

Project Setup Method

Materials Needed

  • 1 x Arduino board (e.g., Arduino Uno R4 Wi-Fi)
  • 1 x LED
  • 1 x 220-ohm resistor
  • 1 x Breadboard
  • Jumper wires
  • 2 x Push buttons
  • 1 x 0.96 inch OLED display (I2C interface)
  • 1 x I2C pull-up resistor (4.7k-ohm)

Circuit Setup

  1. Insert the long leg (anode) of the LED into a breadboard row, and place the 220-ohm resistor next to it.
  2. Connect the other end of the resistor to the breadboard row, ensuring they share the same electrical connection.
  3. Insert the short leg (cathode) of the LED into another breadboard row.
  4. Connect one end of a jumper wire to the cathode's breadboard row.
  5. Connect the other end of the jumper wire to one of the Arduino's digital output pins (e.g., pin 13).
  6. Connect the anode's breadboard row to the Arduino's ground (GND) through another jumper wire.
  7. Connect the push buttons to the Arduino's digital input pins (e.g., pins 2 and 3) using pull-down resistors.
  8. Connect the OLED display to the Arduino's I2C pins (SDA and SCL) and the power supply (5V and GND).
  9. Add a pull-up resistor to the SDA line of the OLED display.

Code

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define LED_PIN 13
#define BUTTON_PIN1 2
#define BUTTON_PIN2 3

int ledState = LOW;
int buttonState1 = 0;
int buttonState2 = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN1, INPUT);
  pinMode(BUTTON_PIN2, INPUT);
  digitalWrite(BUTTON_PIN1, HIGH); // Enable pull-up resistor
  digitalWrite(BUTTON_PIN2, HIGH); // Enable pull-up resistor

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for(;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("LED Control");
  display.display();
}

void loop() {
  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

  if (buttonState1 == LOW) {
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
    display.clearDisplay();
    display.setCursor(0,0);
    if (ledState == HIGH) {
      display.println("LED ON");
    } else {
      display.println("LED OFF");
    }
    display.display();
  }

  if (buttonState2 == LOW) {
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
    display.clearDisplay();
    display.setCursor(0,0);
    if (ledState == HIGH) {
      display.println("LED ON");
    } else {
      display.println("LED OFF");
    }
    display.display();
  }

  delay(200); // Debounce delay
}

Demo Video

TBD.