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

ESP32-Controlled LED with Pushbutton Interface

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled LED with Pushbutton Interface

Circuit Documentation

Summary

This circuit is designed around the ESP32 microcontroller, which is a versatile and powerful microcontroller with a wide range of I/O capabilities. The circuit includes a pushbutton that allows user interaction, a resistor to limit current through the pushbutton, an LED to provide visual feedback, and another resistor to limit current through the LED. The ESP32 reads the state of the pushbutton and controls the LED accordingly. The circuit is powered by the 3.3V supply from the ESP32.

Component List

ESP32 (30 pin)

Pushbutton

Resistor (10k Ohms)

LED: Two Pin (red)

Resistor (220 Ohms)

Wiring Details

ESP32 (30 pin)

Pushbutton

Resistor (10k Ohms)

LED: Two Pin (red)

Resistor (220 Ohms)

Documented Code

// Code for ESP32 microcontroller
int onOff = LOW;
long lastPrint = 0;

void setup() {
  // Initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  
  // Set pin D4 as an OUTPUT for the LED control:
  pinMode(4, OUTPUT);
  
  // Set pin D5 as an INPUT for reading the pushbutton state:
  pinMode(5, INPUT);
}

void loop() {
  // Read the state of the pushbutton:
  int btn = digitalRead(5);
  
  // If the button state has changed, update the LED state and store the new state:
  if(btn != onOff) {
    digitalWrite(4, btn);
    onOff = btn;
  }
  
  // Every 1000 milliseconds, print the state of the button:
  long currentMillis = millis();
  if(currentMillis - lastPrint > 1000) {
    Serial.println(onOff ? "on" : "off");
    lastPrint = currentMillis;
  }
}

This code is designed to run on the ESP32 microcontroller. It initializes the serial communication for debugging purposes and sets up the necessary pins for the LED and pushbutton. The loop function continuously checks the state of the pushbutton and toggles the LED accordingly. It also prints the state of the LED to the serial monitor every second.