{"id":156,"date":"2008-02-24T18:46:59","date_gmt":"2008-02-24T23:46:59","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/\/digital-input-output"},"modified":"2009-09-08T10:42:44","modified_gmt":"2009-09-08T15:42:44","slug":"digital-input-output","status":"publish","type":"page","link":"https:\/\/www.tigoe.com\/pcomp\/code\/controllers\/input-output\/digital-input-output\/","title":{"rendered":"Digital Input &#038; Output"},"content":{"rendered":"<h2>Digital Input<\/h2>\n<p>In many cases, 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.<br \/>\nDigital inputs 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<p><img decoding=\"async\" src=\"\/pcomp\/img\/digin.gif\" alt=\"\" \/>In the above diagram, you add the resistor to resist the current flow. When the switch is closed, the current will follow the path of least resistance, to the microcontroller pin, sending it a signal like you want it to do. you need the connection to ground as a reference point, and the resistor also prevents a power-to-ground short circuit.<strong>Wiring\/Arduino:<\/strong><br \/>\nOn a Wiring module or an Arduino module, you  declare the pin to be an input at the top of your program. Then you read it for the values 1 or 0, like so:<\/p>\n<pre>\/\/ give the pin numbers names:\r\nconst int inputPin = 2;\r\nconst int outputPin = 3;\r\nvoid setup() {\r\n \/\/ declare inputPin to be an input:\r\n pinMode(inputPin, INPUT);\r\n pinMode(outputPin, OUTPUT);\r\n}\r\n\r\nvoid loop() {\r\n if (digitalRead(inputPin) == 1) {\r\n \tdigitalWrite(outputPin, HIGH);\r\n }\r\n}<\/pre>\n<p><strong>PicBasic Pro:<\/strong><br \/>\nIn PicBasic Pro, you first declare the pin to be an input at the top of your program. Then you read it for the values 1 or 0, like so:<\/p>\n<pre>input PORTB.0\r\noutput PORTB.1\r\nmain:\r\n if PORTB.0 = 1 then ' if the switch is closed on pin RB0\r\n \thigh PORTB.1 ' set pin RB1 high\r\n endif\r\ngoto main<\/pre>\n<p><strong>BX-24:<\/strong><br \/>\nTo read a digital input on the BX-24 into a variable, use the command<\/p>\n<pre> x = GetPin(pinNumber)<\/pre>\n<p><em>PinNumber<\/em> is a byte variable. It is the pin number that you want to read. x is a byte variable too. Here&#8217;s an example that turns on a second pin if you the switch is closed:<\/p>\n<pre>sub main()\r\n do\r\n \tif getPin(12) = 1 then\t' if the switch is closed on pin 12\r\n \t\tcall putPin(13,1)\t' set pin 13 high\r\n \tend if\r\n loop\r\nend sub<\/pre>\n<h2>Digital output<\/h2>\n<p>The simplest control you can use over an electrical device is digital output. In this case, you would either turn something off, or on. The diagram below is a digital output controlling an LED:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/pcomp\/img\/digout.gif\" alt=\"\" width=\"149\" height=\"132\" \/><\/p>\n<p align=\"left\">\n<p>Digital outputs are often used to control other electrical devices, through transistors or relays. More detail on these components will follow later in the course.<\/p>\n<p><strong>Wiring\/Arduino:<\/strong><br \/>\nOn a wiring or Arduino module, you declare the pin an output at the top of the program, then in the body of the program you use the commands HIGH and LOW to set the pin high or low, as we&#8217;ve seen above.<br \/>\nHere&#8217;s a simple blinking LED program in the Wiring syntax, for either module:<\/p>\n<pre>\/\/give pin number a name:\r\nconst int LEDpin = 13;\r\n\r\nvoid setup() {\r\n pinMode(LEDPin, OUTPUT);\r\n}\r\n\r\nvoid loop() {\r\n digitalWrite(LEDpin, HIGH);\r\n delay(1000);\r\n digitalWrite(LEDpin, LOW);\r\n delay(1000);\r\n}<\/pre>\n<p><strong>PicBasic Pro:<\/strong><br \/>\nOn the PIC, you declare the pin an output at the top of the program, then in the body of the program you use the commands HIGH and LOW to set the pin high or low, as we&#8217;ve seen above.<br \/>\nHere&#8217;s a simple blinking LED program in PicBasic Pro:<\/p>\n<pre>output PORTB.1main:\r\n high PORTB.1\r\n pause 1000\r\n low PORTB.1\r\n pause 1000\r\ngoto main<\/pre>\n<p><strong>BX-24:<\/strong><br \/>\nThe command on the BX24 for a digital output is<\/p>\n<pre>call putPin(pinNumber, state)<\/pre>\n<p align=\"left\">\n<p><em>pinNumber<\/em> is a byte variable. It is the pin number that you want to output to.<\/p>\n<p><em>state<\/em> is the state that you want to output (0 for 0V, 1 for 5V). It is a byte variable.<br \/>\nHere&#8217;s a simple blink program in BX-BASIC:<\/p>\n<p align=\"left\">\n<pre>sub main()\r\n do\r\n \tcall putPin(12,1)\r\n \tcall delay(1.0)\r\n \tcall putPin(12,0)\r\n \tcall delay(1.0)\r\n loop\r\nend sub<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Digital Input In many cases, 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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/controllers\/input-output\/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":2,"featured_media":0,"parent":155,"menu_order":1,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-156","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":4,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/156\/revisions"}],"predecessor-version":[{"id":588,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/156\/revisions\/588"}],"up":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/155"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}