Skip to content

Commit d96ce9f

Browse files
committed
feat(ledc): Add example and fix log
1 parent 3fc241d commit d96ce9f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Diff for: cores/esp32/esp32-hal-ledc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c
136136
//get resolution of selected channel when used
137137
if (channel_used) {
138138
uint32_t channel_resolution = 0;
139-
log_i("Channel %u frequency: %u, resolution: %u", channel, ledc_get_freq(group, timer), channel_resolution);
140139
ledc_ll_get_duty_resolution(LEDC_LL_GET_HW(), group, timer, &channel_resolution);
140+
log_i("Channel %u frequency: %u, resolution: %u", channel, ledc_get_freq(group, timer), channel_resolution);
141141
handle->channel_resolution = (uint8_t)channel_resolution;
142142
}
143143
else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
LEDC Software Fade on shared channel with multiple pins
3+
4+
This example shows how to software fade LED
5+
using the ledcWriteChannel function on multiple pins.
6+
This example is useful if you need to control synchronously
7+
multiple LEDs on different pins.
8+
9+
Code adapted from original Arduino Fade example:
10+
https://www.arduino.cc/en/Tutorial/Fade
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
// use 8 bit precision for LEDC timer
16+
#define LEDC_TIMER_8_BIT 8
17+
18+
// use 5000 Hz as a LEDC base frequency
19+
#define LEDC_BASE_FREQ 5000
20+
21+
// LED pins
22+
#define LED_PIN_1 4
23+
#define LED_PIN_2 5
24+
25+
// LED channel that will be used instead of automatic selection.
26+
#define LED_CHANNEL 0
27+
28+
int brightness = 0; // how bright the LED is
29+
int fadeAmount = 5; // how many points to fade the LED by
30+
31+
void setup() {
32+
// Use single LEDC channel 0 for both pins
33+
ledcAttachChannel(LED_PIN_1, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT, LED_CHANNEL);
34+
ledcAttachChannel(LED_PIN_2, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT, LED_CHANNEL);
35+
}
36+
37+
void loop() {
38+
// set the brightness on LEDC channel 0
39+
ledcWriteChannel(LED_CHANNEL, brightness);
40+
41+
// change the brightness for next time through the loop:
42+
brightness = brightness + fadeAmount;
43+
44+
// reverse the direction of the fading at the ends of the fade:
45+
if (brightness <= 0 || brightness >= 255) {
46+
fadeAmount = -fadeAmount;
47+
}
48+
// wait for 30 milliseconds to see the dimming effect
49+
delay(30);
50+
}

0 commit comments

Comments
 (0)