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

ESP32-Controlled LED Dimmer with Blink Mode and Adjustable Brightness

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled LED Dimmer with Blink Mode and Adjustable Brightness

Circuit Documentation

Summary of the Circuit

This circuit is designed to control a series of red LEDs using an ESP32 microcontroller. The LEDs can be turned on to a specific brightness level, which is adjustable via a potentiometer. Additionally, a pushbutton is included to toggle between a blinking mode and a steady-on mode for the LEDs. The brightness level and blinking speed are also controlled by the potentiometer. The ESP32 microcontroller is programmed to read the analog value from the potentiometer and the digital state of the pushbutton to adjust the LED behavior accordingly.

Component List

LEDs

Resistors

Trimmer Potentiometer

Pushbutton

ESP32 - Expansion Board

Wiring Details

LEDs

Resistors

Trimmer Potentiometer

Pushbutton

ESP32 - Expansion Board

Documented Code

#define NUM_LEDS 6 // Define the number of LEDs used
const int ledPins[NUM_LEDS] = {15, 16, 17, 18, 19, 21}; // Pins for LEDs
const int potPin = 34;        // Pin for potentiometer
const int buttonPin = 4;      // Pin for pushbutton
bool blinkMode = false;       // Variable to determine blinking mode
int brightness = 0;           // Variable to store LED brightness level
int delayTime = 100;          // Delay time for blinking speed

void setup() {
  // Set each LED pin as an output
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(ledPins[i], OUTPUT); // Set LED pins as OUTPUT
  }
  pinMode(buttonPin, INPUT_PULLDOWN); // Set button pin as input with pull-down resistor
  Serial.begin(9600); // Start serial communication for debugging
}

void loop() {
  // Read the position of the potentiometer
  int potValue = analogRead(potPin); // Read the analog value from the potentiometer
  brightness = map(potValue, 0, 4095, 0, 255); // Map the potentiometer value from 0-4095 to a range of 0-255

  // Check the status of the pushbutton
  if (digitalRead(buttonPin) == HIGH) {
    blinkMode = true;  // Activate blinking mode if button is pressed
  } else {
    blinkMode = false; // Deactivate blinking mode, return to always-on mode
  }

  if (blinkMode) {
    // Blinking mode alternating forward
    for (int i = 0; i < NUM_LEDS; i++) {
      analogWrite(ledPins[i], brightness); // Turn on LED with appropriate brightness
      delay(delayTime); // Wait according to the delay time
      analogWrite(ledPins[i], 0); // Turn off LED
    }

    // Blinking mode alternating backward
    for (int i = NUM_LEDS - 1; i >= 0; i--) {
      analogWrite(ledPins[i], brightness); // Turn on LED with appropriate brightness
      delay(delayTime); // Wait according to the delay time
      analogWrite(ledPins[i], 0); // Turn off LED
    }
    
  } else {
    // Always-on mode with brightness controlled by potentiometer
    for (int i = 0; i < NUM_LEDS; i++) {
      analogWrite(ledPins[i], brightness); // Set all LEDs to a constant brightness
    }
  }
  // Display blinking mode status and brightness value on the serial monitor
  Serial.print(blinkMode);
  Serial.print(" : ");
  Serial.println(brightness);
  delay(100); // Wait a moment before repeating the loop
}

This code is designed to run on an ESP32 microcontroller and controls the brightness and blinking of the LEDs based on the input from a potentiometer and a pushbutton. The setup() function initializes the LED pins, the button pin, and the serial communication. The loop() function reads the potentiometer value to set the brightness, checks the button state to toggle the blinking mode, and then either blinks the LEDs or sets them to a steady brightness based on the mode. The serial output is used for debugging purposes to monitor the current mode and brightness level.