Using LEDC for generating two pulses
-
- Posts: 10
- Joined: Mon Jun 17, 2019 4:04 pm
Using LEDC for generating two pulses
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?
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?
-
- Posts: 9711
- Joined: Thu Nov 26, 2015 4:08 am
Re: Using LEDC for generating two pulses
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.
-
- Posts: 10
- Joined: Mon Jun 17, 2019 4:04 pm
Re: Using LEDC for generating two pulses
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);
}
// 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);
}
-
- Posts: 9711
- Joined: Thu Nov 26, 2015 4:08 am
Re: Using LEDC for generating two pulses
Erm...
That would make both variables refer to the exact same channel.
Code: Select all
const int ledChannel = 0;
const int ledChannel2 = 0;
-
- Posts: 10
- Joined: Mon Jun 17, 2019 4:04 pm
Re: Using LEDC for generating two pulses
How it refers to the same channel when the name is different?
Regards,
MEhdi
Regards,
MEhdi
-
- Posts: 9711
- Joined: Thu Nov 26, 2015 4:08 am
Re: Using LEDC for generating two pulses
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.
-
- Posts: 10
- Joined: Mon Jun 17, 2019 4:04 pm
Re: Using LEDC for generating two pulses
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.
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 (5.65 KiB) Viewed 11965 times
-
- Posts: 9711
- Joined: Thu Nov 26, 2015 4:08 am
Re: Using LEDC for generating two pulses
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.
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.
-
- Posts: 10
- Joined: Mon Jun 17, 2019 4:04 pm
Re: Using LEDC for generating two pulses
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?
How can we give delays to the channels when we are using LEDC?
Re: Using LEDC for generating two pulses
Instead of using LEDC, for <1Hz you would use GPIO and a delay loop. For example:
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
Code: Select all
void loop() {
digitalWrite(17, 1);
delay(2000);
digtalWrite(17, 0);
delay(2000);
}
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 33 guests