Arduino- Duty Cycle Changing When Frequency Is IncreasingPractical issue with PIC PWMproblem of transmitting data from serial device to bluetooth on ArduinoPWM + LC filter - voltage value non linear with duty cycleArduino - Pulses get unstable when updating PWM duty-cyclefluctuating ADC Reading for 4-20mA Pressure transducer inputBuck converter, duty cycle has inverse relationship with Vout?Arduino not outputting square wave with 50% duty cycleArduino - Not outputting 2 separate signals

Levenshtein Neighbours

Moons that can't see each other

Why should someone be willing to write a strong recommendation even if that means losing a undergraduate from their lab?

Arduino- Duty Cycle Changing When Frequency Is Increasing

Have only girls been born for a long time in this village?

Why didn’t Doctor Strange stay in the original winning timeline?

Homogeneous Equations and Linear Algebra

Starships without computers?

Count the frequency of items in an array

How do you call it when two celestial bodies come as close to each other as they will in their current orbits?

Unsolved Problems (Not Independent of ZFC) due to Lack of Computational Power

Why do some academic journals requires a separate "summary" paragraph in addition to an abstract?

Nuclear decay triggers

Interaction between Ethereal Absolution versus Edgar Markov with Captivating Vampire

!I!n!s!e!r!t! !n!b!e!t!w!e!e!n!

Why doesn't the Falcon-9 first stage use three legs to land?

How did Apollo 15's depressurization work?

Stuffing in the middle

Limits and Continuity in Multi variable calculus.

Metal that glows when near pieces of itself

Are there any OR challenges that are similar to kaggle's competitions?

Is it safe to reuse the password when using AES-CTR with scrypt?

Does git delete empty folders?

Can my Boyfriend, who lives in the UK and has a Polish passport, visit me in the USA?



Arduino- Duty Cycle Changing When Frequency Is Increasing


Practical issue with PIC PWMproblem of transmitting data from serial device to bluetooth on ArduinoPWM + LC filter - voltage value non linear with duty cycleArduino - Pulses get unstable when updating PWM duty-cyclefluctuating ADC Reading for 4-20mA Pressure transducer inputBuck converter, duty cycle has inverse relationship with Vout?Arduino not outputting square wave with 50% duty cycleArduino - Not outputting 2 separate signals






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


I’m currently trying to increase the frequency from a pin from an Arduino using PWM. I’m using the Timer1 library as it has functions to output signals.



I connected a push button to my Arduino and this is meant to increase the frequency of the pin once pressed . But every time the button is pressed, the duty cycle is also changed. Also the frequency changes multiple when the button is pressed. I think that has something to do with the lastButtonState logic.
I was wondering if there’s anything that can implemented in my code that can increase the frequency but keep the duty cycle the same.
Thanks in advance.



#include <TimerOne.h>

int buttonState = 0;
int lastButtonState = 0;
int period = 1000;
int sig_out = 9;
int sig_out2 = 10;
int button =2;

void setup()

pinMode(button,INPUT_PULLUP);
pinMode(sig_out, OUTPUT);
//pinMode(sig_out2, OUTPUT);
Timer1.initialize(1000);
Timer1.pwm(sig_out,507,period);
Serial.print(period);
//Timer1.pwm(sig_out2,456,900);

void loop()
buttonState = digitalRead(button);
if (buttonState != lastButtonState)
lastButtonState = buttonState;
if (buttonState == 1)
period -= 10;
Timer1.setPeriod(period);





Schematic










share|improve this question









$endgroup$













  • $begingroup$
    I don't see any debounce for the switch. This could be happening because of that. Try using debounce and see if it helps
    $endgroup$
    – Prateek Dhanuka
    9 hours ago

















3












$begingroup$


I’m currently trying to increase the frequency from a pin from an Arduino using PWM. I’m using the Timer1 library as it has functions to output signals.



I connected a push button to my Arduino and this is meant to increase the frequency of the pin once pressed . But every time the button is pressed, the duty cycle is also changed. Also the frequency changes multiple when the button is pressed. I think that has something to do with the lastButtonState logic.
I was wondering if there’s anything that can implemented in my code that can increase the frequency but keep the duty cycle the same.
Thanks in advance.



#include <TimerOne.h>

int buttonState = 0;
int lastButtonState = 0;
int period = 1000;
int sig_out = 9;
int sig_out2 = 10;
int button =2;

void setup()

pinMode(button,INPUT_PULLUP);
pinMode(sig_out, OUTPUT);
//pinMode(sig_out2, OUTPUT);
Timer1.initialize(1000);
Timer1.pwm(sig_out,507,period);
Serial.print(period);
//Timer1.pwm(sig_out2,456,900);

void loop()
buttonState = digitalRead(button);
if (buttonState != lastButtonState)
lastButtonState = buttonState;
if (buttonState == 1)
period -= 10;
Timer1.setPeriod(period);





Schematic










share|improve this question









$endgroup$













  • $begingroup$
    I don't see any debounce for the switch. This could be happening because of that. Try using debounce and see if it helps
    $endgroup$
    – Prateek Dhanuka
    9 hours ago













3












3








3





$begingroup$


I’m currently trying to increase the frequency from a pin from an Arduino using PWM. I’m using the Timer1 library as it has functions to output signals.



I connected a push button to my Arduino and this is meant to increase the frequency of the pin once pressed . But every time the button is pressed, the duty cycle is also changed. Also the frequency changes multiple when the button is pressed. I think that has something to do with the lastButtonState logic.
I was wondering if there’s anything that can implemented in my code that can increase the frequency but keep the duty cycle the same.
Thanks in advance.



#include <TimerOne.h>

int buttonState = 0;
int lastButtonState = 0;
int period = 1000;
int sig_out = 9;
int sig_out2 = 10;
int button =2;

void setup()

pinMode(button,INPUT_PULLUP);
pinMode(sig_out, OUTPUT);
//pinMode(sig_out2, OUTPUT);
Timer1.initialize(1000);
Timer1.pwm(sig_out,507,period);
Serial.print(period);
//Timer1.pwm(sig_out2,456,900);

void loop()
buttonState = digitalRead(button);
if (buttonState != lastButtonState)
lastButtonState = buttonState;
if (buttonState == 1)
period -= 10;
Timer1.setPeriod(period);





Schematic










share|improve this question









$endgroup$




I’m currently trying to increase the frequency from a pin from an Arduino using PWM. I’m using the Timer1 library as it has functions to output signals.



I connected a push button to my Arduino and this is meant to increase the frequency of the pin once pressed . But every time the button is pressed, the duty cycle is also changed. Also the frequency changes multiple when the button is pressed. I think that has something to do with the lastButtonState logic.
I was wondering if there’s anything that can implemented in my code that can increase the frequency but keep the duty cycle the same.
Thanks in advance.



#include <TimerOne.h>

int buttonState = 0;
int lastButtonState = 0;
int period = 1000;
int sig_out = 9;
int sig_out2 = 10;
int button =2;

void setup()

pinMode(button,INPUT_PULLUP);
pinMode(sig_out, OUTPUT);
//pinMode(sig_out2, OUTPUT);
Timer1.initialize(1000);
Timer1.pwm(sig_out,507,period);
Serial.print(period);
//Timer1.pwm(sig_out2,456,900);

void loop()
buttonState = digitalRead(button);
if (buttonState != lastButtonState)
lastButtonState = buttonState;
if (buttonState == 1)
period -= 10;
Timer1.setPeriod(period);





Schematic







arduino pwm frequency button






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 9 hours ago









NeamusNeamus

1848 bronze badges




1848 bronze badges














  • $begingroup$
    I don't see any debounce for the switch. This could be happening because of that. Try using debounce and see if it helps
    $endgroup$
    – Prateek Dhanuka
    9 hours ago
















  • $begingroup$
    I don't see any debounce for the switch. This could be happening because of that. Try using debounce and see if it helps
    $endgroup$
    – Prateek Dhanuka
    9 hours ago















$begingroup$
I don't see any debounce for the switch. This could be happening because of that. Try using debounce and see if it helps
$endgroup$
– Prateek Dhanuka
9 hours ago




$begingroup$
I don't see any debounce for the switch. This could be happening because of that. Try using debounce and see if it helps
$endgroup$
– Prateek Dhanuka
9 hours ago










2 Answers
2






active

oldest

votes


















4












$begingroup$

If you look at the Timer1.pwm() function call you will see that you are providing two timing parameters: the duty factor and the period. Both of these are specified as an integer number of clock cycles...the duty factor is not given as a percentage of the period.



So, when you change the period the output pin stays high for the same number of clock cycles that you specified in Timer1.pwm(), which effectively changes the duty factor. If you want to keep the duty factor constant then you need to call Timer1.pwm every time you change the period and give it a newly calculated value for the second parameter.






share|improve this answer









$endgroup$














  • $begingroup$
    I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
    $endgroup$
    – Neamus
    8 hours ago


















1












$begingroup$

Timer1.setPeriod(period); 
\type under this line

while (buttonState);
delay(50);


and to prevent arc formation wait 50 milliseconds






share|improve this answer










New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





$endgroup$














  • $begingroup$
    Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
    $endgroup$
    – marcelm
    7 hours ago













Your Answer






StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "135"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f453604%2farduino-duty-cycle-changing-when-frequency-is-increasing%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

If you look at the Timer1.pwm() function call you will see that you are providing two timing parameters: the duty factor and the period. Both of these are specified as an integer number of clock cycles...the duty factor is not given as a percentage of the period.



So, when you change the period the output pin stays high for the same number of clock cycles that you specified in Timer1.pwm(), which effectively changes the duty factor. If you want to keep the duty factor constant then you need to call Timer1.pwm every time you change the period and give it a newly calculated value for the second parameter.






share|improve this answer









$endgroup$














  • $begingroup$
    I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
    $endgroup$
    – Neamus
    8 hours ago















4












$begingroup$

If you look at the Timer1.pwm() function call you will see that you are providing two timing parameters: the duty factor and the period. Both of these are specified as an integer number of clock cycles...the duty factor is not given as a percentage of the period.



So, when you change the period the output pin stays high for the same number of clock cycles that you specified in Timer1.pwm(), which effectively changes the duty factor. If you want to keep the duty factor constant then you need to call Timer1.pwm every time you change the period and give it a newly calculated value for the second parameter.






share|improve this answer









$endgroup$














  • $begingroup$
    I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
    $endgroup$
    – Neamus
    8 hours ago













4












4








4





$begingroup$

If you look at the Timer1.pwm() function call you will see that you are providing two timing parameters: the duty factor and the period. Both of these are specified as an integer number of clock cycles...the duty factor is not given as a percentage of the period.



So, when you change the period the output pin stays high for the same number of clock cycles that you specified in Timer1.pwm(), which effectively changes the duty factor. If you want to keep the duty factor constant then you need to call Timer1.pwm every time you change the period and give it a newly calculated value for the second parameter.






share|improve this answer









$endgroup$



If you look at the Timer1.pwm() function call you will see that you are providing two timing parameters: the duty factor and the period. Both of these are specified as an integer number of clock cycles...the duty factor is not given as a percentage of the period.



So, when you change the period the output pin stays high for the same number of clock cycles that you specified in Timer1.pwm(), which effectively changes the duty factor. If you want to keep the duty factor constant then you need to call Timer1.pwm every time you change the period and give it a newly calculated value for the second parameter.







share|improve this answer












share|improve this answer



share|improve this answer










answered 8 hours ago









Elliot AldersonElliot Alderson

11.6k2 gold badges12 silver badges25 bronze badges




11.6k2 gold badges12 silver badges25 bronze badges














  • $begingroup$
    I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
    $endgroup$
    – Neamus
    8 hours ago
















  • $begingroup$
    I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
    $endgroup$
    – Neamus
    8 hours ago















$begingroup$
I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
$endgroup$
– Neamus
8 hours ago




$begingroup$
I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
$endgroup$
– Neamus
8 hours ago













1












$begingroup$

Timer1.setPeriod(period); 
\type under this line

while (buttonState);
delay(50);


and to prevent arc formation wait 50 milliseconds






share|improve this answer










New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





$endgroup$














  • $begingroup$
    Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
    $endgroup$
    – marcelm
    7 hours ago















1












$begingroup$

Timer1.setPeriod(period); 
\type under this line

while (buttonState);
delay(50);


and to prevent arc formation wait 50 milliseconds






share|improve this answer










New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





$endgroup$














  • $begingroup$
    Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
    $endgroup$
    – marcelm
    7 hours ago













1












1








1





$begingroup$

Timer1.setPeriod(period); 
\type under this line

while (buttonState);
delay(50);


and to prevent arc formation wait 50 milliseconds






share|improve this answer










New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





$endgroup$



Timer1.setPeriod(period); 
\type under this line

while (buttonState);
delay(50);


and to prevent arc formation wait 50 milliseconds







share|improve this answer










New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this answer



share|improve this answer








edited 8 hours ago









Dmitry Grigoryev

19.5k2 gold badges30 silver badges78 bronze badges




19.5k2 gold badges30 silver badges78 bronze badges






New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








answered 9 hours ago









Mahmut BOSTANMahmut BOSTAN

192 bronze badges




192 bronze badges




New contributor



Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Mahmut BOSTAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • $begingroup$
    Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
    $endgroup$
    – marcelm
    7 hours ago
















  • $begingroup$
    Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
    $endgroup$
    – marcelm
    7 hours ago















$begingroup$
Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
$endgroup$
– marcelm
7 hours ago




$begingroup$
Welcome to EE.SE, and thanks for answering a question! Could you perhaps elaborate a little bit on what your solution is trying to achieve, and how?
$endgroup$
– marcelm
7 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Electrical Engineering Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f453604%2farduino-duty-cycle-changing-when-frequency-is-increasing%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480