Page 1 of 2
Using LEDC for generating two pulses
Posted: Mon Jun 17, 2019 5:56 pm
by mehdi.na94rm
Hi,
I am using LEDC to generate two pulses one 1kHz and the other one 5kHz on the 16 and 17 output pins, respectively. But when I check the outputs, both the frequencies are the same and the interesting point is here that sometimes is 1kHz and after sometime it changes to 5 kHz. How can I fix it?
Re: Using LEDC for generating two pulses
Posted: Tue Jun 18, 2019 3:40 am
by ESP_Sprite
What's your code? The issue you may be running into is that all LED channels share two timer, so you can generate at max two frequencies, and you need to explicitly set up these two timers for this.
Re: Using LEDC for generating two pulses
Posted: Tue Jun 18, 2019 3:11 pm
by mehdi.na94rm
Hi, Thanks for your reply. I should mention that It should be fine to have the same frequency but I should be definitely able to have different duty cycles since I am going to run 6 switches. Here is my code:
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO16
const int ledPin2 = 17; // 17 corresponds to GPIO17
//const int ledPin2 = 18; // 18 corresponds to GPIO18
// setting PWM properties
const int freq = 1000;
const int freq2 = 5000;
const int ledChannel = 0;
const int ledChannel2 = 0;
//const int ledChannel2 = 0;
const int resolution = 10;
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel2, freq2, resolution);
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
ledcAttachPin(ledPin2, ledChannel2);
//ledcAttachPin(ledPin2, ledChannel2);
}
void loop(){
const int dutyCycle = 1024;
ledcWrite(ledChannel, dutyCycle);
const int dutyCycle2 = 64;
ledcWrite(ledChannel2, dutyCycle2);
// const int dutyCycle2 = 256;
// ledcWrite(ledChannel2, dutyCycle2);
// delay(20);
}
Re: Using LEDC for generating two pulses
Posted: Wed Jun 19, 2019 2:21 am
by ESP_Sprite
Erm...
Code: Select all
const int ledChannel = 0;
const int ledChannel2 = 0;
That would make both variables refer to the exact same channel.
Re: Using LEDC for generating two pulses
Posted: Wed Jun 19, 2019 1:27 pm
by mehdi.na94rm
How it refers to the same channel when the name is different?
Regards,
MEhdi
Re: Using LEDC for generating two pulses
Posted: Thu Jun 20, 2019 4:42 am
by ESP_Sprite
Sure, but the API doesn't look at the name of the variable, it looks at the number stored in it to figure out which channel to configure.
Re: Using LEDC for generating two pulses
Posted: Thu Jun 20, 2019 4:21 pm
by mehdi.na94rm
Thanks a lot for your comments and I could fix the frequency and the duty cycle. I have another concern, can we go below 1Hz? I have tried 0.125 Hz but it shows 1Hz, it seems the minimum is 1Hz, is there anyway to decrease the frequency below 1Hz?
In general, I am trying to generate the pulses like what I have attached here. What is your suggestion. let mw know if I am in the wrong direction.
Re: Using LEDC for generating two pulses
Posted: Fri Jun 21, 2019 2:38 am
by ESP_Sprite
That is extremely slow... is there a reason why you don't just generate these pulses in software?
The issue here is that the hardware possibly can go lower than 1Hz, but the Arduino HAL driver you're using only understands values of 1Hz or above. You could try poking the LEDC registers directly in order to modify the dividers to get your requested frequency, but it's a bit hacky.
Re: Using LEDC for generating two pulses
Posted: Fri Jun 21, 2019 2:34 pm
by mehdi.na94rm
Great. Creating them by the software you mean just giving some delays and create those pulses from delay function?
How can we give delays to the channels when we are using LEDC?
Re: Using LEDC for generating two pulses
Posted: Thu Jun 27, 2019 11:39 pm
by nevercast
Instead of using LEDC, for <1Hz you would use GPIO and a delay loop. For example:
Code: Select all
void loop() {
digitalWrite(17, 1);
delay(2000);
digtalWrite(17, 0);
delay(2000);
}
This gives a 0.5Hz output, and does not need LEDC.
Perhaps if you tell us what the pulses need to do, and why you need pulses, we can provide better help