ESP32 PWM using Arduino IDE
pin : GPIO pinpin : GPIO pin
Table of Content
Pulse Width Modulation (PWM)
ESP32 has Five Power Modes:
- Active Mode
- Mode-Sleep Mode
- Light-Sleep Mode
- Deep-Sleep Mode
- Hibernation Mode
Power Mode | CPU | Wi-Fi/Bluetooth | RTC Memory & Peripherals | ULP | Power Consumption |
---|---|---|---|---|---|
Active Mode | ON | ON | ON | ON | Refer [RF Modes] |
Modem-Sleep Mode | ON | OFF | ON | ON | 20 mA to 68 mA |
Light-Sleep Mode | PAUSE | OFF | ON | ON | 0.8 mA |
Deep-Sleep Mode | OFF | OFF | ON | ON | 10 uA to 150 uA |
Hibernation Mode | OFF | OFF | RTC Memory – OFF RTC Timer – ON (Slow clock 8 MHz) RTC GPIOs – ON | OFF | 5 uA |
Power Off | OFF | OFF | OFF | OFF | 1 uA |
Please find power consumption for various RF modes in ESP32:
RF Mode | Min | Typ | Max | Unit |
---|---|---|---|---|
Transmit 802.11b, DSSS 1 Mbps, POUT = +19.5 dBm | – | 240 | – | mA |
Transmit 802.11g, OFDM 54 Mbps, POUT = +16 dBm | – | 190 | – | mA |
Transmit 802.11n, OFDM MCS7, POUT = +14 dBm | – | 180 | – | mA |
Receive 802.11b/g/n | – | 95 to 100 | – | mA |
Transmit BT/BLE, POUT = 0 dBm | – | 130 | – | mA |
Receive BT/BLE | – | 95 to 100 | – | mA |
ESP32 PWM Specs
pins, frequency voltage
LED brightness control
battery backup = battery capacity (mAH) / current consumption
battery backup(Active Mode) = 500 (mAH) / 240 mA = 2.1 Hours
battery backup(Deep-Sleep) = 500 (mAH) / {(150*0.9 uA)+(240*0.1 mA)} = 21 Hours
From the battery life calculation, you can clearly see that the watch will have 10x more battery backup. You can still increase the battery backup by putting the ESP32 into Hibernation mode and increasing the sleep duration.
Putting ESP32 in different Power Modes?
The function esp_deep_sleep_start(void) puts ESP32 in Deep-Sleep Mode. However, you need to define the wakeup source before putting ESP32 into sleep mode.
esp_deep_sleep_start(void);
Wakeup Sources
Timer Wake-up
ESP32 can wake up at predetermined times. It is useful in applications where devices don’t need to remain active all the time.
- Applications:
- Alarm clocks in mobile,
- Smart Watch,
- Weather Station data logger
esp_sleep_enable_timer_wakeup(time_in_us);
Touch Wake-up
ESP32 will wake up when touch is detected on the capacitive touch-sensing GPIO pins.
- Applications:
- Fingerprint Scanner,
- Touch wake-up on the Smartphone screen
touchSleepWakeUpEnable(pin, threshold);
External (GPIO) Wake-up
ESP32 will wake up when the signal is detected on pins.
- Applications
- Wildlife tracking Camera (captures photo when movement is detected),
- The power button in the Smartphone
esp_sleep_enable_ext0_wakeup(pin, level);
AND
esp_sleep_enable_ext1_wakeup(pin, level);
analogWrite() function
void analogWrite(Pin, val);
analogWrite(pin, dutyCycle) function generates steady rectangular wave until next call to analogWrite() (or digitalWrite() or digitalRead) on the same pin. analogWrite() function returns nothing.
Pin | GPIO Pin (note: you need to assign the GPIO Pin as a OUTPUT) |
val | Ranges from 0 to 255 (0=OFF, 255=Always ON) Duty Cycle (%) = (val/255 )* 100 |
int ledPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
// Increasing LED brightness
for(int val=0; val<=255; val++)
{
analogWrite(ledPin, val);
delay(1);
}
// Decreasing LED brightness
for(int val=255; val>=0; val--)
{
analogWrite(ledPin, val);
delay(1);
}
}
ledcAttach() & ledcWrite()
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
ledcAttach(pin, analog_frequency, analog_resolution) function attaches a pin to the LEDC pin, with a given frequency, resolution (and channel automatically).
pin : GPIO pinanalog_frequency : PWM Signal Frequencyanalog_resolution : Resolution in number of bits
Success (
bool ledcWrite(uint8_t pin, uint32_t duty);
bool ledcWrite(uint8_t pin, uint32_t duty) function is used to set PWM duty cycle on the pin.
Parameters
Pin: GPIO Pin
duty: duty cycle (0 to 2^N-1)
Returns: success (true) or error (false) of the configuration
-
pin
: GPIO Pin -
duty
: duty cycle (0 to 2^N-1)
analog_resolution
function attaches a pin to the LEDC pin, with a given frequency, resolution (and channel automatically).
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
ledcAttach(pin, analog_frequency, analog_resolution) function attaches a pin to the LEDC pin, with a given frequency, resolution (and channel automatically).
pin | GPIO Pin (note: you need to assign the GPIO Pin as a OUTPUT) |
analog_frequency | frequency |
analog_resolution | Resolution in number of bits |
CEW
WVE
void setup() {
// Setup code
}
void loop() {
// Loop code
}