BASIC Stamp FAQ
STAMP FAQS PROGRAMMING INFORMATION
Last Revised On: 7/21/00 Page: 16
How do I set an I/O pin to input or output mode?
There are many ways to set the I/O pin directions. The most readable and intuitive method is to use the INPUT and
OUTPUT commands. For example:
INPUT 5
OUTPUT 6
will set I/O pin 5 to an input and I/O pin 6 to an output. By default, upon reset or startup, all I/O pins are inputs,
thus only the second line of code above is really necessary.
Another method for setting the pin directions is to use the predefined variable DIRS or any one of its derivatives.
The following code is functionally equivalent to the first example:
DIR5 = 0
DIR6 = 1
DIR0 through DIR15 (or DIR7 on the BASIC Stamp I) are bit variables which control the direction of the
corresponding I/O pin. DIRS is a 16-bit variable (or 8-bit variable on the BASIC Stamp I) in which each bit
represents the direction of the corresponding I/O pin. A ‘1’ bit means output and a ‘0’ bit means input. The
advantage of this method of declaring pin directions is that multiple pin directions can be defined at once:
DIRS = %00101110
The above line of code defines I/O pins 7, 6, 4 and 0 as inputs and 5, 3, 2 and 1 as outputs. Review the BASIC
Stamp manual for more examples of this.