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
  • beacon_dave
    0 beacon_dave over 7 years ago

    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.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • beacon_dave
    0 beacon_dave over 7 years ago

    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.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
No Data
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