Arduino Forum

Using Arduino => Programming Questions => Topic started by: MFMFZY on Aug 18, 2017, 11:59 pm

Title: Double PWM signals with High frequency
Post by: MFMFZY on Aug 18, 2017, 11:59 pm
Dear all in arduino.cc
I want to make an interleaved dc/dc boost converter. The interleaved boost converter needs a square pulses each one have the same duty cycle but each one is shifted  about half  of the conduction period like this (check the attachments):
 
 The used frequency is 20000 HZ . I used arduino to get the pulses but only single pulse as in the program below:

#include <PWM.h>
int32_t frequency = 20000; // frequency in hertz
void setup()
{
InitTimersSafe ();
bool success = SetPinFrequencySafe (9,frequency);
if (success) {
  pinMode (9, OUTPUT); // connect the output pin at pin 10
  digitalWrite (9, HIGH);
}
}
void loop() {
  // put your main code here, to run repeatedly:
int sensorValue = analogRead (A1);
pwmWrite (9, sensorValue/4);
delay (30);
}


The used library is gotten from the links:
http://forum.arduino.cc/index.php?topic=117425.0
and
https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads

by changing potentiometer  the pulse width is changed. I am using arduino UNO where the library is changing the frequency only for pins 9 and 10. Now I know how to change the frequency  at  pin 9 for signal No.1  as shown in the picture,  but how to make another signal in pin 10 and shifted around half the frequency period of the first one. I hope that you can understand the matter and find a solutions to me. Thank you.

d
Title: Re: Double PWM signals with High frequency
Post by: MFMFZY on Aug 19, 2017, 12:05 am
please see the attached picture
Title: Re: Double PWM signals with High frequency
Post by: Power_Broker on Aug 19, 2017, 03:53 am
If you want multiple PWM outputs, you need multiple analogWrite() calls with each call addressed to different PWM pins. Is that what you are asking? You question/explanation is very difficult to follow.
Title: Re: Double PWM signals with High frequency
Post by: dougp on Aug 19, 2017, 03:35 pm
If you could get the Arduino to output 40KHz you run the signal to a D flip-flop set up as a divide-by-2 (http://www.electronics-tutorials.ws/sequential/seq_4.html) which would bring it back down to a 20KHz square wave.
Title: Re: Double PWM signals with High frequency
Post by: MarkT on Aug 19, 2017, 04:10 pm
You need to program one of the counters to have one pin inverted, then you can do this trick
(setting analogWrite to the same value then gives you antiphase, to slightly different values gives
slight gaps or slight overlap depending on the sense of the difference).  You have to use a phase
correct mode for this to work. 

Time to read the timer section(s) of the datasheet.
Title: Re: Double PWM signals with High frequency
Post by: MFMFZY on Aug 20, 2017, 12:39 am
Thanks for all replies...

I will extend the ideas from MarKT & Power_Broker theI will reply to you but I have made this code :

 #include <PWM.h>
int32_t frequency = 25000; // frequency in hertz

void setup()
{
InitTimersSafe ();
bool success = SetPinFrequencySafe (9,frequency);
if (success) {
  pinMode (9, OUTPUT); // connect the output pin at pin 10
  digitalWrite (9, HIGH);
  digitalWrite (10, HIGH);
}

}

void loop() {
  // put your main code here, to run repeatedly:
int sensorValue = analogRead (A1);
pwmWrite (9, sensorValue/4);
int sensorValue1 = analogRead (A0);
pwmWrite (10, sensorValue1/6);  // change the duty cycle ratio
delay (30);

}

I am still using one potentiometer and got 2 different duty cycles of 2 signals at same frequency. I did not get my aim but I will try . Please if any one can give good idea or nice code I will be thankful to him.
 



Title: Re: Double PWM signals with High frequency
Post by: jbellavance on Aug 20, 2017, 01:25 am
Hi,

Not only are you trying to change the duty cycles. You are also trying to change the width of the signal when it is high and low. I am not sure that PWM can do that.

Is 20KHz the the maximum frequency that you want to achieve?

If so, you could generate your pulses yourself (without PWM) using the Blink Without Delay technique.

Of course, if you are doing something else in the background, you might not fire the signal very acurately.

Jacques
Title: Re: Double PWM signals with High frequency
Post by: MFMFZY on Aug 20, 2017, 01:59 am
Dear Sir , thanks for reply

By the used library the frequency can be changed more than 20 KHZ.  I need the PWM because of regulation techniques later. I need to give fire signals to 2 transistors where each signal have the same frequency and duty cycles but shifted around half of the frequency period to each other.
Title: Re: Double PWM signals with High frequency
Post by: jbellavance on Aug 20, 2017, 02:04 am
Maybe you could fire PWM on pin 9, use delayMicroseconds(), and then fire PWM on pin 10.

At this point, I have no other idea.

Jacques
Title: Re: Double PWM signals with High frequency
Post by: dougp on Aug 20, 2017, 02:09 am
Have a look at Tone (https://www.arduino.cc/en/Reference/Tone).  You could set it for 40KHz and drive two divide-by-2 flip-flops. Drive one on the rising edge of 40KHz, the other on the falling edge of 40KHz.
Title: Re: Double PWM signals with High frequency
Post by: MFMFZY on Aug 20, 2017, 02:25 am
thanks dougp  :)

I will try it and tell you
Title: Re: Double PWM signals with High frequency
Post by: Anarit on Aug 20, 2017, 08:53 pm
I have a same  problem on arduino nano, i use PWM frequency library for 20Khz and use pwmWrite (9, val1), bute when use analogWrite(10, val2), both generate 20khz pwm and duty cycle is correct. But I'm not sure if it's reliable.
Title: Re: Double PWM signals with High frequency
Post by: MFMFZY on Aug 22, 2017, 02:24 pm
What do you mean about your correct pwm? Can you please show what is the modification that you have did?
Title: Re: Double PWM signals with High frequency
Post by: MorganS on Aug 23, 2017, 01:03 pm
You are going to have to search long and hard to find a library that does exactly what you want. I expect you will find something close but it will need deep modifications to do exactly what you want.

The reason is that the hardware is capable of doing what you want with only a few lines of code but it's such a rare requirement for Arduino experimenters that nobody has written the library to make it easy.

Read the section of the datasheet that talks about phase-correct PWM. It will take many re-readings to understand even 10% of what is there.
Title: Re: Double PWM signals with High frequency
Post by: MFMFZY on Aug 25, 2017, 12:27 pm
waw! you gave me weak hope!
Title: Re: Double PWM signals with High frequency
Post by: MarkT on Aug 25, 2017, 12:35 pm
Hi,

Not only are you trying to change the duty cycles. You are also trying to change the width of the signal when it is high and low. I am not sure that PWM can do that.
That's the same thing.  duty cycle controls the width of the high and low parts of the waveform (in concert
of course).

