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

ESP32-Based LED Blinker with Potentiometer Control and Pushbutton Start/Stop

View and Edit Project In Cirkit DesignerImage of ESP32-Based LED Blinker with Potentiometer Control and Pushbutton Start/Stop

Circuit Documentation

Summary

This circuit involves an ESP32 microcontroller, a potentiometer, an LED, a resistor, a pushbutton, and a 5V battery. The ESP32 reads the value from the potentiometer and uses it to control the blinking interval of the LED. The pushbutton is used to start and stop the LED blinking. The circuit is powered by a 5V battery.

Component List

  1. 5V Battery

    • Description: Provides power to the circuit.
    • Pins: Positive, Negative
  2. ESP32 38 PINS

    • Description: Microcontroller used to read sensor values and control the LED.
    • Pins: GND, 23, 22/SCL, 1, 2, 21/SDA, 19, 18, 5, 17/TX2, 16/RX2, 4, 0, 15, D1, D0, CLK, 3V3, EN, VP, VN, 34, 35, 32, 33, 25, 26, 27, 14, 12, 13, D2, D3, 11, 5V/VIN
  3. PR-Rotary Potentiometer

    • Description: Used to vary resistance and provide an analog input to the ESP32.
    • Pins: GND, OUT, VCC
  4. LED: Two Pin (red)

    • Description: Visual indicator controlled by the ESP32.
    • Pins: Cathode, Anode
  5. Resistor

    • Description: Limits current to the LED.
    • Pins: Pin1, Pin2
    • Properties:
      • Resistance: 200 Ohms
  6. Pushbutton

    • Description: Used to start and stop the LED blinking.
    • Pins: Pin 3 (out), Pin 4 (out), Pin 1 (in), Pin 2 (in)

Wiring Details

5V Battery

ESP32 38 PINS

PR-Rotary Potentiometer

LED: Two Pin (red)

Resistor

Pushbutton

Code Documentation

/*
 * This Arduino Sketch reads the value from a potentiometer connected to pin 25
 * of an ESP32 microcontroller and uses it to control the blinking interval of
 * an LED connected to pin 23. The potentiometer value ranges from 0 to 4095,
 * which is mapped to a delay interval between 100ms and 2000ms for the LED.
 * The program starts and stops with a button push connected to pin 18. If the
 * program is off, the LED is not blinking.
 */

const int potPin = 25; // Pin connected to the potentiometer output
const int ledPin = 23; // Pin connected to the LED
const int buttonPin = 18; // Pin connected to the button

bool isRunning = false; // State of the program

void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
  pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as an input with pullup
  Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}

void loop() {
  if (digitalRead(buttonPin) == LOW) { // Check if the button is pressed
    delay(20); // Debounce delay
    if (digitalRead(buttonPin) == LOW) { // Confirm the button press
      isRunning = !isRunning; // Toggle the running state
      while (digitalRead(buttonPin) == LOW); // Wait for button release
      delay(20); // Debounce delay
    }
  }

  if (isRunning) {
    pinMode(ledPin, OUTPUT);
    int potValue = analogRead(potPin); // Read the potentiometer value (0-4095)
    int interval = map(potValue, 0, 4095, 50, 500); // Map to delay range
    digitalWrite(ledPin, HIGH); // Turn the LED on
    delay(interval); // Wait for the mapped delay interval
    digitalWrite(ledPin, LOW); // Turn the LED off
    delay(interval); // Wait for the mapped delay interval
    Serial.print("Potentiometer Value: ");
    Serial.print(potValue);
    Serial.print(" | Delay Interval: ");
    Serial.println(interval);
  } else {
    int potValue = analogRead(potPin); // Read the potentiometer value (0-4095)
    int voltage = map(potValue, 0, 4095, 255, 0); // Map to PWM
    float volt = voltage*3.3/255.0; // Map to screen
    analogWrite(ledPin,voltage);
    Serial.print("Potentiometer Value: ");
    Serial.print(potValue);
    Serial.print(" | Napeti: ");
    Serial.println(volt);
    delay(100);
  }
}

This code reads the potentiometer value and uses it to control the blinking interval of the LED. The pushbutton is used to start and stop the LED blinking. The potentiometer value is also printed to the serial monitor along with the delay interval.