SunFounder ESP32 Starter Kit
const int ledPin = 26; // The GPIO pin for the LED
2. Now, initialize the pin in the setup() function, where you need to initialize the pin to OUTPUT mode.
void setup() {
pinMode(ledPin, OUTPUT);
}
• void pinMode(uint8_t pin, uint8_t mode);: This function is used to define the GPIO
operation mode for a specific pin.
– pin defines the GPIO pin number.
– mode sets operation mode.
The following modes are supported for the basic input and output:
– INPUT sets the GPIO as input without pullup or pulldown (high impedance).
– OUTPUT sets the GPIO as output/read mode.
– INPUT_PULLDOWN sets the GPIO as input with the internal pulldown.
– INPUT_PULLUP sets the GPIO as input with the internal pullup.
3. The loop() function contains the main logic of the program and runs continuously. It alternates between setting
the pin high and low, with one-second intervals between the changes.
void loop() {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage␣
˓→level)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage␣
˓→LOW
delay(1000); // wait for a second
}
• void digitalWrite(uint8_t pin, uint8_t val);: This function sets the state of the se-
lected GPIO to HIGH or LOW. This function is only used if the pinMode was configured as OUTPUT.
– pin defines the GPIO pin number.
– val set the output digital state to HIGH or LOW.
1.6 2.2 Fading
In the previous project, we controlled the LED by turning it on and off using digital output. In this project, we will
create a breathing effect on the LED by utilizing Pulse Width Modulation (PWM). PWM is a technique that allows us
to control the brightness of an LED or the speed of a motor by varying the duty cycle of a square wave signal.
With PWM, instead of simply turning the LED on or off, we will be adjusting the amount of time the LED is on versus
the amount of time it is off within each cycle. By rapidly switching the LED on and off at varying intervals, we can
create the illusion of the LED gradually brightening and dimming, simulating a breathing effect.
By using the PWM capabilities of the ESP32 WROOM 32E, we can achieve smooth and precise control over the
LED’s brightness. This breathing effect adds a dynamic and visually appealing element to your projects, creating an
eye-catching display or ambiance.
Required Components
22 Chapter 1. For Arduino User