ESP32 PWM using Arduino IDE

ESP32 has Five Power Modes:

  • Active Mode
  • Mode-Sleep Mode
  • Light-Sleep Mode
  • Deep-Sleep Mode
  • Hibernation Mode
Power ModeCPUWi-Fi/BluetoothRTC Memory & PeripheralsULPPower Consumption
Active ModeONONONONRefer [RF Modes]
Modem-Sleep ModeONOFFONON20 mA to 68 mA
Light-Sleep ModePAUSEOFFONON0.8 mA
Deep-Sleep ModeOFFOFFONON10 uA to 150 uA
Hibernation ModeOFFOFFRTC Memory – OFF
RTC Timer – ON (Slow clock 8 MHz)
RTC GPIOs – ON
OFF5 uA
Power OffOFFOFFOFFOFF1 uA
ESP32 Power Modes

Please find power consumption for various RF modes in ESP32:

RF ModeMinTypMaxUnit
Transmit 802.11b, DSSS 1 Mbps, POUT = +19.5 dBm240mA
Transmit 802.11g, OFDM 54 Mbps, POUT = +16 dBm190mA
Transmit 802.11n, OFDM MCS7, POUT = +14 dBm180mA
Receive 802.11b/g/n95 to 100mA
Transmit BT/BLE, POUT = 0 dBm130mA
Receive BT/BLE95 to 100mA
ESP32 Power Modes

pins, frequency voltage

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.

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);

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);

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);

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);

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
PinGPIO Pin (note: you need to assign the GPIO Pin as a OUTPUT)
valRanges from 0 to 255 (0=OFF, 255=Always ON)
Duty Cycle (%) = (val/255 )* 100
analogWrite() function
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);
  }
}
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).

ledcAttach(pin, analog_frequency, analog_resolution) function attaches a pin to the LEDC pin, with a given frequency, resolution (and channel automatically).

Parameters
  • pin: GPIO pin
  • analog_frequency: PWM Signal Frequency
  • analog_resolution: Resolution in number of bits
Returns
Success ( true ) or error ( false ) of the configuration.



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).

pinGPIO Pin (note: you need to assign the GPIO Pin as a OUTPUT)
analog_frequencyfrequency
analog_resolutionResolution in number of bits
analogWrite() function

CEW

WVE

void setup() {
  // Setup code
}
void loop() {
  // Loop code
}
pin: GPIO pin
pin: GPIO pin
pin: GPIO pin analogWrite()
pin: GPIO pin analogWrite()
pin: GPIO pin analogWrite()
pin: GPIO pin analogWrite()
pin: GPIO pin analogWrite()
pin: GPIO pin analogWrite(pin, val)>

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *