element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 Would it be better if all the I/O pins on the micro-controllers had PWM?
  • 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 Not Answered
  • Replies 3 replies
  • Subscribers 417 subscribers
  • Views 320 views
  • Users 0 members are here
Related

Would it be better if all the I/O pins on the micro-controllers had PWM?

the_future
the_future over 12 years ago

Hi my name is Orlando and I am only beginning my journey

in the electronic and robotic world so please don't cuss me for this image

 

I was doing a few projects and I was just wondering what are advantages

and disadvantages of having  Pulse width modulation (PWM)? wouldn't it be

better to all have PWM so that they can all show different duty cycle percentages

(LED breathing effect).

 

e.g

 

#define LED1 3

int delay_breathEffect =5;

 

void setup()

{

  pinMode(LED1, OUTPUT);

}

 

void loop()

{

  for (int c = 0 ; c < 256;  c++)

    {

         analogWrite(LED1, c);

         delay(delay_breathEffect);

     }

  for (int c = 0 ; c < 256;  c++)

    {

         analogWrite(LED1, c);

         delay(delay_breathEffect);

     }

   delay(200);

}

  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 12 years ago +1
    http://arduino.cc/en/Tutorial/PWM http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM On an UNO, the PWM pins (3, 5, 6, 9, 10, or 11) are all available to you for output but there are caveats, for one, there…
  • Robert Peter Oakes
    0 Robert Peter Oakes over 12 years ago

    http://arduino.cc/en/Tutorial/PWM

    http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM

     

    On an UNO, the PWM pins (3, 5, 6, 9, 10, or 11) are all available to you for output but there are caveats, for one, there are only 3 timers shared across them so changing a timer can affect a PWM output frequency for example, there all fundamentally 8 bit resolution, though with some simple code changes you can get a 16 bit PWM if you leverage timer 3.


    Not all outputs support hardware PWM but there is nothing stopping you from running some under software control except it ties up CPU resources to manage it, which is where in a moment I will point out part of your code that could be improved


    PWM is a very handy feature and is great for simulating an Analog output (Hence using the AnalogWrite function to control it). The reasons you may not want to use PWM could be your controlling a relay or other device where a pulse train could actually cause issues in the control system or that you just simply want ON or Off and therefore why waste the additional resources (Hardware) and therefore power consumption to do that


    at the end of the day it is simply down to choice and suitability for the task, that's the beauty of programmable logic, if you want to change something then simply upload new code



    now that comment on your code


    Using Delay in the way you are introduces effectively a stop on all processing while your "Waiting", this is not good if your trying to monitor and control a whole bunch of different things. What is better is to use a variable and soft control the wait times rather than Wait a specific amount of time.


    like this:


    const timerOneDelay = 500 // milliseconds
    const timerTwoDelay = 1000 // milliseconds
    
    
    
    double timerOneWait
    double timerTwoWait
    int breath1 = 0;
    int breath2 = 0;
    int breathCount1 = 1;
    int breathCount2 = 1;
    
    
    
    setup()
    {
    timerOneWait = mills() + timerOneDelay;
    timerTwoWait = mills() + timerTwoDelay;
    //... Other Setup stuff
    }
    
    
    loop()
    {
         //do stuff....
         breathLEDs(); // process breathing of LEDs but they will be very quick as no delays in returning
    
    
    }
    
    
    void breathLEDs()
    {
         // now these work independently and you can do other stuff without delay
         if (timerOneWait < mills())
         {
              breath1+=breathCount2;
              if(breath1 == 255) breatCount1 = -1; // now decrement
              else if(breath1 == 0 )breathCount1 = 1; // now increment
    
    
              analogWrite(LED1, breath1+breathCount );     
              timerOneWait = mills() + timerOneDelay; //reset next breath
         }
    
    
         // now these work independently and you can do other stuff without delay
    
         if (timerTwoWait < mills())
         {
              breath2+=breathCount2;
              if(breath2 == 255) breatCount2 = -1; // now decrement
              else if(breath2 == 0 )breathCount2 = 1; // now increment
    
    
               analogWrite(LED1, breath2 );         
               timerTwoWait = mills() + timerTwoDelay;
         }
    }


    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • the_future
    0 the_future over 12 years ago in reply to Robert Peter Oakes

    Thanks Peter I have learned a lot from this.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 12 years ago in reply to the_future

    your welcome, let us know how it goes

    • Cancel
    • Vote Up 0 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 © 2026 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.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube