Adafruit Library
Public Member Functions | List of all members
ws_ledc Class Reference

High-level interface for the ESP32/ESP32-Sx/ESP32-Cx LED Control (LEDC) peripheral. Instead of specifying a timer or channel, this class automatically allocates a channel and associates it with a pin. Underlying esp32-hal-ledc performs timer management and handles the low-level LEDC peripheral API calls. More...

#include <ws_ledc.h>

Public Member Functions

 ws_ledc ()
 Ctor.
 
 ~ws_ledc ()
 Dtor.
 
bool attachPin (uint8_t pin, uint32_t freq, uint8_t resolution)
 Sets up a LEDC pin with given frequency and resolution. More...
 
bool detachPin (uint8_t pin)
 Detaches a pin from LEDC. More...
 
bool setDuty (uint8_t pin, uint32_t duty)
 Sets the duty cycle of a LEDC pin. More...
 
bool analogWrite (uint8_t pin, int value)
 Arduino AnalogWrite function, but for ESP32's LEDC. More...
 
uint32_t tone (uint8_t pin, uint32_t freq)
 Writes a square wave with a fixed duty cycle and variable frequency to a pin. Used by piezo buzzers and speakers. More...
 

Detailed Description

High-level interface for the ESP32/ESP32-Sx/ESP32-Cx LED Control (LEDC) peripheral. Instead of specifying a timer or channel, this class automatically allocates a channel and associates it with a pin. Underlying esp32-hal-ledc performs timer management and handles the low-level LEDC peripheral API calls.

Member Function Documentation

◆ attachPin()

bool ws_ledc::attachPin ( uint8_t  pin,
uint32_t  freq,
uint8_t  resolution 
)

Sets up a LEDC pin with given frequency and resolution.

Parameters
pinDesired GPIO pin number.
freqDesired timer frequency, in Hz.
resolutionDesired timer resolution, in bits.
Returns
True if configuration is successful. False is returned if error occurs and LEDC channel was not configured.

◆ detachPin()

bool ws_ledc::detachPin ( uint8_t  pin)

Detaches a pin from LEDC.

Parameters
pinDesired GPIO pin number.
Returns
True if successfully detached, False otherwise.

◆ setDuty()

bool ws_ledc::setDuty ( uint8_t  pin,
uint32_t  duty 
)

Sets the duty cycle of a LEDC pin.

Parameters
pinDesired GPIO pin to write to.
dutyDesired duty cycle.
Returns
True if duty cycle was set, False otherwise.

◆ analogWrite()

bool ws_ledc::analogWrite ( uint8_t  pin,
int  value 
)

Arduino AnalogWrite function, but for ESP32's LEDC.

Parameters
pinThe desired pin to write to.
valueThe duty cycle.
Returns
True if PWM value written to LEDC pin, False otherwise.

◆ tone()

uint32_t ws_ledc::tone ( uint8_t  pin,
uint32_t  freq 
)

Writes a square wave with a fixed duty cycle and variable frequency to a pin. Used by piezo buzzers and speakers.

Parameters
pinThe desired pin to write to.
freqThe frequency of the tone, in Hz.
Returns
The frequency of the LEDC pin. 0 if error occurs and LEDC pin was not configured.

The documentation for this class was generated from the following files: