Overview
In this project, we will build a Desktop Dimming Light using Raspberry Pi Pico with MicroPython Code. We will use LED and a potentiometer for this application.
Whenever we rotate the potentiometer, the brightness of the lamp is controlled. So we achieve the effect of dimming the desk lamp.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
1. Raspberry Pi Pico Board – 1
2. Potentiometer Module – 1
3. LED – 1
4. Breadboard – 1
5. Jumper Wires – 4
6. Micro-USB Cable – 1
Potentiometer
A potentiometer is a resistive element with three terminals whose resistance can be adjusted according to a certain changing law. A potentiometer usually consists of a resistive body and a movable brush. When the brush moves along the resistor body, the resistance value or voltage that has a certain relationship with the displacement is obtained at the output end.
Potentiometers can be used as both three-terminal components and two-terminal components. The latter can be regarded as a variable resistor because its function in the circuit is to obtain an output voltage that has a certain relationship with the input voltage (applied voltage), so it is called a potentiometer.
Circuit Diagram & Connection
The connection diagram for Desktop Dimming Light using Raspberry Pi Pico & potentiometer is given below.
Connect the VCC, GND & Analog Output Pin of the Potentiometer Module to the VSYS, GND & GP26 Pin of the Raspberry Pi Pico Board. Also connect the LED positive terminal to GP15 & negative to GND.
MicroPython Code/Program
Open your Thonny IDE & Copy the following code & paste it to the editor window.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from machine import Pin,ADC,PWM from time import sleep Led_pin = 15 Potentiometer_pin = 0 # ADC0 multiplexing pin is GP26 def setup(): global LED global Pot_ADC LED = PWM(Pin(Led_pin)) LED.freq(2000) #Set the LED operating frequency to 2KHz Pot_ADC = ADC(Potentiometer_pin) def loop(): while True: print ('Potentiometer Value:', Pot_ADC.read_u16()) Value = Pot_ADC.read_u16() LED.duty_u16(Value) sleep(0.2) if __name__ == '__main__': setup() loop() |
Now you can run this code to test the working of the circuit. Rotate the potentiometer to control the brightness of the lamp to achieve the effect of dimming the desk lamp.









