{"id":52326,"date":"2026-11-01T10:50:58","date_gmt":"2026-11-01T10:50:58","guid":{"rendered":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/"},"modified":"2026-11-01T10:50:58","modified_gmt":"2026-11-01T10:50:58","slug":"pwm-and-analogwrite","status":"publish","type":"siratek_tutorial","link":"https:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/","title":{"rendered":"Dimming and Speed Control with PWM"},"content":{"rendered":"<body><p><\/p>\n<h2>What this covers<\/h2>\n<p>A digital pin has two states, on and off. Yet <code>analogWrite()<\/code> dims an LED and slows a motor. This explains how, which pins can do it, and the three places it behaves differently from what you would expect.<\/p>\n<h2>Before you begin<\/h2>\n<p>An LED and a 220 ohm resistor on pin 9, wired as in <em>Your First Circuit, an LED and a Resistor<\/em>. Everything here can be seen on that one LED.<\/p>\n<h2>Switching fast instead of switching partly<\/h2>\n<p>The pin never sits at a middle voltage. It switches between 0 V and 5 V, hundreds of times a second, and spends a different proportion of each cycle high.<\/p>\n<p>That proportion is the <strong>duty cycle<\/strong>. At 25 percent the pin is high for a quarter of each cycle; the LED receives a quarter of the energy and looks dim. At 75 percent it looks bright. The switching is far too fast to see, so your eye averages it into steady brightness.<\/p>\n<p><code>analogWrite(pin, value)<\/code> sets the duty cycle, with <code>value<\/code> from 0 to 255.<\/p>\n<table>\n<thead>\n<tr>\n<th><code>analogWrite<\/code> value<\/th>\n<th>Duty cycle<\/th>\n<th>LED looks<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>0 percent, always off<\/td>\n<td>Off<\/td>\n<\/tr>\n<tr>\n<td>64<\/td>\n<td>25 percent<\/td>\n<td>Dim<\/td>\n<\/tr>\n<tr>\n<td>128<\/td>\n<td>50 percent<\/td>\n<td>Half<\/td>\n<\/tr>\n<tr>\n<td>191<\/td>\n<td>75 percent<\/td>\n<td>Bright<\/td>\n<\/tr>\n<tr>\n<td>255<\/td>\n<td>100 percent, always on<\/td>\n<td>Full<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The name is misleading. It is not analog output, and the Uno cannot produce a true varying voltage. It produces a square wave whose <em>average<\/em> is what you asked for.<\/p>\n<figure class=\"siratek-tutorial-figure\"><img decoding=\"async\" src=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/pwm-and-analogwrite-duty-cycle.svg\" alt=\"Four square wave traces at 0, 25, 50 and 100 percent duty cycle\" loading=\"lazy\"><figcaption>The pin is always fully on or fully off. Only the proportion changes.<\/figcaption><\/figure>\n<h2>Which pins<\/h2>\n<p>On an Arduino Uno, six pins only: <strong>3, 5, 6, 9, 10 and 11<\/strong>. They are marked with a tilde on the board.<\/p>\n<p><code>analogWrite()<\/code> on any other pin does not produce an error. It treats anything from 128 upwards as fully on and anything below as off, which looks like a broken fade rather than a mistake. If your dimming only ever gives two brightnesses, this is why.<\/p>\n<h2>Three things that surprise people<\/h2>\n<h3>The frequency is not the same on every pin<\/h3>\n<p>Pins 5 and 6 switch at roughly 980 Hz. The other four switch at roughly 490 Hz. Both are far too fast to see on an LED, but they matter for a motor, which can whine audibly at the lower rate, and for anything that filters or measures the signal.<\/p>\n<h3>Pins 5 and 6 do not quite reach zero<\/h3>\n<p>Those two share a timer with the Arduino core\u2019s own timekeeping, the one <code>millis()<\/code> and <code>delay()<\/code> depend on. The interaction leaves them slightly brighter than expected at very low values, and <code>analogWrite(5, 0)<\/code> may not be completely dark.<\/p>\n<p>If you need a pin to go genuinely off, use <code>digitalWrite(pin, LOW)<\/code> for that case, or put the LED on 3, 9, 10 or 11.<\/p>\n<h3><code>tone()<\/code> takes pins 3 and 11 away<\/h3>\n<p><code>tone()<\/code> claims the timer that drives PWM on pins 3 and 11. While a tone is playing, <code>analogWrite()<\/code> on those two does nothing useful, silently. A project that makes sounds and dims lights should put the lights on 5, 6, 9 or 10.<\/p>\n<h2>Minimal working example<\/h2>\n<pre><code>\/\/ Siratek: what a duty cycle looks like.\n\/\/ Steps an LED through five brightness levels, naming each one.\n\nconst int ledPin = 9;\n\nconst int levels[] = { 0, 64, 128, 191, 255 };\nconst int percent[] = { 0, 25, 50, 75, 100 };\nconst int levelCount = 5;\n\nvoid setup() {\n  pinMode(ledPin, OUTPUT);\n  Serial.begin(9600);\n  Serial.println(\"Siratek: stepping through duty cycles.\");\n}\n\nvoid loop() {\n  for (int i = 0; i &lt; levelCount; i++) {\n    analogWrite(ledPin, levels[i]);\n\n    Serial.print(\"value=\");\n    Serial.print(levels[i]);\n    Serial.print(\"  duty=\");\n    Serial.print(percent[i]);\n    Serial.println(\"%\");\n\n    delay(1200);\n  }\n}<\/code><\/pre>\n<p>Stepping through fixed levels shows the relationship far better than a smooth fade, because you can hold each one and compare.<\/p>\n<h3>Driving it from a potentiometer<\/h3>\n<p><code>analogRead()<\/code> gives 0 to 1023 and <code>analogWrite()<\/code> wants 0 to 255, so the value has to be rescaled.<\/p>\n<pre><code>const int potPin = A0;\nconst int ledPin = 9;\n\nvoid loop() {\n  int reading = analogRead(potPin);\n  analogWrite(ledPin, map(reading, 0, 1023, 0, 255));\n  delay(20);\n}<\/code><\/pre>\n<p>Dividing by four would also work, and is what <code>map()<\/code> effectively does here. Use <code>map()<\/code> anyway: it states the intent, and it keeps working when one of the ranges changes.<\/p>\n<h2>What PWM can and cannot drive<\/h2>\n<p>PWM suits anything that responds to average power: LED brightness, small motor speed, heater output, a servo\u2019s control signal.<\/p>\n<p>It does not suit anything that needs a steady voltage, such as a reference for a sensor or an audio level. For those you need a real digital-to-analog converter, or a filter to smooth the square wave into a level.<\/p>\n<p>And the pin\u2019s current limit has not changed. A motor still needs a transistor or a driver module between the pin and the load, whatever the duty cycle. PWM controls <em>when<\/em> the current flows, not how much the pin can supply.<\/p>\n<h2>Check yourself<\/h2>\n<ol>\n<li><code>analogWrite(7, 128)<\/code> on an Uno. What does the LED do?<\/li>\n<li>Why might a motor whine on pin 9 but not on pin 5?<\/li>\n<li>Your sketch plays a tone and dims an LED on pin 11. The LED stops dimming. Why?<\/li>\n<li>Does <code>analogWrite(9, 128)<\/code> put 2.5 V on pin 9?<\/li>\n<\/ol>\n<p>Answers in order: pin 7 has no PWM, so 128 and above is fully on and the LED is simply lit. Pin 9 switches at about 490 Hz, which is within hearing; pin 5 runs at about 980 Hz. <code>tone()<\/code> has taken the timer that drives pin 11. No, the pin is still switching between 0 V and 5 V; 2.5 V is only the average.<\/p>\n<h2>Troubleshooting<\/h2>\n<table>\n<thead>\n<tr>\n<th>Symptom<\/th>\n<th>Likely cause<\/th>\n<th>Check or fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Only two brightnesses<\/td>\n<td>Not a PWM pin<\/td>\n<td>Use 3, 5, 6, 9, 10 or 11<\/td>\n<\/tr>\n<tr>\n<td>Will not go fully off on pin 5 or 6<\/td>\n<td>Timer shared with the core timing<\/td>\n<td>Use <code>digitalWrite()<\/code> for off, or another pin<\/td>\n<\/tr>\n<tr>\n<td>Dimming stopped when sound started<\/td>\n<td><code>tone()<\/code> holds pins 3 and 11<\/td>\n<td>Move the output to 5, 6, 9 or 10<\/td>\n<\/tr>\n<tr>\n<td>Motor whines<\/td>\n<td>490 Hz switching is audible<\/td>\n<td>Try pin 5 or 6<\/td>\n<\/tr>\n<tr>\n<td>Fade looks uneven<\/td>\n<td>Eye response, not a fault<\/td>\n<td>Expected; curve the values if it matters<\/td>\n<\/tr>\n<tr>\n<td>Measured voltage is wrong<\/td>\n<td>A meter averages the square wave<\/td>\n<td>The pin is switching, not holding a level<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Next steps<\/h2>\n<ul>\n<li><strong>Easier first:<\/strong> <em>Your First Circuit, an LED and a Resistor<\/em>.<\/li>\n<li><strong>Next:<\/strong> <em>Timing Without delay()<\/em>, so a fade can run while the rest of the project works.<\/li>\n<li><strong>Later:<\/strong> the servo and motor driver tutorials, which are PWM applied to movement.<\/li>\n<\/ul>\n<h2>Sources and attribution<\/h2>\n<p>All text and code in this tutorial are original Siratek work.<\/p>\n<ul>\n<li>Arduino core documentation and source for <code>analogWrite()<\/code>, the PWM pin list and the timers behind them.<\/li>\n<li>ATmega328P datasheet for the timer behaviour on pins 5 and 6.<\/li>\n<\/ul>\n<p>No third-party tutorial text, diagrams, photographs or code has been reproduced here.<\/p>\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>A digital pin has two states, on and off. Yet analogWrite() dims an LED and slows a motor. This explains how, which pins can do it, and the three places it behaves differently from what you would expect.<\/p>\n","protected":false},"featured_media":0,"template":"","siratek_tutorial_level":[],"class_list":["post-52326","siratek_tutorial","type-siratek_tutorial","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Dimming and Speed Control with PWM - siratek<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dimming and Speed Control with PWM - siratek\" \/>\n<meta property=\"og:description\" content=\"A digital pin has two states, on and off. Yet analogWrite() dims an LED and slows a motor. This explains how, which pins can do it, and the three places it behaves differently from what you would expect.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/\" \/>\n<meta property=\"og:site_name\" content=\"siratek\" \/>\n<meta property=\"og:image\" content=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/pwm-and-analogwrite-duty-cycle.svg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/\",\"url\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/\",\"name\":\"Dimming and Speed Control with PWM - siratek\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/pwm-and-analogwrite-duty-cycle.svg\",\"datePublished\":\"2026-11-01T10:50:58+00:00\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/#primaryimage\",\"url\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/pwm-and-analogwrite-duty-cycle.svg\",\"contentUrl\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/pwm-and-analogwrite-duty-cycle.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/pwm-and-analogwrite\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/siratek.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Dimming and Speed Control with PWM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\",\"url\":\"https:\\\/\\\/siratek.net\\\/\",\"name\":\"siratek\",\"description\":\"Your Best Electronic Retail Shop\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/siratek.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dimming and Speed Control with PWM - siratek","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/","og_locale":"en_US","og_type":"article","og_title":"Dimming and Speed Control with PWM - siratek","og_description":"A digital pin has two states, on and off. Yet analogWrite() dims an LED and slows a motor. This explains how, which pins can do it, and the three places it behaves differently from what you would expect.","og_url":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/","og_site_name":"siratek","og_image":[{"url":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/pwm-and-analogwrite-duty-cycle.svg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/","url":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/","name":"Dimming and Speed Control with PWM - siratek","isPartOf":{"@id":"https:\/\/siratek.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/#primaryimage"},"image":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/#primaryimage"},"thumbnailUrl":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/pwm-and-analogwrite-duty-cycle.svg","datePublished":"2026-11-01T10:50:58+00:00","breadcrumb":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/#primaryimage","url":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/pwm-and-analogwrite-duty-cycle.svg","contentUrl":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/pwm-and-analogwrite-duty-cycle.svg"},{"@type":"BreadcrumbList","@id":"http:\/\/siratek.net\/index.php\/tutorials\/pwm-and-analogwrite\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siratek.net\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"https:\/\/siratek.net\/index.php\/tutorials\/"},{"@type":"ListItem","position":3,"name":"Dimming and Speed Control with PWM"}]},{"@type":"WebSite","@id":"https:\/\/siratek.net\/#website","url":"https:\/\/siratek.net\/","name":"siratek","description":"Your Best Electronic Retail Shop","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siratek.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial"}],"about":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/types\/siratek_tutorial"}],"version-history":[{"count":0,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52326\/revisions"}],"wp:attachment":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/media?parent=52326"}],"wp:term":[{"taxonomy":"siratek_tutorial_level","embeddable":true,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial_level?post=52326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}