ESP32 OLED dan LED Controller — ESP32 simulation

Proyek ESP32 dengan OLED SSD1306, LED, dan Pushbutton

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by smekantri.

Components

Sketch code

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

#define SDA_PIN 21
#define SCL_PIN 22

#define BUTTON_PIN 33
#define LED_PIN 25

Adafruit_SH1106G oled(128, 64, &Wire, -1);

bool lampState = false;
bool lastButtonState = HIGH;

void setup() {
  // I2C OLED
  Wire.begin(SDA_PIN, SCL_PIN);

  // OLED
  oled.begin(0x3C, true);
  oled.clearDisplay();
  oled.setTextColor(SH110X_WHITE);
  oled.setTextSize(2);

  // Push Button dan LED
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);

  digitalWrite(LED_PIN, LOW);

  // Tampilan awal
  oled.setCursor(0, 0);
  oled.println("LAMPU OFF");
  oled.display();
}

void loop() {
  
  bool buttonState = digitalRead(BUTTON_PIN);

  // Jika tombol baru saja ditekan
  if (lastButtonState == HIGH && buttonState == LOW) {

    lampState = !lampState;

    if (lampState == true) {
      // LED ON
      digitalWrite(LED_PIN, HIGH);

      oled.clearDisplay();
      oled.setCursor(0, 0);
      oled.println("LAMPU");
      oled.setCursor(0, 25);
      oled.println("ON");
      oled.display();

    } else {
      // LED OFF
      digitalWrite(LED_PIN, LOW);

      oled.clearDisplay();
      oled.setCursor(0, 0);
      oled.println("LAMPU");
      oled.setCursor(0, 25);
      oled.println("OFF");
      oled.display();
    }

    delay(200); // anti bouncing
  }

  lastButtonState = buttonState;
}

More projects by smekantri