Digital Input & Output

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 a digital input, or switch.
Digital inputs have two states: off and on. If voltage is flowing, the circuit is on. If it’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.

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.Wiring/Arduino:
On 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:

PicBasic Pro:
In 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:

input PORTB.0
output PORTB.1
main:
if PORTB.0 = 1 then ' if the switch is closed on pin RB0
high PORTB.1 ' set pin RB1 high
endif
goto main

BX-24:
To read a digital input on the BX-24 into a variable, use the command

x = GetPin(pinNumber)

PinNumber is a byte variable. It is the pin number that you want to read. x is a byte variable too. Here’s an example that turns on a second pin if you the switch is closed:

sub main()
do
if getPin(12) = 1 then ' if the switch is closed on pin 12
call putPin(13,1) ' set pin 13 high
end if
loop
end sub

Digital output

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:

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.

Wiring/Arduino:
On 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’ve seen above.
Here’s a simple blinking LED program in the Wiring syntax, for either module:

PicBasic Pro:
On 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’ve seen above.
Here’s a simple blinking LED program in PicBasic Pro: