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

ESP8266-Based LED Control System with Potentiometer and Toggle Switch

View and Edit Project In Cirkit DesignerImage of ESP8266-Based LED Control System with Potentiometer and Toggle Switch

Circuit Documentation

Summary

This circuit is designed to control a red and green LED using a toggle switch and an LED bar using a potentiometer. The circuit utilizes an ESP8266 microcontroller to manage the LEDs and the LED bar. The red LED is connected to pin D7, the green LED to pin D6, and the potentiometer to pin A0. The LED bar is controlled via pins D4 (DI) and D3 (DCKI).

Component List

  1. ESP8266

    • Description: A Wi-Fi microcontroller used to control the LEDs and the LED bar.
    • Pins: 3V, G, D8, D7, D6, D5, D4, D3, D2, D1, D0, A0, S3, S2, S1, SC, S0, SK, EN, VIN, RST, VU
  2. Toggle Switch SPST

    • Description: A single-pole single-throw switch used to toggle between the red and green LEDs.
    • Pins: L1, COM
  3. Adafruit Slide Trinkey

    • Description: A small board with a potentiometer and touch sensor.
    • Pins: 3.3V, POTENTIOMETER, GND, TOUCH
  4. Resistor (100 Ohms)

    • Description: Resistors used to limit current to the LEDs.
    • Pins: pin1, pin2
  5. Resistor (10k Ohms)

    • Description: A resistor used in the circuit.
    • Pins: pin1, pin2
  6. LED: Two Pin (Red)

    • Description: A red LED.
    • Pins: cathode, anode
  7. LED: Two Pin (Green)

    • Description: A green LED.
    • Pins: cathode, anode
  8. Bar Graph (Wokwi Compatible)

    • Description: An LED bar graph display.
    • Pins: A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, C8, C9, C10, C7, C6, C5, C4, C3, C2, C1
  9. 74HC595

    • Description: A shift register used to control the LED bar.
    • Pins: Q1, Q2, Q3, Q4, Q5, Q6, Q7, GND, VCC, Q0, DS (DATA), OE (Enable), ST_CP (RCLK), SH_CP (SRCLK), MR (SRCLR), Q7' (QH)

Wiring Details

ESP8266

Toggle Switch SPST

Adafruit Slide Trinkey

Resistor (100 Ohms)

Resistor (100 Ohms)

Resistor (10k Ohms)

LED: Two Pin (Red)

LED: Two Pin (Green)

Bar Graph (Wokwi Compatible)

74HC595 (First)

74HC595 (Second)

Code Documentation

/*
 * This Arduino Sketch controls a red and green LED using a toggle switch and
 * an LED bar using a potentiometer. The red LED is connected to pin D7, the
 * green LED to pin D6, and the potentiometer to pin A0. The LED bar is
 * controlled via pins D4 (DI) and D3 (DCKI).
 */

// Pin definitions
const int redLEDPin = D7;
const int greenLEDPin = D6;
const int toggleSwitchPin = D5;
const int potPin = A0;
const int ledBarDIPin = D4;
const int ledBarDCKIPin = D3;

void setup() {
  // Initialize pins
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(toggleSwitchPin, INPUT);
  pinMode(ledBarDIPin, OUTPUT);
  pinMode(ledBarDCKIPin, OUTPUT);
}

void loop() {
  // Read the state of the toggle switch
  int toggleState = digitalRead(toggleSwitchPin);

  // Control the LEDs based on the toggle switch state
  if (toggleState == HIGH) {
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(greenLEDPin, LOW);
  } else {
    digitalWrite(redLEDPin, LOW);
    digitalWrite(greenLEDPin, HIGH);
  }

  // Read the potentiometer value
  int potValue = analogRead(potPin);

  // Map the potentiometer value to a range suitable for the LED bar
  int ledBarValue = map(potValue, 0, 1023, 0, 255);

  // Control the LED bar
  analogWrite(ledBarDIPin, ledBarValue);
  analogWrite(ledBarDCKIPin, ledBarValue);

  // Small delay to stabilize readings
  delay(10);
}

This code initializes the pins for the red and green LEDs, the toggle switch, and the LED bar. In the loop function, it reads the state of the toggle switch to control the red and green LEDs. It also reads the potentiometer value and maps it to control the LED bar.