X
Home > Blog > STEM for Arduino > HOW to Achieve Light Adjustment with Kidspico

HOW to Achieve Light Adjustment with Kidspico

By r September 29th, 2024 49 views
In this experiment, we will introduce how to adjust the brightness of LED module via a potentiometer.

Flow

4301

Assembly

line1

Required:

43_00

line1

Step 1

43_01

line1

Step 2

43_02

line1

Step 3

43_03

line1

Step 4

43_04

line1

Step 5

43_05

line1

Step 6

43_06

line1

Step 7

43_07

line1

Completed!

43_08

line1

Wiring Diagram

4302

Test Code

Open 4.3Brightness_adjustment.py and click1407.

'''
 * Filename    : Brightness_adjustment
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin,ADC,PWM
import time

RP = ADC(26)
led = PWM(Pin(11))
led.freq(1000)

while True:
    RP_Value = RP.read_u16()
    print('ADC Value:',RP_Value)
    led.duty_u16(RP_Value)
    time.sleep(0.1)

Explanation

5top

  1. from machine import Pin,ADC,PWM

    import PIN, ADC, PWM module.

  2. led = PWM(Pin(11))

    a PWM function whose PWM output pin is io11.

  3. led.freq(1000)

Set PWM output frequency to 1000Hz.

  1. led.duty_u16(RP_Value)

    Set duty cycle, set the value output by the potentiometer to the duty cycle of the white LED module.

Conceive:

  1. Initialization: set the pins of potentiometer and white LED module, set the output frequency of the LED module.

  2. Loop:

    ① Read the ADC value of potentiometer, and print it on Shell.

    ② Set the value as the duty cycle of white LED module, so that the potentiometer controls the brightness of LED.

5bottom

Test Result
4top
Upload code and rotate the potentiometer, and the LED brightness will change accordingly. The greater the ADC is, the brighter the LED will be.
4303
4bottom
HOW to Make an Automatic Lighting System with Kidspico
Previous
HOW to Make an Automatic Lighting System with Kidspico
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us