ESP32 Starter Kit
(continued from previous page)
void loop() {
digitalWrite(ledPin, HIGH); //Output a high level, LED lights up
delay(1000);//Delay 1000ms
digitalWrite(ledPin, LOW); //Output a low level, LED goes off
delay(1000);
}
5. Test Result
After uploading the code and powering on, LED will light up for 1s and off for 1 s.
6. Code Explanation
setup()Function: It is used to initialize variables and pin modes and to enable the library. It runs once only after each
time the board powering on or being reset.
loop()Function: Followed by setup(), loop()function perpetually executes its code, such as read the pin or output the
pin.
int ledPin = 3: “int” is a variable within range of -32768 ~ 32767. This example code means we define an variable
ledpin with an assignment of 5. Therefore, we adopt ledpin rather than “5” in later steps, which largely simplifies
experimental recordings when considerable sensors and pins are included.
pinMode(pin,mode):“pin” is the pin number of mode setting. And the “mode” is optional for INPUT, OUTPUT, and
INPUT_PULLUP. Here we set pin 3 to output mode.
digitalWrite(pin, value): “pin” is the digital tube pin of MCU, and here we define as pin 5. “value” is the digital output
level (HIGH/LOW). If we apply pinMode() to set pin to OUTPUT, its voltage should be modified correspondingly. For
instance, 5V (it is 3.3V if on a 3.3V-board) corresponds to HIGH, while 0V (GND) is for LOW.
However, if LED links with the pin rather than setting pinMode() to OUTPUT, LED may become dim when recalling
digitalWrite(HIGH). This is because digitalWrite() enables the inner pull-up resistor, whose function is similar to a
great current-limiting resistor.
delay(ms) :It is a delay function and “ms” is the delay time in micro seconds.
For more Arduino grammar explanations, please refer to: https://www.arduino.cc/reference/en/
8.5.3 Project 2: Breathing LED
1. Description
Arduino breathing led utilizes on-board programmable PWM to output analog waveform. After powering on, LED
brightness can be adjusted through duty cycle of the waveform to eventually realize the effect of breathing led. In this
way, ambient light can be simulated by changing LED brightness over time. Also, breathing led can form a colorful
mini light to construct a tranquil and warm environment.
8.5. Arduino Project 47