{"id":51,"date":"2014-06-24T14:36:29","date_gmt":"2014-06-24T18:36:29","guid":{"rendered":"https:\/\/itp.nyu.edu\/physicalcomputing\/?page_id=51"},"modified":"2022-11-06T17:23:03","modified_gmt":"2022-11-06T22:23:03","slug":"digital-input-output","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/lessons\/digital-input-output\/","title":{"rendered":"Digital Input &#038; Output"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is an introduction to basic digital input and output on a microcontroller. In order to get the most out of it, you should know something about the following concepts.&nbsp; You can check how to do so in the links below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a title=\"Electricity: the Basics\" rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/electricity-the-basics\/\" target=\"_blank\">Electrical circuits<\/a><\/li><li><a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/microcontrollers-the-basics\/\">What a microcontroller is and what it can do<\/a><\/li><\/ul>\n\n\n\n<p>These videos will help in understanding digital inputs and outputs:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/374066875\" target=\"_blank\">Digital Inputs<\/a><\/li><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/374067285\" target=\"_blank\">Digital Outputs<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Digital_Inputs\"><\/span>Digital Inputs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When you&#8217;re trying to sense activity in&nbsp;the physical world using a microcontroller, the simplest activities you can sense are those in which&nbsp;you only need to know one thing about the physical world: Whether something is true or false. Is the viewer in the room or out? Are they touching the table or not? Is the door open or closed? In these cases, you can determine what you need to know using a digital input, or switch.<\/p>\n\n\n\n<p><strong>Digital<\/strong>&nbsp;or&nbsp;<strong>binary&nbsp;inputs<\/strong>&nbsp;to microcontrollers have two states: off and on. If voltage is flowing, the circuit is on. If it&#8217;s not flowing, the circuit is off. To make a digital circuit, you need a circuit, and a movable conductor which can either complete the circuit, or not.<\/p>\n\n\n\n<figure class=\"wp-block-image alignnone\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_in_schem-e1408734660930.png\"><img loading=\"lazy\" decoding=\"async\" width=\"164\" height=\"192\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_in_schem-e1408734660930.png\" alt=\"Schematic of a Digital Input to a microcontroller\" class=\"wp-image-1680\"\/><\/a><figcaption>Figure 1. Schematic of a Digital Input to a microcontroller<\/figcaption><\/figure>\n\n\n\n<p>Figure 1 shows the electrical schematic for a digital input to a microcontroller. The current has two directions it can go to ground: through the resistor or through the microcontroller.&nbsp;When the switch is closed, the current will follow the path of least resistance, to the microcontroller pin, and the microcontroller can then read the voltage. The microcontroller pin will then read as high voltage or HIGH. When the switch is open, the resistor connects the digital input to ground, so that it reads as zero voltage, or LOW.<\/p>\n\n\n\n<p>On an&nbsp;Arduino module, you declare the pin to be an input at the top of your program. Then you read it for the values 1 (HIGH) or 0 (LOW), like so:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid setup() {\n \/\/ declare pin 2 to be an input:\n pinMode(2, INPUT);\n declare pin 3 to be an output:\n pinMode(3, OUTPUT);\n}\n\nvoid loop() {\n \/\/ read pin 2:\n if (digitalRead(2) == 1) {\n   \/\/ if pin 2 is HIGH, set pin 3 HIGH:\n   digitalWrite(3, HIGH);\n } else {\n   \/\/ if pin 2 is LOW, set pin 3 LOW:\n   digitalWrite(3, LOW);\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Digital_output\"><\/span>Digital output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Just as digital inputs allow you to sense activities which have two states, <strong>digital<\/strong>&nbsp;or <strong>binary&nbsp;outputs<\/strong> allow you to control activities which can have two states. With a digital output you can&nbsp;either turn something off or on. Figure 2 is the schematic diagram for digital output controlling an LED:<\/p>\n\n\n\n<figure class=\"wp-block-image alignnone\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_out_schem.png\"><img loading=\"lazy\" decoding=\"async\" width=\"223\" height=\"179\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_out_schem.png\" alt=\"Schematic of and led as a digital output from a microcontroller\" class=\"wp-image-1658\"\/><\/a><figcaption>Figure 2. Schematic of and led as a digital output from a microcontroller<\/figcaption><\/figure>\n\n\n\n<p>Digital outputs are often used to control other electrical devices besides LEDs,&nbsp; through <a rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/electronics\/transistors-relays-and-controlling-high-current-loads\/#Transistors\" target=\"_blank\">transistors<\/a> or <a rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/electronics\/transistors-relays-and-controlling-high-current-loads\/#Relays\" target=\"_blank\">relays<\/a>.&nbsp;For more information on that, see these <a title=\"Transistors, Relays, and Controlling High-Current Loads\" rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/transistors-relays-and-controlling-high-current-loads\/\" target=\"_blank\">notes on controlling high-current circuits<\/a>.<\/p>\n\n\n\n<p>On an&nbsp;Arduino module, you declare the pin an output at the top of the program just like you did with inputs. Then in the body of the program you use the <tt><a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/digital-io\/digitalwrite\/\" target=\"_blank\" rel=\"noreferrer noopener\">digitalWrite()<\/a><\/tt> command&nbsp;with the values&nbsp;HIGH and LOW to set the pin high or low, as you&#8217;ve&nbsp;seen above.<br>Here&#8217;s a simple blinking LED program in Arduino:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid setup() {\n  pinMode(13, OUTPUT);\n}\n\nvoid loop() {\n  digitalWrite(13, HIGH);\n  delay(1000);\n  digitalWrite(13, LOW);\n  delay(1000);\n}\n<\/pre><\/div>\n\n\n<p>As inputs, the pins of a microcontroller can accept very little current. Likewise, as outputs, they produce very little current. The electrical signals that they read and write are mainly changes in voltage, not current. When you want to read an electrical change that&#8217;s high-current, you limit the current input using a resistor. Similarly, when you want to control a high-current circuit, you use a transistor or relay, both of which allow you to control a circuit with only small voltage changes and minimal current. <a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380347302\" target=\"_blank\">Related video: Transistor<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This is an introduction to basic digital input and output on a microcontroller. In order to get the most out of it, you should know something about the following concepts.&nbsp; You can check how to do so in the links below: Electrical circuits What a microcontroller is and what it can do These videos &hellip; <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/digital-input-output\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Digital Input &#038; Output&#8221;<\/span><\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"parent":13,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[46,11,21,25,52,54,19,28],"tags":[],"class_list":["post-51","page","type-page","status-publish","hentry","category-arduino-fundamentals","category-code","category-components","category-digital","category-lesson","category-microcontrollers","category-switches","category-transistor"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/51"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":28,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/51\/revisions"}],"predecessor-version":[{"id":10693,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/51\/revisions\/10693"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}