








{"id":84,"date":"2020-10-12T21:06:38","date_gmt":"2020-10-12T21:06:38","guid":{"rendered":"https:\/\/osoyoo.info\/?p=84"},"modified":"2020-10-13T22:07:16","modified_gmt":"2020-10-13T22:07:16","slug":"arduino-basic-lesson-12-buzzer","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-12-buzzer\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 12: Buzzer"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-11-button\/\"><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-13-photoresistor\/\"><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\">About the button<\/a><\/li>\n<li><a href=\"#4\">Connection<\/a><\/li>\n<li><a href=\"#5\">Upload Sketch<\/a><\/li>\n<li><a href=\"#6\">Program Running Result<\/a><\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson8\/passive-buzzer.png\" \/><\/p>\n<p>The buzzer is a small component with a piece of metal in it that moves when you apply a voltage across it. This motion causes a small sound, or \u201cclick.\u201d<\/p>\n<p>If you turn the voltage on and off fast enough, you get different beeps, squeals, chirps and buzzes. You will use PWM to control the speed of turning the piezo on and off \u2014 and, in turn, the audio frequency coming out of the buzzer. Adjusting the PWM enables you to get legitimate notes out of the buzzer.<\/p>\n<p>If you flip the buzzer over and look at the bottom, you will see that one pin has a (+) next to it. That pin gets connected to a signal from the P0 pin. The other pin should be connected to ground.<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson8\/buzzer.png\" \/><\/p>\n<p>Note: We use the buzzer module in this project.<\/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 x 1<\/li>\n<li>OSOYOO Passive Buzzer 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=\"4\">Connection<\/h1>\n<p>First, please plug Osoyoo Magic I\/O shield into UNO board:<\/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 Passive Buzzer module to the D8 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\/lesson8\/Base-Passive-Buzzer.png\" \/><\/p>\n<h1 id=\"5\">Upload Sketch<\/h1>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.<\/p>\n<h2>Code Program<\/h2>\n<p>You can <a href=\"https:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/code\/Lesson13_Passive_buzzer_song.rar\" target=\"_blank\" rel=\"noopener noreferrer\">download the sketch from this link<\/a> or copy below code to your Arduino IDE window:<\/p>\n<pre><span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">buzzer<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">8<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/the pin of the passive buzzer<\/span><\/pre>\n<pre><span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">buzzer<\/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;\">\/\/initialize the buzzer pin as an output<\/span><\/pre>\n<pre><span style=\"color: #000000;\">}<\/span><\/pre>\n<pre><span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span><\/pre>\n<pre><span style=\"color: #00979c;\">unsigned<\/span> <span style=\"color: #00979c;\">char<\/span> <span style=\"color: #000000;\">i<\/span><span style=\"color: #000000;\">;<\/span><\/pre>\n<pre><span style=\"color: #5e6d03;\">while<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">)<\/span><\/pre>\n<pre><span style=\"color: #000000;\">{<\/span> <span style=\"color: #434f54;\">\/\/output an frequency<\/span><\/pre>\n<pre><span style=\"color: #5e6d03;\">for<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">i<\/span><span style=\"color: #434f54;\">=<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #000000;\">i<\/span><span style=\"color: #434f54;\">&lt;<\/span><span style=\"color: #000000;\">80<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #000000;\">i<\/span><span style=\"color: #434f54;\">++<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">buzzer<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/wait for 1ms<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">buzzer<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/wait for 1ms<\/span><\/pre>\n<pre><span style=\"color: #000000;\">}<\/span> <span style=\"color: #434f54;\">\/\/output another frequency<\/span><\/pre>\n<pre><span style=\"color: #5e6d03;\">for<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">i<\/span><span style=\"color: #434f54;\">=<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #000000;\">i<\/span><span style=\"color: #434f54;\">&lt;<\/span><span style=\"color: #000000;\">100<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #000000;\">i<\/span><span style=\"color: #434f54;\">++<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #000000;\">{<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">buzzer<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">2<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/wait for 2ms<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">buzzer<\/span><span style=\"color: #434f54;\">,<\/span><span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><\/pre>\n<pre><span style=\"color: #d35400;\">delay<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">2<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span><span style=\"color: #434f54;\">\/\/wait for 2ms<\/span><\/pre>\n<pre><span style=\"color: #000000;\">}<\/span><\/pre>\n<pre><span style=\"color: #000000;\">}<\/span><\/pre>\n<pre><span style=\"color: #000000;\">}<\/span><\/pre>\n<pre><\/pre>\n<h2>Compile and upload<\/h2>\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\/07\/sketch.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-7908 aligncenter\" src=\"https:\/\/osoyoo.com\/wp-content\/uploads\/2017\/07\/sketch.jpg\" alt=\"\" width=\"486\" height=\"594\" \/><\/a><\/p>\n<p>After compile this sketch, simply click the \u201cUpload\u201d button in the environment. Wait a few seconds \u2013 you should see the RX and TX leds on the board flashing. If the upload is successful, the message \u201cDone uploading.\u201d will appear in the status bar.<\/p>\n<h1 id=\"6\">Running Result<\/h1>\n<p>A few seconds after the upload finishes, you should hear the buzzer beep.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Note again:<\/strong><\/span><\/p>\n<p>The active buzzer has built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed frequency.<\/p>\n<div align=\"center\"><iframe src=\"https:\/\/www.youtube.com\/embed\/ILe6t1E3pn4\" width=\"853\" height=\"480\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-11-button\/\"><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-13-photoresistor\/\"><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","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 About the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":154,"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\/84"}],"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=84"}],"version-history":[{"count":2,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/84\/revisions\/89"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/154"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}