Posts: 2
Joined: Sat Mar 31, 2018 4:17 pm


Postby gregaryb » Mon Feb 04, 2019 12:41 pm

I have never been able to get my head around the concept of 'channels'.

E.G. In this ESP32 example sketch

What is the point behind multiple channels associated with a digital pin?

ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);

Code: Select all

LEDC Software Fade

This example shows how to software fade LED
using the ledcWrite function.

Code adapted from original Arduino Fade example:

This example code is in the public domain.

// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0

// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13

// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 5000

// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN 5

int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
// calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);

// write duty to LEDC
ledcWrite(channel, duty);

void setup() {
// Setup timer and attach timer to a led pin
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);

void loop() {
// set the brightness on LEDC channel 0
ledcAnalogWrite(LEDC_CHANNEL_0, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
// wait for 30 milliseconds to see the dimming effect

Posts: 2665
Joined: Thu Nov 26, 2015 4:08 am

Re: Channels?

Postby ESP_Sprite » Thu Feb 07, 2019 4:39 am

It may help if you look up the LED_PWM section in the TRM. Long story short, the LED_PWM peripheral has 2x8 PWM outputs, called channels. You can set the PWM value of each of those channels and get a PWM signal from it. These signals aren't by default routed to any specific GPIO; they go to the GPIO matrix, which is a big switchboard allowing you to connect any PWM output to any GPIO pin. What your code does is first to set up channel 0 to generate a specific PWM signal, second to use the GPIO matrix to hook up that channel to a GPIO pin.

Who is online

Users browsing this forum: No registered users and 2 guests