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:
// give the pin numbers names: const int inputPin = 2; const int outputPin = 3; void setup() { // declare inputPin to be an input: pinMode(inputPin, INPUT); pinMode(outputPin, OUTPUT); } void loop() { if (digitalRead(inputPin) == 1) { digitalWrite(outputPin, HIGH); } }
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:
//give pin number a name: const int LEDpin = 13; void setup() { pinMode(LEDPin, OUTPUT); } void loop() { digitalWrite(LEDpin, HIGH); delay(1000); digitalWrite(LEDpin, LOW); delay(1000); }
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:
output PORTB.1main: high PORTB.1 pause 1000 low PORTB.1 pause 1000 goto main
BX-24:
The command on the BX24 for a digital output is
call putPin(pinNumber, state)
pinNumber is a byte variable. It is the pin number that you want to output to.
state is the state that you want to output (0 for 0V, 1 for 5V). It is a byte variable.
Here’s a simple blink program in BX-BASIC:
sub main() do call putPin(12,1) call delay(1.0) call putPin(12,0) call delay(1.0) loop end sub