6 Replies Latest reply on Dec 31, 2017 11:30 AM by mcb1

    PWM Timing Issue

    tdmcdaniel

      I am going through a series of tutorials to get familiar with the Arduino, and I am having trouble understanding why something isn't working the way that I expected it to.  The following two blocks of code do the same thing, but I tried it both ways to see if there was any change, and the results were essentially identical (which the exception of the delay(800)).  The problem that I am experiencing is that the LED dims far slower than it brightens.  In fact the 0-255 loop seems to be happening in about a second, whereas the 255-0 loop is taking closer to five seconds.  There does not seem to be a reason for this in the code, so I am at a loss.  Any help or an explanation would be greatly appreciated!

       

      #define ledPin 9
      
      void setup() {
        // put your setup code here, to run once:
        pinMode(ledPin, OUTPUT);
      }
      
      void loop() {
        // put your main code here, to run repeatedly:
        for(int a = 0; a <= 255; a++) {
          analogWrite(ledPin, a);
          delay(8);
        }
        for (int a = 255; a >= 0; a--) {
          analogWrite(ledPin, a);
          delay(8);
        }
      }

       

      And

      //Controlling LED By PWM
      //The LED lights up gradually,and then goes out gradually,repeatedly
      //Email:support@sunfounder.com
      //Website:www.sunfounder.com
      //2015.5.7
      /**************************************************************/
      const int ledPin = 9; // the pin that the LED is attached to pin 9
      void setup ()
      {
      pinMode(ledPin, OUTPUT); // declare pin 9 to be an output
      }
      
      void loop()
      {
      for (int a=0; a<=255;a++) //loop from 0 to 255
      {
      analogWrite(ledPin, a); // set the brightness of pin 9:
      delay(8); //wait for 8 ms 
      }
      for (int a=255; a>=0;a--) //loop from 255 down to 0
      {
      analogWrite(ledPin, a); // set the brightness of pin 9:
      delay(8); //wait for 8 ms 
      }
      delay(800); //wait for 800 ms 
      }
        • Re: PWM Timing Issue
          mcb1

          Both loops should take just over 2 seconds to complete.

          255 * 8 = 2040 mS = 2.04 secs

           

           

          LED's are not linear, and I suspect what you are seeing is the brightness increasing in the first loop until you reach what appears to be the maximum brightness, but the PWM is not 255.

          It then goes into the second loop and again you're not seeing anything until it reaches a much lower point.

           

          You could try adding a pause in-between the two loops and show the LED in the opposite state before the loop starts.

          //Controlling LED By PWM
          //The LED lights up gradually,and then goes out gradually,repeatedly
          //Email:support@sunfounder.com
          //Website:www.sunfounder.com
          //2015.5.7
          /**************************************************************/
          constint ledPin = 9; // the pin that the LED is attached to pin 9
          void setup ()  
          {  
          pinMode(ledPin, OUTPUT); // declare pin 9 to be an output
          }  
          void loop()  
          {  
          for (int a=0; a<=255;a++) //loop from 0 to 255
          {  
          analogWrite(ledPin, a); // set the brightness of pin 9:
          delay(8); //wait for 8 ms 
          } 
          delay(500); //wait for 500 ms 
          analogWrite(ledPin, 0); //Turn OFF the led
          delay(500); //wait for 500 ms to show the LED is off
          for (int a=255; a>=0;a--) //loop from 255 down to 0
          {  
          analogWrite(ledPin, a); // set the brightness of pin 9:
          delay(8); //wait for 8 ms 
          }  
          delay(500); //wait for 500 ms 
          analogWrite(ledPin, 255); //Turn ON the led
          delay(500); //wait for 500 ms to show the LED is on
          }  
          
          

           

          Mark

          3 of 3 people found this helpful
            • Re: PWM Timing Issue
              tdmcdaniel

              The code sample that you gave is actually setting the pin to off when it should be on, and on when it should be off.  I made changes to reflect what (I think) you were trying to get the code to do, but the problem of the LED getting bright too fast and dimming too slow remains.  The brightening of the LED is still much faster than the dimming.  I understand the math behind how long each loop should take to complete, but the reality is that this is not the case.

               

              //Controlling LED By PWM  
              //The LED lights up gradually,and then goes out gradually,repeatedly  
              //Email:support@sunfounder.com  
              //Website:www.sunfounder.com  
              //2015.5.7  
              /**************************************************************/  
              const int ledPin = 9; // the pin that the LED is attached to pin 9  
              void setup () {    
              pinMode(ledPin, OUTPUT); // declare pin 9 to be an output  
              }
              void loop() {
              for (int a=0; a<=255;a++) //loop from 0 to 255
              {
              analogWrite(ledPin, a); // set the brightness of pin 9:
              delay(8); //wait for 8 ms
              }
              analogWrite(ledPin, 255); //Turn ON the led 
              delay(500); //wait for 500 ms
              for (int a=255; a>=0;a--) //loop from 255 down to 0  
              {
              analogWrite(ledPin, a); // set the brightness of pin 9:  
              delay(8); //wait for 8 ms   
              }
              analogWrite(ledPin, 0); //Turn OFF the led
              delay(500); //wait for 500 ms
              }
                • Re: PWM Timing Issue
                  mcb1
                  setting the pin to off when it should be on, and on when it should be off

                  I deliberately did that to show the difference when the loop stopped and started again.

                   

                  I would challenge anyone to see the difference in brightness of a LED between 155 and 255.

                   

                  What happens when you simply comment out the second loop.?

                  (Highlight the lines and choose Comment)

                   

                   

                  Mark

                    • Re: PWM Timing Issue
                      tdmcdaniel

                      I suppose that I should have paid more attention to your first answer.  I guess I just perceived the brightening loop to be happening faster than the dimming loop.  After timing it (as best I could), it does appear that both of the loops are taking the same (correct) length of time to complete.  Thank you for your help, and I apologize for being so hard-headed.

                        • Re: PWM Timing Issue
                          mcb1
                          I just perceived the brightening loop to be happening faster than the dimming loop.

                          And it's not hard to see why you would think that ... until you add something.

                           


                          being so hard-headed

                           

                          So long as you learnt something, no problems.

                           

                          Mark

                  • Re: PWM Timing Issue
                    beacon_dave

                    you could perhaps use the millis() function to help diagnose this more accurately

                    https://www.arduino.cc/reference/en/language/functions/time/millis/

                     

                    store the value returned by millis() to a variable at the start of the loop and subtract it from the value returned by millis() at the end of the loop, and send it to the serial console. this should give you a fairly accurate indication of the number of milliseconds the loop took to run. do this for both loops and compare the results.

                    4 of 4 people found this helpful