I want to output two PWM signals of different frequencies by esp32 with two GPIO pins
- GPIO 13 outputs 1kHz 50% PWM
- GPIO 12 output 27.7kHz 50% PWM
After measurement by oscilloscope
The output frequency of the two pins will be the same as 27.7 kHz
How can I solve this problem?
Make my two GPIO output pins have different frequencies
const int ledPin = 13; const int freq = 1000; const int ledChannel = 0; const int resolution = 8; const int dutyCycle = 128; /////////////////////////// const int ledPin2 = 12; const int freq2 = 27700; const int ledChannel2 = 0; const int resolution2 = 8; const int dutyCycle2 = 128; ///////////////////////////// void setup() {ledcSetup(ledChannel,freq,resolution); ledcAttachPin(ledPin, ledChannel); ledcSetup(ledChannel2,freq2,resolution2); ledcAttachPin(ledPin2,ledChannel2); } void loop() { ledcWrite(ledChannel,dutyCycle);///////// 1khz ledcWrite(ledChannel2,dutyCycle2);/////////27.7khz }
Hi.
You need two different PWM channels.
You have both channels set to 0
const int ledChannel = 0;
const int ledChannel2 = 0;
Set a different channel.
Then, tell me if it works.
Regards,
Sara
the frequency of two pin are still the same as 27.7kHz after I change the ledChannel here is my code const int ledPin = 13; const int freq = 1000; const int ledChannel = 0; const int resolution = 8; const int dutyCycle = 128; /////////////////////////////// const int ledPin2 = 12; const int freq2 = 27700; const int ledChannel2 = 1; const int resolution2 = 8; const int dutyCycle2 = 128; ////////////////////////////////// void setup() {ledcSetup(ledChannel,freq,resolution); ledcAttachPin(ledPin,ledChannel); delay(1); ledcSetup(ledChannel2,freq2,resolution2); ledcAttachPin(ledPin2,ledChannel2); } void loop() { ledcWrite(ledChannel,dutyCycle); ///////// 1khz ledcWrite(ledChannel2,dutyCycle2); /////////27.7khz }
This is the result of my measurement yellow:pin 13 green:pin 12 https://drive.google.com/file/d/1rwXy5Iqwyhi55K_i0czh8fMU8Dj5d6C3/view
If I’m not mistaken, channel 0 and channel 1 are grouped together.
So, choose channel 0 and channel 2.
I’ve experimented channel 0 and channel 2 and it works well.
Offtopic question: What do you thing of that handheld oscilloscope? We’ve ordered one from Banggood. We’ll make a review when it arrives.
Regards,
Sara
perfect !!!
thanks for helping my problem
here’s my result
https://drive.google.com/open?id=12lz94C1U1AZvaHLt39uPwDcrYZ3RUcha
is there any other channels are grouped together too?
The handheld oscilloscope is a very convenience tool you can take outdoor it doesn’t take up too much space
but some subtle signal changes or electronic surge are unmeasured and it takes time to familiar with the operation
Great!
Thanks for your feedback.
I think that for most of my work that oscilloscope is perfect.
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara