element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum PWM Timing Issue
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 6 replies
  • Answers 1 answer
  • Subscribers 392 subscribers
  • Views 836 views
  • Users 0 members are here
  • pwm
  • arduino sketch
Related

PWM Timing Issue

tdmcdaniel
tdmcdaniel over 7 years ago

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 
}

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 7 years ago +2 verified
    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…
  • mcb1
    mcb1 over 7 years ago in reply to tdmcdaniel +1
    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…
  • tdmcdaniel
    tdmcdaniel over 7 years ago in reply to mcb1 +1
    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…
Parents
  • mcb1
    0 mcb1 over 7 years ago

    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

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • tdmcdaniel
    0 tdmcdaniel over 7 years ago in reply to mcb1

    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
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • tdmcdaniel
    0 tdmcdaniel over 7 years ago in reply to mcb1

    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
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • mcb1
    0 mcb1 over 7 years ago in reply to tdmcdaniel
    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

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • tdmcdaniel
    0 tdmcdaniel over 7 years ago in reply to mcb1

    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.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 7 years ago in reply to tdmcdaniel
    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

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube