{"id":94,"date":"2019-10-23T05:31:15","date_gmt":"2019-10-23T05:31:15","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=94"},"modified":"2019-10-23T05:32:07","modified_gmt":"2019-10-23T05:32:07","slug":"unit-13-fading-leds-using-the-analog-output","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/","title":{"rendered":"Unit 13 \u2013 Fading LEDs using the Analog Output"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>You already know how to switch on and off LEDs. Now, we want to try if we can also fade it.<\/p><\/blockquote>\n\n\n\n<p>The light switch of your room can be switched on and off. Everything what can take exactly two conditions is called&nbsp;<em>digital<\/em>&nbsp;by the Arduino. You learned the commands&nbsp;<em>digitalWrite()<\/em>&nbsp;and&nbsp;<em>digitalRead()<\/em>. Sometimes, lamps have not a switch, but a knob or fader to fade it. With it the luminosity can be adjusted. So it not only knows on and off but conditions in between. Arduino calls it&nbsp;<em>analog<\/em>.<\/p>\n\n\n\n<p>To fade a LED we can simply use the command&nbsp;<em>analogWrite()<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>analogWrite(ledPin, luminosity);<\/code><\/pre>\n\n\n\n<p>Again, we send two arguments. Similar to&nbsp;<em>digitalWrite()<\/em>&nbsp;the first argument defines the pin number. But this time the second argument is not just HIGH and LOW but a number between 0 and 255. That means we can adjust the luminosity of a LED in 256 steps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1348\" height=\"1053\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-button-if-statement.jpg\" alt=\"Arduino LED Fading Analog Output Circuit\" class=\"wp-image-84\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-button-if-statement.jpg 1348w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-button-if-statement-300x234.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-button-if-statement-768x600.jpg 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-button-if-statement-1024x800.jpg 1024w\" sizes=\"auto, (max-width: 1348px) 100vw, 1348px\" \/><\/figure>\n\n\n\n<p>Yes, you already know <a href=\"https:\/\/starthardware.org\/en\/2019\/10\/22\/unit-8-its-alive\/\">this circuit<\/a>. It is a LED connected through a resistor to the GND and directly to pin 9.<\/p>\n\n\n\n<p>This could be the program code to test it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\n\nvoid setup(){\n\n}\n\nvoid loop(){\n  analogWrite(ledPin, 200);\n}<\/code><\/pre>\n\n\n\n<p>Did you noticed there is nothing in the&nbsp;<em>setup()<\/em>? When we use the analog output we don\u2019t have declare something like&nbsp;<em>pinMode()<\/em>&nbsp;like we would have using the digital output. Cool. Now, just try some other values like instead of 200 (e.g. 50,250,0,100). Don\u2019t forget to upload it \ud83d\ude09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Expert Knowledge: PWM<\/h3>\n\n\n\n<p>To fade LEDs Arduino uses a trick. Actually, we would have to reduce voltage but Arduino is not capable to do so. But Arduino is very good in rapid switching pins on and off. The relation between on and off controls our perception of the luminosity. This trick is called pulse width modulation (PWM) and is very common in micro-controller technology. Arduino can\u2019t do PWM on every pin. PWM pins are labeled with a wavy line (pins 3,5,6,9,10 and 11)..<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fading using the for-loop<\/h3>\n\n\n\n<p>We discussed the for-Loop in Unit 10. Let\u2019s use it to fade the LED automatically. Using a for-loop we can change a LED every time it is repeated. I say we use it to count the luminosity. The variable should start at 0 and count up to 255. That looks this way:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>for (int luminosity=0; luminosity &lt;=255; luminosity = luminosity +1){<\/code><\/pre>\n\n\n\n<p>If you are a lazy coder (like me) you can write instead of<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>luminosity = luminosity + 1<\/code><\/pre>\n\n\n\n<p>even that<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>luminosity++<\/code><\/pre>\n\n\n\n<p>It also counts the variable up by 1. To make it more clear now, I stay with the longer version. The code looks like that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\n\nvoid setup(){\n\n}\n\nvoid loop(){\n  for (int luminosity =1; luminosity &lt;= 255; luminosity = luminosity +1){\n    analogWrite(ledPin, luminosity);\n    delay(20);\n  }\n}<\/code><\/pre>\n\n\n\n<p>We will need a little delay to perceive a change. Upload the code to your Arduino.<\/p>\n\n\n\n<p>Now the LED slowly turns on. What do we have to do to slowly turn it off? We would need a second for-loop counting a luminosity variable from 255 down to 0 reducing itself by on every time the for-loop is repeated. The for-loop should be repeated as long as the variable luminosity is greater than 0. Hm \u2026 that should be possible, no? Try it by your own. You can do it!<\/p>\n\n\n\n<p>Compare your approach with this solution:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int ledPin = 9;\n\nvoid setup(){\n\n}\n\nvoid loop(){\n  for (int luminosity =1; luminosity &lt;= 255; luminosity = luminosity +1){\n    analogWrite(ledPin, luminosity);\n    delay(20);\n  }\n  for (int luminosity =255; luminosity >= 0; luminosity = luminosity -1){\n    analogWrite(ledPin, luminosity);\n    delay(20);\n  }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You already know how to switch on and off LEDs. Now, we want to try if we can also fade it. The light switch of your room can be switched on and off. Everything what can take exactly two conditions is called&nbsp;digital&nbsp;by the Arduino. You learned the commands&nbsp;digitalWrite()&nbsp;and&nbsp;digitalRead(). Sometimes, lamps have not a switch, but&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Unit 13 \u2013 Fading LEDs using the Analog Output<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":95,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unit 13 \u2013 Fading LEDs using the Analog Output - StartHardware - Tutorials for Arduino<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unit 13 \u2013 Fading LEDs using the Analog Output - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"You already know how to switch on and off LEDs. Now, we want to try if we can also fade it. The light switch of your room can be switched on and off. Everything what can take exactly two conditions is called&nbsp;digital&nbsp;by the Arduino. You learned the commands&nbsp;digitalWrite()&nbsp;and&nbsp;digitalRead(). Sometimes, lamps have not a switch, but&hellip;&nbsp;Read More &raquo;Unit 13 \u2013 Fading LEDs using the Analog Output\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-23T05:31:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-23T05:32:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-fading-led-pwm.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1361\" \/>\n\t<meta property=\"og:image:height\" content=\"766\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stefan Hermann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/starthardware.org\/en\/#website\",\"url\":\"https:\/\/starthardware.org\/en\/\",\"name\":\"StartHardware - Tutorials for Arduino\",\"description\":\"Arduino, Electronics, Fun\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/starthardware.org\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-fading-led-pwm.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-fading-led-pwm.jpg\",\"width\":1361,\"height\":766,\"caption\":\"Arduino Fading LED PWM Title\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/\",\"name\":\"Unit 13 \u2013 Fading LEDs using the Analog Output - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#primaryimage\"},\"datePublished\":\"2019-10-23T05:31:15+00:00\",\"dateModified\":\"2019-10-23T05:32:07+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unit 13 \u2013 Fading LEDs using the Analog Output\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\",\"name\":\"Stefan Hermann\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"caption\":\"Stefan Hermann\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unit 13 \u2013 Fading LEDs using the Analog Output - StartHardware - Tutorials for Arduino","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":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/","og_locale":"en_US","og_type":"article","og_title":"Unit 13 \u2013 Fading LEDs using the Analog Output - StartHardware - Tutorials for Arduino","og_description":"You already know how to switch on and off LEDs. Now, we want to try if we can also fade it. The light switch of your room can be switched on and off. Everything what can take exactly two conditions is called&nbsp;digital&nbsp;by the Arduino. You learned the commands&nbsp;digitalWrite()&nbsp;and&nbsp;digitalRead(). Sometimes, lamps have not a switch, but&hellip;&nbsp;Read More &raquo;Unit 13 \u2013 Fading LEDs using the Analog Output","og_url":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2019-10-23T05:31:15+00:00","article_modified_time":"2019-10-23T05:32:07+00:00","og_image":[{"width":1361,"height":766,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-fading-led-pwm.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Hermann","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/starthardware.org\/en\/#website","url":"https:\/\/starthardware.org\/en\/","name":"StartHardware - Tutorials for Arduino","description":"Arduino, Electronics, Fun","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/starthardware.org\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-fading-led-pwm.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/10\/arduino-fading-led-pwm.jpg","width":1361,"height":766,"caption":"Arduino Fading LED PWM Title"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#webpage","url":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/","name":"Unit 13 \u2013 Fading LEDs using the Analog Output - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#primaryimage"},"datePublished":"2019-10-23T05:31:15+00:00","dateModified":"2019-10-23T05:32:07+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/unit-13-fading-leds-using-the-analog-output\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Unit 13 \u2013 Fading LEDs using the Analog Output"}]},{"@type":"Person","@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59","name":"Stefan Hermann","image":{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","caption":"Stefan Hermann"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":2,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/94\/revisions\/97"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/95"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}