ESPectro32 Library
Library for using ESPectro32 board
 All Classes Functions Variables Enumerations Enumerator Pages
Public Member Functions | Private Attributes | List of all members
PWM Class Reference

A wrapper for ESP32 PWM control. More...

#include <PWM.h>

Public Member Functions

 PWM (int gpioNum, uint32_t frequency=100, ledc_timer_bit_t bitSize=LEDC_TIMER_10_BIT, ledc_timer_t timer=LEDC_TIMER_0, ledc_channel_t channel=LEDC_CHANNEL_0)
 Construct an instance. More...
 
uint32_t getDuty ()
 Get the duty cycle value. More...
 
uint32_t getFrequency ()
 Get the frequency/period in Hz. More...
 
void setDuty (uint32_t duty)
 Set the duty cycle value. More...
 
void setDutyPercentage (uint8_t percent)
 Set the duty cycle as a percentage value. More...
 
void setFrequency (uint32_t freq)
 Set the frequency/period in Hz. More...
 
void stop (bool idleLevel=false)
 Stop the PWM. More...
 

Private Attributes

ledc_channel_t channel
 
ledc_timer_t timer
 
ledc_timer_bit_t bitSize
 

Detailed Description

A wrapper for ESP32 PWM control.

Pulse Width Modulation (PWM) is known as "LEDC" in the ESP32. It allows us to set a repeating clock signal. There are two aspects to the signal called the frequency and duty cycle. The frequency is how many times per second the signal repeats. The duty cycle is how much of a single period the output signal is high compared to low. For example, a duty cycle of 0% means that the signal is always low, while a duty cycle of 100% means the signal is always high. A duty cycle of 50% means that the signal is high for 50% of the output and low for the remaining 50%.

Constructor & Destructor Documentation

PWM::PWM ( int  gpioNum,
uint32_t  frequency = 100,
ledc_timer_bit_t  bitSize = LEDC_TIMER_10_BIT,
ledc_timer_t  timer = LEDC_TIMER_0,
ledc_channel_t  channel = LEDC_CHANNEL_0 
)

Construct an instance.

A timer starts ticking up from 0 to the maximum value of the bit size. When it reaches the maximum value, it resets.

In the following example, we create a signal that has a frequency of 100Hz and is a square wave (50% on, 50% off).

PWM pwm(GPIO_NUM_21);
pwm.setDutyPercentage(50);
pwm.setFrequency(100);
Parameters
[in]gpioNumThe GPIO pin to use for output.
[in]frequencyThe frequency of the period.
[in]bitSizeThe size in bits of the timer. Allowed values are LEDC_TIMER_10_BIT, LEDC_TIMER_11_BIT, LEDC_TIMER_12_BIT, LEDC_TIMER_13_BIT, LEDC_TIMER_14_BIT, LEDC_TIMER_15_BIT.
[in]timerThe timer to use. A value of LEDC_TIMER0, LEDC_TIMER1, LEDC_TIMER2 or LEDC_TIMER3.
[in]channelThe channel to use. A value from LEDC_CHANNEL0 to LEDC_CHANNEL1.
Returns
N/A.

Member Function Documentation

uint32_t PWM::getDuty ( )

Get the duty cycle value.

Returns
The duty cycle value.
uint32_t PWM::getFrequency ( )

Get the frequency/period in Hz.

Returns
The frequency/period in Hz.
void PWM::setDuty ( uint32_t  duty)

Set the duty cycle value.

The duty cycle value is a numeric between 0 and the maximum bit size. When the PWM tick value is less than this value, the output is high while when it is higher than this value, the output is low.

Parameters
[in]dutyThe duty cycle value.
Returns
N/A.
void PWM::setDutyPercentage ( uint8_t  percent)

Set the duty cycle as a percentage value.

Parameters
[in]percentThe percentage of the duty cycle (0-100).
Returns
N/A.
void PWM::setFrequency ( uint32_t  freq)

Set the frequency/period in Hz.

Parameters
[in]freqThe frequency to set the PWM.
Returns
N/A.
void PWM::stop ( bool  idleLevel = false)

Stop the PWM.

Parameters
[in]idleLevelThe level to leave the output after end.
Returns
N/A.

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