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;
$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);

arduino pwm frequency button
$endgroup$
add a comment |
$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);

arduino pwm frequency button
$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
add a comment |
$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);

arduino pwm frequency button
$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);

arduino pwm frequency button
arduino pwm frequency button
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
add a comment |
$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
add a comment |
2 Answers
2
active
oldest
votes
$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.
$endgroup$
$begingroup$
I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
$endgroup$
– Neamus
8 hours ago
add a comment |
$begingroup$
Timer1.setPeriod(period);
\type under this line
while (buttonState);
delay(50);
and to prevent arc formation wait 50 milliseconds
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
$endgroup$
$begingroup$
I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
$endgroup$
– Neamus
8 hours ago
add a comment |
$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.
$endgroup$
$begingroup$
I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
$endgroup$
– Neamus
8 hours ago
add a comment |
$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.
$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.
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
add a comment |
$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
add a comment |
$begingroup$
Timer1.setPeriod(period);
\type under this line
while (buttonState);
delay(50);
and to prevent arc formation wait 50 milliseconds
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
add a comment |
$begingroup$
Timer1.setPeriod(period);
\type under this line
while (buttonState);
delay(50);
and to prevent arc formation wait 50 milliseconds
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
add a comment |
$begingroup$
Timer1.setPeriod(period);
\type under this line
while (buttonState);
delay(50);
and to prevent arc formation wait 50 milliseconds
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
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.
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
add a comment |
$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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$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