• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

esp32 two frequency pwm output

Q&A Forum › Category: ESP32 › esp32 two frequency pwm output
0 Vote Up Vote Down
ENG asked 5 years ago

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
}
6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

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

0 Vote Up Vote Down
ENG answered 5 years ago
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
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

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

0 Vote Up Vote Down
ENG answered 5 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

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

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.