Potentiometer-Controlled LED Brightness Using MicroPython for ESP32 and ESP8266

In this project, you will learn how to use a Potentiometer-Controlled LED Brightness Using MicroPython for ESP32 and ESP8266. The potentiometer provides an analog input, which will be read by the ADC (Analog-to-Digital Converter). The analog value is then used to control the brightness of the LED through Pulse Width Modulation (PWM). This project introduces the concepts of ADC for reading analog values and PWM for controlling LED brightness.

What is ADC and PWM in MicroPython for ESP32 and ESP8266?

  • ADC (Analog-to-Digital Converter): Converts the analog signal from the potentiometer into a digital value that the ESP32/ESP8266 can process.
  • PWM (Pulse Width Modulation): Controls the brightness of an LED by adjusting the duty cycle of the signal, where a higher duty cycle means a brighter LED.

Purpose of the Project:

This project will teach you how to:

  • Use ADC to read the analog values from a potentiometer.
  • Use PWM to control the brightness of an LED based on the potentiometer’s position.
  • Understand the relationship between analog inputs and digital control of hardware components.

MicroPython Syntax for Potentiometer-Controlled LED Brightness:

Operation Syntax Example
Initialize ADC adc = machine.ADC(Pin(pin_number)) adc = machine.ADC(machine.Pin(34))
Read ADC value adc.read() pot_value = adc.read()
Initialize PWM pwm = machine.PWM(Pin(pin_number)) pwm = machine.PWM(machine.Pin(5))
Set PWM frequency pwm.freq(frequency) pwm.freq(1000)
Set PWM duty cycle pwm.duty(duty_cycle) pwm.duty(512) (Duty cycle range 0-1023)

Required Components:

  • ESP32 or ESP8266
  • 1 Potentiometer
  • 1 LED
  • 1 Resistor (220Ω)
  • Breadboard
  • Jumper Wires

Circuit Diagram:

   Potentiometer        LED          ESP32/ESP8266

    ————-      —–          ————–

    Pin 1 —————> 3.3V

    Pin 2 —————> GPIO34 (ADC Pin)

    Pin 3 —————> GND

                        Anode ———> GPIO5 (PWM Pin)

                        Cathode ———> GND

 

Circuit Connection Table:

Component Pin ESP32/ESP8266 Pin Explanation
Potentiometer Pin 1 to 3.3V, Pin 2 to GPIO34, Pin 3 to GND Reads the analog value from the potentiometer
LED Anode Connect to GPIO5 GPIO5 (PWM Pin) Controls the LED brightness using PWM
LED Cathode Connect to GND GND Ground connection for the LED

Warnings:

  • Always use a 220Ω resistor with the LED to avoid damaging it due to excessive current.
  • Ensure that you are using an ADC-capable GPIO pin for reading the potentiometer’s analog value.

Circuit Analysis:

The potentiometer provides an analog signal that is read by the ADC on the ESP32/ESP8266. Based on the value from the potentiometer, the PWM signal’s duty cycle is adjusted, which in turn controls the brightness of the LED. As the potentiometer is rotated, the brightness of the LED will increase or decrease.

Installing MicroPython and Libraries:

Install MicroPython on your ESP32 or ESP8266 using Thonny or esptool:
esptool.py –chip esp32 erase_flash

esptool.py –chip esp32 write_flash -z 0x1000 esp32-20210902-v1.17.bin

  1. No Additional Libraries Required: The built-in machine library is sufficient for this project.

Writing the MicroPython Code for Potentiometer-Controlled LED Brightness:

import machine

import time

 

# Initialize the potentiometer (ADC on GPIO34)

adc = machine.ADC(machine.Pin(34))

 

# Initialize the LED with PWM (on GPIO5)

pwm = machine.PWM(machine.Pin(5))

pwm.freq(1000)  # Set frequency to 1000 Hz

 

while True:

    # Read the value from the potentiometer (0-4095)

    pot_value = adc.read()

    

    # Map the potentiometer value (0-4095) to PWM duty cycle (0-1023)

    duty_cycle = int((pot_value / 4095) * 1023)

    

    # Set the PWM duty cycle for the LED

    pwm.duty(duty_cycle)

    

    # Print the potentiometer value and corresponding duty cycle

    print(“Potentiometer Value:”, pot_value, “Duty Cycle:”, duty_cycle)

    

    # Small delay to prevent flooding the console

    time.sleep(0.1)

 

Running the Code and Checking the Output:

  1. Upload the code to your ESP32 or ESP8266 using Thonny or another MicroPython IDE.
  2. Rotate the potentiometer to adjust the brightness of the LED.
  3. Observe the console output: You will see the potentiometer value (0-4095) and the corresponding PWM duty cycle (0-1023) being printed.

Explanation of the Code:

  • ADC Initialization: The ADC is initialized on GPIO34, which reads the analog value from the potentiometer.
  • PWM Initialization: The PWM is initialized on GPIO5 with a frequency of 1000 Hz to control the brightness of the LED.
  • Mapping Values: The potentiometer value (0-4095) is mapped to the PWM duty cycle (0-1023) to adjust the LED brightness.
  • Loop: The code continuously reads the potentiometer value and adjusts the LED brightness in real-time.

Expanding the Project:

  • Multiple LEDs: Use multiple potentiometers to control multiple LEDs with varying brightness.
  • Add OLED Display: Display the potentiometer value and the LED brightness level on an OLED screen.
  • Integrate with Other Sensors: Combine the potentiometer control with other sensor data (e.g., temperature sensors) for more complex control systems.

Common Problems and Solutions:

  1. Problem: The LED brightness doesn’t change as the potentiometer is rotated.
    • Solution: Ensure that the potentiometer is wired correctly and that the ADC pin is correctly initialized.
  2. Problem: The LED flickers when changing brightness.
    • Solution: Adjust the PWM frequency to a higher value if flickering occurs.
  3. Problem: The potentiometer value range is incorrect.
    • Solution: Check the connections of the potentiometer and ensure the correct pin is used for the ADC.

FAQ:

Q: Can I use a different GPIO pin for the potentiometer or LED?
A: Yes, as long as the GPIO pin for the potentiometer supports ADC and the GPIO pin for the LED supports PWM.

Q: How do I change the brightness range?
A: You can adjust the mapping function that converts the potentiometer value to the PWM duty cycle to change the brightness range.

Q: Can I use a sensor other than a potentiometer to control the LED brightness?
A: Yes, any analog sensor (such as a temperature sensor or light sensor) can be used to control the LED brightness based on the sensor’s readings.

Conclusion:

In this project, you successfully controlled the brightness of an LED using a potentiometer and ADC in MicroPython for ESP32 and ESP8266. By learning how to read analog values from the potentiometer and using PWM to control the LED, you gained practical experience in controlling hardware components. This project is a great foundation for building more advanced systems involving analog inputs and PWM-controlled outputs.