4.1 Project : Lighting System
Let’s start our first project, lighting system.
Lighting up an LED is one of the most fundamental KidsBlock practices.
This start-up lesson is designed for beginners to understand hardware and software programming on ESP32 development board and to master basic circuit and programming knowledge.

Therefore, our tutorial guidance is simple. And this intriguing project can be applied in actual scenarios at home or in office.
In this project, you will have learned the basic connections and settings of the ESP32 development board in the KidsBlock graphical programming. What’s more, some functions will also be presented for you, such as lighting on/off an LED via the output level of a digital pin or by a button.
All in all, this is an entry-level tutorial to lay the foundation for subsequent KidsBlock programming practices.
4.1.1 Flow Diagram

4.1.2 Light up an LED
Description:
LED, short for Light Emitting Diode, is a solid-state semiconductor that converts electrical energy into visible light, so it is also called solid-state lighting.
When current passes through an LED, it light up.
Various LED:

LED module is a device to output, whose brightness and blinks can be controlled. For how to use, you only need to directly plug it into digital output pins on the development board.

Working principle:
When S is at a high level, Q1 triode is into conduction, and VCC voltage passes through LED to light up it.

Parameters:
Voltage: 3~5V
Current: ≤1.5mA
Power: 0.07W
Wiring Diagram:
Connect the LED module to io27.
Attention: Connect yellow to S(Signal), red to V(Power), and black to GND. Do not reverse them!

Test Code:
Open Kidsblock and choose the correct device and port.

Drag
from
to the code editing area. Code Blocks execute only when they are in this area.

With this block, when booting the development board, code will run.

In
, drag “ forever ” and paste it below the previous block. Block “forever ” indicates a loop.

Drag an “LED pin output” block from
and paste it in “ forever ”. Set the pin to IO27 and output level to HIGH, so that the LED pin will continue to output high level.


Add a 1s delay. Duplicate the “LED pin output” block but set the output to LOW, and also add a delay. Then LED will light up and go off in circulation.

Test Result:
LED blinks per second, because io27 on ESP32 board outputs high and low level alternatively every second. Besides, various interactive applications can also be realized via an LED, like breathing LED, water flow lights and flashing police light.
Power Level |
Result |
|---|---|
HIGH |
LED on |
LOW |
LED off |
Expansion: Breathing LED
Description:
IO interfaces of MCU (like ESP32) output only digital signals (high or low level). For instance, in previous experiment (light up an LED), the digital outputs are only HIGH(3.3V) and LOW(0V).
If MCU outputs a high level of 3.3V or a low level of 0V, the input voltage should be at 0~3.3V. Thus, PWM (Pulse Width Modulation) is needed to output different voltage value, which is called “analog output”.

Knowledge:
What is PWM?
PWM contains three elements: Frequency(Hz), Period, Duty Cycle(%).
PWM Frequency (f): the times of signal changing from high to low and return to high within one second. Generally speaking, Frequency is the number of PWM Period in a second.
PWM Period (T): Period = 1 / Frequency (T=1/f, and 1 means 1 second). For instance: f = 50Hz, so T = 20ms, which implies there are 50 times of Period per second.
PWM Duty Cycle: the time ratio of HIGH to the whole Period. If Period = 10ms and 8ms is pulse width time, Low level occupies 2ms, so the Duty Cycle = 8/(8+2) = 80%.

Conclusion: At an appropriate signal frequency, PWM changes effective output voltage by changing the duty cycle in one period. In plain English, within a specified time, the more high level the IO port outputs, the greater PWM value is, and the lighter LED will be.

Test Code:

Define a variable item and assign it to 0.

Drag a “forever” block and paste a “repeat” block in it. Set repeat times to 255.

Drag a “variable mode” block in “repeat” and set the mode to “ ++ ”, which means item will increase 1 after each execution.

Find the block to set PWM which is contained in
as shown
below, so you only need to set corresponding pin and analog value to
output PWM.

Set LED pin:

Set channel: (16 channels in total: including 0~15)

Set PWM output value to item, which will automatically add 1 from 0 to 255. PWM output is 0~255, so we set the repeat times to 255.

Add a delay to 0.01s, so that LED will light up gradually rather than all of a sudden.

Duplicate the “repeat” block as follows, but set mode to “--”, which decreases variable item each time. And LED will dim gradually.

Test Result
LED lights up and dims gradually; it breathes evenly.

4.1.3 Lighting Control
Description
In above basic experiments, we remould an auto-locking button to control the LED. An auto-locking button is suitable for any situations where a certain state needs to be maintained, for example, when LED needs to light up for a long time, the ESP32 development board is required for some operations.
In this experiment, we will adopt the ESP32 PLUS board to guide you to implement a lighting system and simulate real-life scenes to control light via the button.
Wiring Diagram:
Connect the button to io5 and LED to io27
Attention: Connect yellow to S(Signal), red to V(Power), and black to GND. Do not reverse them!

Test Code:
Code Flow:

Complete code:
Based on the code for Auto-locking Button, we add “LED pin output” blocks.

Test Result:
When you press the button once, LED lights up; if you press again, LED turns off. This operation is a loop, which is consistent with the lighting principle in reality.
In this chapter, we have demonstrated how to program and control via KidsBlock, and we have learned the basics as well as some software and hardware concepts in experiments such as auto-locking button and lighting control system.
These are essential for a good KidsBlock developer. Next, we will guide you to keep exploring more applications and skills, whether you are a beginner or a veteran. Hope you enjoy the fun and challenges during learning KidsBlock. Let’s move on!
4.1.5 FAQ
Q: LED doesn’t light up after uploading code.
A: Please check whether the pin defined in code is consistent with that in your wirings. If they are incompatible, please adjust it referring to the code.
Q: The button sometimes works while sometimes doesn’t.
A: Please modify the delay of jitter elimination to a proper value.












