ESPectro32 Library
Library for using ESPectro32 board
|
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 |
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%.
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).
[in] | gpioNum | The GPIO pin to use for output. |
[in] | frequency | The frequency of the period. |
[in] | bitSize | The 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] | timer | The timer to use. A value of LEDC_TIMER0, LEDC_TIMER1, LEDC_TIMER2 or LEDC_TIMER3. |
[in] | channel | The channel to use. A value from LEDC_CHANNEL0 to LEDC_CHANNEL1. |
uint32_t PWM::getDuty | ( | ) |
Get the duty cycle value.
uint32_t PWM::getFrequency | ( | ) |
Get 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.
[in] | duty | The duty cycle value. |
void PWM::setDutyPercentage | ( | uint8_t | percent | ) |
Set the duty cycle as a percentage value.
[in] | percent | The percentage of the duty cycle (0-100). |
void PWM::setFrequency | ( | uint32_t | freq | ) |
Set the frequency/period in Hz.
[in] | freq | The frequency to set the PWM. |
void PWM::stop | ( | bool | idleLevel = false | ) |
Stop the PWM.
[in] | idleLevel | The level to leave the output after end. |