X
Home > Blog > STEM for Arduino > HOW to Design a Lighting System with KidsIOT
HOW to Design a Lighting System with KidsIOT
By r August 26th, 2024 45 views
1. Description
As an introductory project for smart farms, lighting up LED is one of the most basic KidsBlock (based on Scratch) practical projects. It is designed to let beginners understand the hardware and software programming of kidsIOT board (based on ESP32) and master basic circuit and programming knowledge.
In this project, you will learn the basic connections and settings of the kidsIOT board in the KidsBlock graphical programming environment, as well as control the digital pin to output level to control the state of LEDs, LED breathing lights, and button control of LEDs, and you can also apply them in your home or lounge.

2. Components

Img

Img

Img

kidsIOT Mainboard×1

White LED Module×1

Button Module×1

Img

Img

Img

USB Cable×1

Wire×2

Lighting System LEGO Pieces×1

Img

3. Assembly Steps

Step 1:Components Needed

Img

Step 2: Process

Process 1:

Img

Process 2:

Img

Process 3:

Img

Process 4:

Img

Process 5:

Img

Process 6:

Img

Process 7:

Img

Complete

Img

4. Wiring Diagram

Module

kidsIOT Mainboard

White LED Module

No.2 port(control pin is io2)

Button Module

No.4 port(control pin is io27)

Connect the kidsIOT mainboard to your computer via USB cable.

Img

5. State of LED

Img

(1). Knowledge

(1)To keep the light on, the electricity is needed. When we say that there is electricity, we mean that there is current flowing through an electrical appliance like a light. Current comes to our home from the power station via wires. And the generator of a power station is the power supply, which enables to provide voltage and current. The battery we usually use is also the power supply. Wires can be used to conduct electricity, which connect a path for the current to flow. This path is called a circuit. If we want to make a lamp emit light, both a power supply and a complete circuit are needed.

img

(2). Programming Steps
Step 1:Description of the Building Blocks

Img

This block indicates that when the kidsIOT board is started, the code will be run.

image

Set input or output to the specified pin. input means input mode,output means output mode. Select input-pullup can set the input mode for the pin and make it become high level.

image

Set high or low to the specified pin. Select high means to set high level for the pin. If there is voltage and current, the LED will be on. Select low means to set low level for the pin. If there is no voltage and current, the LED will be off.

image

This is a delay block. The number 1 can be changed to whatever number of seconds it is delayed.

image

It will do one thing forever.

Step 2:Write the Program

① Open the KidsBlock(based on Scratch)software to select the kidsIOT board and port(COMx).

Img

② Drag the instruction block Imgin the Events module to the script area.

Img

③ Drag the instruction block Img in the “Pin” module to the script area. Since the white LED module is connected to the No. 2 interface on the mainboard (The control pin is io2) and it is in output mode, so change “input” to “output”.

Img

④ Drag the block Img in the “Control” module to the script area.

Img

⑤ Drag the block Img in the “Pin” module to Img.

Img

⑥ Drag the block Img in the “Control” module to Img and set to delay 1 second.

Img

⑦ Copy the code block Img into Img, and change “High” to “Low”.

Img

(3). Test Result

ClickImg to upload the above complete code to the kidsIOT mainboard. After powering up via the USB cable, the white LED will be on and off for 1s.

We can also realize breathing light effect, flowing water light effect and police light effect via LEDs.

Level

Function

high

LED lights up

low

LED lights off

6. LED Breathing LED

Img

(1). Knowledge

The IO port on the kidsIOT mainboard outputs digital signals, which can only output high level and low level. For example, in the lighting up LEDs project, the digital output of the ESP32 is used, which has only two levels: high (3.3V) and low (0V).

Assuming that the high level of the kidsIOT board is 3.3V and the low level is 0V, then if you want to output a voltage between 0 and 3.3V, you need to use PWM (Pulse Width Modulation). PWM can output different voltage values, like a progress bar, which is analog output.

Img

