5.8 Turn the Knob¶
A potentiometer is a three-terminal device that is commonly used to adjust the resistance in a circuit. It features a knob or a sliding lever that can be used to vary the resistance value of the potentiometer. In this project, we will utilize it to control the brightness of an LED, similar to a desk lamp in our daily life. By adjusting the position of the potentiometer, we can change the resistance in the circuit, thereby regulating the current flowing through the LED and adjusting its brightness accordingly. This allows us to create a customizable and adjustable lighting experience, similar to that of a desk lamp.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
ESP32 Starter Kit |
320+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Available Pins
Available Pins
Here is a list of available pins on the ESP32 board for this project.
Available Pins
IO14, IO25, I35, I34, I39, I36
Strapping Pins
The following pins are strapping pins, which affect the startup process of the ESP32 during power on or reset. However, once the ESP32 is booted up successfully, they can be used as regular pins.
Strapping Pins
IO0, IO12
Schematic
When you rotate the potentiometer, the value of I35 will change. By programming, you can use the value of I35 to control the brightness of the LED. Therefore, as you rotate the potentiometer, the brightness of the LED will also change accordingly.
Wiring
Code
Note
Open the
5.8_turn_the_knob.py
file located in theesp32-starter-kit-main\micropython\codes
path, or copy and paste the code into Thonny. Then, click “Run Current Script” or press F5 to execute it.Make sure to select the “MicroPython (ESP32).COMxx” interpreter in the bottom right corner.
from machine import ADC, Pin, PWM
import time
pot = ADC(Pin(35, Pin.IN)) # create an ADC object acting on a pin
# Configure the ADC attenuation to 11dB for full range
pot.atten(pot.ATTN_11DB)
# Create a PWM object
led = PWM(Pin(26), freq=1000)
while True:
# Read a raw analog value in the range of 0-4095
value = pot.read()
# Scale the value to the range of 0-1023 for ESP32 PWM duty cycle
pwm_value = int(value * 1023 / 4095)
# Update the LED brightness based on the potentiometer value
led.duty(pwm_value)
# Read the voltage in microvolts and convert it to volts
voltage = pot.read_uv() / 1000000
# Print the raw value and the voltage
print(f"value: {value}, Voltage: {voltage}V")
# Wait for 0.5 seconds before taking the next reading
time.sleep(0.5)
With this script run, the LED brightness changes as the potentiometer is rotated, while the analog value and voltage at this point are displayed in the Shell.