How to control the onboard LEDC of ESP32-S2 #1206
Answered
by
Ellerbach
sangyuxiaowu
asked this question in
Q&A
-
I have a development version of the ESP32 development board brushed FEATHER_S2 firmware. I want to control the onboard LEDC. I tried many ways, but I can only make it always on, and I can't control it. The following is provided Arduino example: /*
LEDC Software Fade
This example shows how to software fade LED
using the ledcWrite function.
Code adapted from original Arduino Fade example:
https://www.arduino.cc/en/Tutorial/Fade
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 9
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
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
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
delay(30);
}
this is my code: using nanoFramework.Hardware.Esp32;
using System;
using System.Device.Pwm;
using System.Diagnostics;
using System.Threading;
namespace BlinkBoardLed
{
public class Program
{
public static void Main()
{
Configuration.SetPinFunction(9, DeviceFunction.PWM1);
PwmChannel led = PwmChannel.CreateFromPin(9, 5000, 0.4);
led.Start();
bool goingUp = true;
float dutyCycle = .00f;
while (true)
{
if (goingUp)
{
// slowly increase light intensity
dutyCycle += 0.05f;
// change direction if reaching maximum duty cycle (100%)
if (dutyCycle > .95)
goingUp = !goingUp;
}
else
{
// slowly decrease light intensity
dutyCycle -= 0.05f;
// change direction if reaching minimum duty cycle (0%)
if (dutyCycle < 0.10)
goingUp = !goingUp;//break;
}
// update duty cycle
led.DutyCycle = dutyCycle;
Debug.WriteLine(dutyCycle.ToString());
Thread.Sleep(500);
}
led.DutyCycle = .00f;
led.Stop();
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
Ellerbach
Jan 12, 2023
Replies: 1 comment 2 replies
-
From the data sheet you sent, the power led will always be on: |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sangyuxiaowu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the data sheet you sent, the power led will always be on:
It is not controllable by software.