Disturbed pmw signal with timerOne

Hi Everyone!

I have a problem when using the TimerOne.h library on a Uno board.
The scetch include an adjustable pwm signal whith low frequency. It works but
for some reason the output is disturbed at various frequencies, see photos. I have removed all unnecessary code and added the basic code.
Replacing the Arduino board didn't matter.
What could be the cause of this?

Regards Wim

#include <TimerOne.h>

unsigned long t=30000,f,k=512;

byte k1,kn,kn1,kn2;
int drive,drive0;

void setup()
{ 
 pinMode(9, OUTPUT);
  pinMode(A2,INPUT);// button at input A2
  pinMode(12,INPUT);// button at input 12 
  pinMode(13,INPUT);// button at input 13
}
void loop()
{
  Timer1.initialize(t); // period    
  Timer1.pwm(9, k); // k - fill factor 0-1023. 
  kn=digitalRead(A2);// button input 6 (- pulse period) 
  kn1=digitalRead(12);// button input 7 (+ pulse period)
  kn2=digitalRead(13);// button input 13 (+ fill factor)

  if(kn==HIGH) // decreasing the period 
    {
      drive++; 
    if(t>14000)
      { 
        t=t-800;  
      }
    else if(t>7000 && t<=14000 )
      { 
        t=t-500; 
      }
    else if(t<=7000)
      {
        t=t-200;
      }
   }
  else
  {
    drive=0;
  }

  if(kn1==HIGH)// adding a period 
    { 
      drive0++;
      if(t>14000)
        { 
          t=t+800;  
        }
      else if(t>7000 && t<=14000 )
        { 
          t=t+500; 
        }
      else if(t<=7000)
        {
          t=t+200;
        }
   }
  else
  {
    drive0=0;
  }
  if(t==0 || t>300000){ // limiting the pulse duration to the minimum 

    t=1;

  }
  
  if(t>200000 && t<300000){ // limiting the pulse duration to the  maximum

    t=200000;

  } 
  f=1000000/t; // calculate the frequency 
  k1=k*100/1024; // calculate% fill factor

  if(kn2==HIGH){// button for adjusting the fill factor


    k=k+16;// step 16 out of 1024 
  }
  if(k==1024){
    k=0;
  }

  delay(100);

}

Please edit your post to add code tags (select code, use "</>" editor button).

What are we supposed to learn from the scope plot?

Please describe the expected behavior of the output, and what it does instead.

Why do you initialize the timer, every pass through loop()? Something like that is usually done in setup().

1 Like

There is only one photo and it doesn't clarify for me what you mean by "disturbed".

Hello,

Thanks for your reply.
I tried to add á recorded movie but that's not possible.
The scope shows a flickering in horizontal direction. You can see the little moving dot bottom left that disturb the signal. In the movie it's very clear...

Hello,

Unfortunately I can't add more than one photo. I'am a new user.

Do you see this effect if you move the Timer1.initialize(t); to setup as suggested?

Do you see this effect if you are not adjusting the frequency or duty cycle?

Problem solved!!

You're right. I changed the code:

#include <TimerOne.h>

unsigned long t=30000,f,k=512;

byte k1,kn,kn1,kn2;
int drive,drive0;

void setup()
{  
  Timer1.initialize(t); // period 
  pinMode(9, OUTPUT);
  pinMode(A2,INPUT);// button at input A2
  pinMode(12,INPUT);// button at input 12 
  pinMode(13,INPUT);// button at input 13
}
void loop()
{
  Timer1.setPeriod(t); // period    
  Timer1.pwm(9, k); // k - fill factor 0-1023. 
  kn=digitalRead(A2);// button input 6 (- pulse period) 
  kn1=digitalRead(12);// button input 7 (+ pulse period)
  kn2=digitalRead(13);// button input 13 (+ fill factor)

  if(kn==HIGH) // decreasing the period 
    {
      drive++; 
    if(t>14000)
      { 
        t=t-800;  
      }
    else if(t>7000 && t<=14000 )
      { 
        t=t-500; 
      }
    else if(t<=7000)
      {
        t=t-200;
      }
   }
  else
  {
    drive=0;
  }

  if(kn1==HIGH)// adding a period 
    { 
      drive0++;
      if(t>14000)
        { 
          t=t+800;  
        }
      else if(t>7000 && t<=14000 )
        { 
          t=t+500; 
        }
      else if(t<=7000)
        {
          t=t+200;
        }
   }
  else
  {
    drive0=0;
  }
  if(t==0 || t>300000){ // limiting the pulse duration to the minimum 

    t=1;

  }
  
  if(t>200000 && t<300000){ // limiting the pulse duration to the  maximum

    t=200000;

  } 
  f=1000000/t; // calculate the frequency 
  k1=k*100/1024; // calculate% fill factor

  if(kn2==HIGH){// button for adjusting the fill factor


    k=k+16;// step 16 out of 1024 
  }
  if(k==1024){
    k=0;
  }

  delay(100);

}

Hello,

I moved the Timer1.initilize(t) to setup as suggested and that was the solution.

Thanks all!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.