kidsIOT Mainboard×1 |
White LED Module×1 |
Button Module×1 |
USB Cable×1 |
Wire×2 |
Lighting System LEGO Pieces×1 |
Process 1:
Process 2:
Process 3:
Process 4:
Process 5:
Process 6:
Process 7:
Complete
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.
(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.
This block indicates that when the kidsIOT board is started, the code will be run.
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.
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.
This is a delay block. The number 1 can be changed to whatever number of seconds it is delayed.
It will do one thing forever.
① Open the KidsBlock(based on Scratch)software to select the kidsIOT board and port(COMx).
② Drag the instruction block in the Events module to the script area.
③ Drag the instruction block 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”.
④ Drag the block in the “Control” module to the script area.
⑤ Drag the block in the “Pin” module to .
⑥ Drag the block in the “Control” module to and set to delay 1 second.
⑦ Copy the code block into , and change “High” to “Low”.
Click 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 |
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.
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.
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%.
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.
The following are “Variable” command blocks.
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.
Get variable item.
Set the value of variable item.
Set the variable item mode to increase item by 1 or decrease item by 1 every time the loop is executed.
Set the string variable item.
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.
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.
① Drag the instruction block in the Events module to the script area.
② Drag the instruction block in the “Pin” module to the script area, change “input” to “output”.
③ Drag the block in the “Variable Type” module to the script area.
④ Drag blocks and in the “Control” module to the script area.
⑤ Drag the instruction blockin the “Pin” module to the script area.
⑥ Drag the blockin the “Variable Type” module to the script area.
⑦ Drag the blockin the “Variable Type” module to the script area,“++” means that each time the loop is executed, item will be increased by 1.
⑧ Drag the block 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.
⑨ Copy the code block ,change “++” to “–”, then LED will slowly turn from bright to dark.
⑩ Complete Program
Click 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.
① Based on the code of the self-locking button above, add the relevant blocks for turning the LED on and off.
② Complete Program
Click 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.
A: Please check whether the board type is correct.
Please check whether the USB port number is selected correctly.
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.