PWM uses digital control to generate square waves with different duty cycles (a signal that constantly switches between high level and low level) to control the analog output.

Img

PWM has three elements: frequency (Hz), period (s) and duty cycle (%)

  • PWM frequency (f): It refers to the number of PWM cycles in one second.

  • PWM period (T): Period=1/frequency (T=1/f, where 1 is 1 second), for example: the frequency is 50Hz, which means that one period is 20ms, then one second is 50 PWM cycles.

  • PWM duty cycle: It refers to the ratio of high level time to the entire cycle time within a pulse cycle. For example: the cycle time is 10ms, the pulse width time is 8ms, then the low level time is 2ms, and the total duty cycle is 8/(8+2)= 80%.

Img

PWM can change the effective output voltage by changing the duty cycle in one cycle under the appropriate signal frequency. Among the levels output by the IO port at the specified time, the more high levels, the greater the PWM value and the brighter the LED.

Img

(2). Programming Steps
Step 1:Description of the Building Blocks

The following are “Variable” command blocks.

Img

This block is used to create “Variable”. You can declare “global” or “local”, or set the type, name and value of the variable, item is the variable name.

Img

Get variable item.

Img

Set the value of variable item.

Img

Set the variable item mode to increase item by 1 or decrease item by 1 every time the loop is executed.

image

Set the string variable item.

Img

Img

This block is used to set the PWM. You need to set the corresponding pin via the channel (a total of 16 channels (0~15)) and the output value, so that the PWM value can be output.

Img

This is a conditional loop control statement that exit the loop when the number of loops is met. For example: 10 means that the loop is executed 10 times. The number 10 can be changed to other numbers.

Step 2:Write the Program

① Drag the instruction block Imgin the Events module to the script area.

Img

② Drag the instruction block Img in the “Pin” module to the script area, change “input” to “output”.

Img

③ Drag the block Img in the “Variable Type” module to the script area.

Img

④ Drag blocks Img and Img in the “Control” module to the script area.

Img

⑤ Drag the instruction blockImgin the “Pin” module to the script area.

Img

⑥ Drag the blockImgin the “Variable Type” module to the script area.

Img

⑦ Drag the blockImgin the “Variable Type” module to the script area,“++” means that each time the loop is executed, item will be increased by 1.

Img

⑧ Drag the block Img in the “Control” module to the script area and set the delay to 0.01 second, the repeat 10 to 255, for the corresponding PWM code block outputs 0~255. In this way, the LED light will slowly turn from dark to bright.

Img

⑨ Copy the code block Img,change “++” to “”, then LED will slowly turn from bright to dark.

Img

⑩ Complete Program

Img

(3). Test Result

ClickImg to upload the above complete code to the kidsIOT motherboard. After powering up via the USB cable, the LED will gradually brighten and then dim, like breathing.

7. Read the value of the button module

Img

(1). Knowledge

The principle of the button module is based on the switch circuit.

When the button is pressed, the switch closes, allowing current to pass through the button to GND, then the digital input pin of the kidsIOT motherboard detects a low level signal.

When not pressed, the switch is in the off state, the pin is pulled high by the pull-up resistor, and the digital input pin detects a high-level signal.

(2). Programming Steps
Step 1:Description of the Building Blocks

Img

The block is used to set serial baud rate(generally, the baud rate 9600 is taken as an example)

Img

This block is used to set print mode for the serial port. warp means line feed printing, no-warp means no line feed printing, HEX means hexadecimal printing.

Img

It is used to read the digital signal value of the specified pin(0 or 1).

Step 2:Write the Program

① Drag the instruction block Imgin the Events module to the script area.

Img

② Drag the instruction blockimage in the Serial module to the script area and take the baud rate 15200 as an example.

Img

③ Drag the block Img in the “Pin” module to the script area. Since the button module is connected to No. 4 port ( the control pin is io27) , so change pin IO2 to IO27.

Img

④ Drag the block Img in the “Variable Type” module to the script area, then change item to “Button”.

Img

⑤ Drag the block Img in the “Control” module to the script area.

