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

HOW to Achieve Light Adjustment with KidsIOT

By r September 20th, 2024 55 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 KidsBlock and connect to the board, click File –> Load from your computer.

3111

Choose D:\Code\2.Code_kidsIOT to open 4.3Brightness_adjustment.sb3

4303

Click 3209 to connect to port and then click 2210.

Explanation

5top

Code Blocks

Blocks

Code block

Events

begin

P

setmode

P

setpwm

P

readanalog

Control

forever

Data

map

line5

Conceive:

  1. Initialization

    Set the pin of the white LED and the potentiometer module, initialize the serial monitor.

    4304

line3

  1. Main Code

    Loop: Read the value output by the potentiometer, and set the value as the duty cycle of the LED module to control its brightness.

    4305


    Build blocks:

     forever

    ② Put setpwm into “forever” and set pin to IO3.

    ③ Add a map (map block).

    peg

    line3

    6top

    Q : The brightness value of LED is 0 ~ 255, while the output analog value of potentiometer is 0 ~ 4095. So how to control the brightness via the potentiometer?

    A : We use map block to map the output range to the brightness range.


    Q : What is MAP?

    A : In mathematics, MAP is the term for a relationship between two sets of elements that “correspond” to each other.

    Set: a whole consisting of one or more defined elements.

    There is a correspondence between Set A and B. For every Element a in Set A, there is always a unique element b corresponding to it in Set B, which is called map A to B.

    The concept of mapping is not only used in mathematics, but in computer science. In a computer program, mapping is a way to map each element in one set to a unique element in another.

    We assume that the output analog values of the potentiometer is A, and the brightness range is B. We map values in A to B, so the output value will correspond to a unique brightness value. This kind of one-to-one mapping enables the potentiometer to directly adjust the brightness.

    6bottom

    line3

    Five values need to be set in this map block:

    ​ (1): the value to be mapped;

    ​ (2 & 3): the range of the value to be mapped;

    ​ (4 & 5): the range after mapping.

    Set the mapped value as the analog value read by IO34: Drag readanalog to the first box and set pin to IO34.

    4306


    Set the range of the value to be mapped: here is the output range of the potentiometer(0 ~ 4095), so we set the second and third value to 0 and 4095 respectively.

    4307


    Set the range after mapping: here is the brightness value range of LED(0 ~ 255), so we set the forth and fifth value to 0 and 255 respectively.

    4308

    ④ Put the map block into the PWM output as the duty cycle of the white LED module, so that we can control the brightness via the potentiometer.

    4309

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.
4310
4bottom
HOW to Make an Automatic Lighting System with KidsIOT
Previous
HOW to Make an Automatic Lighting System with KidsIOT
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