Read the datasheet of the IC if you want to find out what it can do, it can do a lot (there are 16 modes
for timer1, for instance).
Title: Re: Double PWM signals with High frequency
Post by: cattledog on Aug 25, 2017, 05:01 pm
Quote
I need to give fire signals to 2 transistors where each signal have the same frequency and duty cycles but shifted around half of the frequency period to each other.
Quote
the interleaved boost converter needs a square pulses each one have the same duty cycle but each one is shifted  about half  of the conduction period like this (check the attachments):
I am uncertain of your requirements given this image you posted(http://forum.arduino.cc/index.php?action=dlattach;topic=495680.0;attach=222756)

Can you please attach a hand drawn sketch of what you want? Do you want a deadband between the two pulses?

Check out these two threads for non overlapping complementary pulses
https://forum.arduino.cc/index.php?topic=438667.0 (https://forum.arduino.cc/index.php?topic=438667.0)
https://forum.arduino.cc/index.php?topic=495249.0 (https://forum.arduino.cc/index.php?topic=495249.0)
Title: Re: Double PWM signals with High frequency
Post by: johnwasser on Aug 26, 2017, 04:38 pm
The picture shows two pulse trains with DIFFERENT frequency and DIFFERENT duty-cycle.  That is not what is being described.  The descriptions seem to call for two identical PWM signals (SAME frequency and SAME duty cycle) that are 180° out of phase (offset by half the PWM period).  To do that you will need to use TWO timers.  Set them both up to generate the required PWM frequency.  Use the GTTCR (General Timer/Counter Control Register) to start them with the required phase difference.  They will then run at that phase offset and you can change the duty cycle as needed.