Arduino- Duty Cycle Changing When Frequency Is IncreasingPractical issue with PIC PWMproblem of transmitting...

Can pay be witheld for hours cleaning up after closing time?

Do ability scores have any effect on casting Wish spell

How would one country purchase another?

Would it be possible to have a GMO that produces chocolate?

If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?

Is it possible to create a golf ball sized star?

Is there a limit on how long the casting (speaking aloud part of the spell) of Wish can be?

Is it safe to remove the bottom chords of a series of garage roof trusses?

How to write triplets in 4/4 time without using a 3 on top of the notes all the time

Was Switzerland really impossible to invade during WW2?

When translating the law, who ensures that the wording does not change the meaning of the law?

Three Singles in Three Clubs

How is "sein" conjugated in this sub-sentence?

Is there any practical application for performing a double Fourier transform? ...or an inverse Fourier transform on a time-domain input?

Verb form to finish someone else's sentence?

How much code would a codegolf golf if a codegolf could golf code?

Co-author responds to email by mistake cc'ing the EiC

Why were movies shot on film shot at 24 frames per second?

What magic extends life or grants immortality

What to say to a student who has failed?

The teacher logged me in as administrator for doing a short task, is the whole system now compromised?

How to compare two different formulations of a problem?

Is there such a thing as too inconvenient?

Fried gnocchi with spinach, bacon, cream sauce in a single pan



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
    2 days 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
    2 days 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 2 days ago









NeamusNeamus

1928 bronze badges




1928 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
    2 days 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
    2 days 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
2 days 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
2 days ago










1 Answer
1






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
    2 days 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

























1 Answer
1






active

oldest

votes








1 Answer
1






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
    2 days 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
    2 days 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 2 days 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
    2 days ago


















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
















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




$begingroup$
I added the Timer1.pwm() below the Timer1.setPeriod(period); and the duty cycle stays at 50%.
$endgroup$
– Neamus
2 days 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

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...