Using LEDC for generating two pulses

mehdi.na94rm
Posts: 10
Joined: Mon Jun 17, 2019 4:04 pm

Using LEDC for generating two pulses

Postby mehdi.na94rm » Mon Jun 17, 2019 5:56 pm

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?

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Using LEDC for generating two pulses

Postby ESP_Sprite » Tue Jun 18, 2019 3:40 am

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.

mehdi.na94rm
Posts: 10
Joined: Mon Jun 17, 2019 4:04 pm

Re: Using LEDC for generating two pulses

Postby mehdi.na94rm » Tue Jun 18, 2019 3:11 pm

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);


}

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Using LEDC for generating two pulses

Postby ESP_Sprite » Wed Jun 19, 2019 2:21 am

Erm...

Code: Select all

const int ledChannel = 0;
const int ledChannel2 = 0;
That would make both variables refer to the exact same channel.

mehdi.na94rm
Posts: 10
Joined: Mon Jun 17, 2019 4:04 pm

Re: Using LEDC for generating two pulses

Postby mehdi.na94rm » Wed Jun 19, 2019 1:27 pm

How it refers to the same channel when the name is different?

Regards,
MEhdi

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Using LEDC for generating two pulses

Postby ESP_Sprite » Thu Jun 20, 2019 4:42 am

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.

mehdi.na94rm
Posts: 10
Joined: Mon Jun 17, 2019 4:04 pm

Re: Using LEDC for generating two pulses

Postby mehdi.na94rm » Thu Jun 20, 2019 4:21 pm

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.
Attachments
pulses.png
pulses.png (5.65 KiB) Viewed 12088 times

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Using LEDC for generating two pulses

Postby ESP_Sprite » Fri Jun 21, 2019 2:38 am

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.

mehdi.na94rm
Posts: 10
Joined: Mon Jun 17, 2019 4:04 pm

Re: Using LEDC for generating two pulses

Postby mehdi.na94rm » Fri Jun 21, 2019 2:34 pm

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?

nevercast
Posts: 7
Joined: Tue Jan 08, 2019 2:58 am

Re: Using LEDC for generating two pulses

Postby nevercast » Thu Jun 27, 2019 11:39 pm

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 :)

Who is online

Users browsing this forum: No registered users and 75 guests