Img

⑥ Drag the block Img in the “Variable Type” module to the script area, then change item to “Button”.

Img

⑦ Drag the block Img in the “Pin” module to the script area, then change pin IO2 to IO27.

Img

⑧ Drag the instruction blockimage in the Serial module to the script area.

Img

⑨ Drag the block Img in the “Variable Type” module to the script area, then change item to “Button”.

Img

⑩Drag the block Img in the “Control” module to the script area and set the delay to 0.1 second.

Img

⑪ Complete Program

Img

(3). Test Result

ClickImg to upload the above complete code to the kidsIOT motherboard. After powering up via the USB cable, click Img in the serial monitor and set the baud rate to 15200. When the button is not pressed, the serial monitor prints the digital signal 1, when pressed, the digital signal 0 is printed.

Img

8. Self-locking button function

Img

(1). Knowledge

Self-locking button: It locks when pressed and will not automatically pop up. It will pop up only when pressed again, which is very similar to a switch. The switch can be turned on and off using self-locking buttons. If it is controlled by the kidsIOT mainboard, this operation can also be achieved via software.

(2). Programming Steps
Step 1:Description of the Building Blocks

Img

It is a conditional statement code executing if-then function: If the logical judgment statement in Img is satisfied, the code statement below then is executed.

Img

It is a conditional statement: If the logical judgment statement in imageis satisfied, the loop will be executed continuously. If not, the loop will be terminated.

Img

It is a conditional statement code executing if-then-else function: If the logical judgment statement in Img is satisfied, the code statement below then is executed, otherwise, the code below else is executed.

Step 2:Write the Program

① First define a Value to get the button status, and then define a Button. At the same time, select the serial baud rate to 15200 and the control button pin IO27 to “input” mode.

Img

②Assign the read button value to “Value”.

Img

③ Determine whether the button is pressed. When pressed, change the value of “Button” and print it.

Img

Description

Img

The delay of 0.01 seconds is the button anti-shake function. After detecting that the button is closed, a delay program is executed, with a delay of 5ms ~ 10ms (depending on the mechanical characteristics). After the jitter disappears, the button status is detected again. If the closed state level is still maintained, then a button is pressed.

When it is released, a delay of 5ms ~ 10ms is also required. Only after the jitter disappears can the button processing program be transferred. When the button is pressed once, the button becomes 1, when pressed again, it becomes 0.

④ Complete Program

Img

(3). Test Result

ClickImg to upload the above complete code to the kidsIOT motherboard. After powering up via the USB cable, click Img in the serial monitor and set the baud rate to 15200. When the button is pressed, the serial monitor prints the number 1, and when pressed again, the monitor prints the number 0, so as to achieve the self-locking function of buttons.

Img

9. Lighting control system

Img

(1). Programming Steps
Step 1:Flow Chart

Img

Step 2:Write the Program

① Based on the code of the self-locking button above, add the relevant blocks for turning the LED on and off.

Img

② Complete Program

Img

(2). Test Result

ClickImg to upload the above complete code to the kidsIOT motherboard, then power up via the USB cable. When the button is pressed for the first time, the LED is turned on. When pressed for the second time, the LED is turned off. When pressed for the third time, the LED is turned on again…, which is consistent with the LED switch in real life.

Img

10. Common Problems
Q1: There are errors in kidsIOT board programming program

A: Please check whether the board type is correct.

Please check whether the USB port number is selected correctly.

Q2: The LED does not light up after burning the code

A: Please confirm whether the pins in the code are consistent with the actual wiring. If there is an error, please wire correctly according to the pins in the code.

Q3: Are the buttons insensitive? Sometimes it can be detected, sometimes not?
A: Modify the button delay time and set it to an appropriate delay.
Img
HOW to Make a Large Crane with Kidsuno
Previous
HOW to Make a Large Crane with Kidsuno
Read More
HOW to Make an Automatic Fan with Kidsuno
Next
HOW to Make an Automatic Fan with Kidsuno
Read More
Message Us