








{"id":77,"date":"2020-10-12T20:19:53","date_gmt":"2020-10-12T20:19:53","guid":{"rendered":"https:\/\/osoyoo.info\/?p=77"},"modified":"2020-10-13T22:05:47","modified_gmt":"2020-10-13T22:05:47","slug":"arduino-basic-lesson-10-pwm-control-led-brightnessbreathing-light","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-10-pwm-control-led-brightnessbreathing-light\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 10: PWM Control LED Brightness(Breathing Light)"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-9-flowing-water-light\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-11-button\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/div>\n<div align=\"center\"><\/div>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#3\">What is PWM?<\/a><\/li>\n<li><a href=\"#4\">Connection<\/a><\/li>\n<li><a href=\"#5\">Upload Sketch<\/a><\/li>\n<li><a href=\"#6\">Running Result<\/a><\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p>In this lesson, we will show how to gradually change the luminance of an LED through programming. Since the pulsing light looks like breathing, we give it a magical name &#8211; breathing LED. We\u2019ll accomplish this effect with pulse width modulation (PWM).<\/p>\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>OSOYOO UNO Board (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO Magic I\/O Shield for Arduino x1<\/li>\n<li>Red LED Module x 1<\/li>\n<li>OSOYOO 3-Pin PnP cable x 1<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<h2 id=\"2.2\">Software<\/h2>\n<p>Arduino IDE (version 1.6.4+)<\/p>\n<h1 id=\"3\">What is PWM?<\/h1>\n<p>Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of &#8220;on time&#8221; is called the pulse width. To get varying analog values, you change, or modulate, that pulse width.<br \/>\nTo get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5V controlling the brightness of the LED. (See <a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/PWM\" target=\"_blank\" rel=\"noopener noreferrer\">the PWM description on the official website of Arduino<\/a>).<br \/>\n<a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/06\/pwm.png\"><img loading=\"lazy\" class=\"size-full wp-image-7720 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/06\/pwm.png\" alt=\"\" width=\"628\" height=\"394\" \/><\/a><br \/>\nWe can see from the top oscillogram that the amplitude of the DC voltage output is 5V. However, the actual voltage output is only 3.75V through PWM because the high level only takes up 75% of the total voltage within a period.<br \/>\nHere is an introduction to the three basic parameters of PWM:<br \/>\n<a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/06\/pwm2.png\"><img loading=\"lazy\" class=\"size-full wp-image-7721 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/06\/pwm2.png\" alt=\"\" width=\"616\" height=\"347\" \/><\/a><\/p>\n<ol>\n<li><strong>uty cycle<\/strong> describes the proportion of &#8220;on&#8221; time to the regular interval or period of time.<\/li>\n<li><strong>Period<\/strong> describes the reciprocal of pulses in one second.<\/li>\n<li>The voltage amplitude here is 0V\u20135V.<\/li>\n<\/ol>\n<h1 id=\"4\">Connection<\/h1>\n<p>There are 6 pins on most Arduino boards marked with \u2018PWM\u2019 next to the pin number (on some boards it is an \u201c~\u201d symbol) \u2013 these pins are PWM pins.We use the pin D3 to control the LED module here.<\/p>\n<p>First, please plug Osoyoo Magic I\/O shield into UNO board\uff1a<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson4\/UNO%2BBase-min.png\" \/><\/p>\n<p>Then connect the LED module to the D3 port of the Magic I\/O shield with a 3-pin PNP cable as below:<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson3\/Base-Red-LED-min.png\" \/><br \/>\n<span style=\"color: #ff0000;\"><strong>Notice:<\/strong><\/span><br \/>\nYou cannot directly connect an LED to a battery or voltage source. Firstly, because the LED has a positive and a negative lead and will not light if they are the wrong way around and secondly, an LED must be used with a resistor to limit or &#8216;choke&#8217; the amount of current flowing through the LED &#8211; otherwise the LED could burn out!<\/p>\n<h1 id=\"5\">Upload Sketch<\/h1>\n<p>Connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson1\/uno-pc.png\" \/><\/p>\n<p>Open the Arduino IDE and select corresponding board type and port type for your Arduino board.<\/p>\n<p><a href=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/06\/boardportselect.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-7723 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/06\/boardportselect.jpg\" alt=\"\" width=\"486\" height=\"593\" \/><\/a><\/p>\n<p>Now load the \u2018<a id=\"7489fc3c914392aa4f40c9095e2cbb90-be7a26bff3d4d1ab19d19c64f907acfc96bfcf95\" class=\"js-navigation-open\" title=\"PWM_Control_LED.zip\" href=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/code\/Lesson11_PWM_Control_LED.rar\" target=\"_blank\" rel=\"noopener noreferrer\">PWM_Control_LED<\/a>\u2019 example sketch or copy below code to your new Arduino IDE window and upload it to your arduino board. Wait a few seconds &#8211; you should see the RX and TX leds on the board flashing. If the upload is successful, the message &#8220;Done uploading.&#8221; will appear in the status bar.<\/p>\n<pre><span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n  <span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">3<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/ declare pin 3 to be an output<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n  <span style=\"color: #5e6d03;\">for<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">a<\/span><span style=\"color: #434f54;\">=<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\">a<\/span><span style=\"color: #434f54;\">&lt;=<\/span><span style=\"color: #000000;\">255<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #000000;\">a<\/span><span style=\"color: #434f54;\">++<\/span><span style=\"color: #000000;\">)<\/span>     <span style=\"color: #434f54;\">\/\/loop from 0 to 255,it controls the increase in PWM brightness<\/span>\r\n  <span style=\"color: #000000;\">{<\/span>\r\n    <span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">3<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">a<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>           <span style=\"color: #434f54;\">\/\/set the brightness of pin 3:          <\/span>\r\n    <span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">8<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>                   <span style=\"color: #434f54;\">\/\/wait for 8 ms        <\/span>\r\n  <span style=\"color: #000000;\">}<\/span>\r\n    <span style=\"color: #5e6d03;\">for<\/span> <span style=\"color: #000000;\">(<\/span><span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">a<\/span><span style=\"color: #434f54;\">=<\/span><span style=\"color: #000000;\">255<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #000000;\">a<\/span><span style=\"color: #434f54;\">&gt;=<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #000000;\">a<\/span><span style=\"color: #434f54;\">--<\/span><span style=\"color: #000000;\">)<\/span>   <span style=\"color: #434f54;\">\/\/loop from 255 down to 0,it control PWM brightness reduction<\/span>\r\n  <span style=\"color: #000000;\">{<\/span>\r\n    <span style=\"color: #d35400;\">analogWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">3<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #000000;\">a<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>           <span style=\"color: #434f54;\">\/\/ set the brightness of pin 3:<\/span>\r\n    <span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">8<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>                   <span style=\"color: #434f54;\">\/\/wait for 8 ms<\/span>\r\n  <span style=\"color: #000000;\">}<\/span>\r\n  <span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">800<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>                  <span style=\"color: #434f54;\">\/\/wait for 800 ms<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<\/pre>\n<h1 id=\"6\">Running Result<\/h1>\n<p>A few seconds after the upload finishes, you should see the LED gets brighter and brighter, and then slowly dimmer, just like breathing.<\/p>\n<div align=\"center\"><iframe src=\"https:\/\/www.youtube.com\/embed\/zcTFu-tmWBo\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<div align=\"center\">\n<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-9-flowing-water-light\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-11-button\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software What is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":3,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions\/83"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}