IO programming
IO programming
I/O Programming
• Digital Output Circuitry
• Digital Input Circuitry
• pinMode ()
• Arduino Uno Pin Definitions
• digitalWrite ()
• delay ()
• digitalRead()
• Analog Input Circuitry
• analogRead()
• Fully appreciate the software interface to microcontrollers and to gain insight into how to
interface controllers to external devices, it is useful to examine the underlying circuitry.
• In this chapter we shall investigate the basic digital output circuitry.
• Anything that can be controlled with a simple logical yes/no, true/false input can be a target.
• Although controllers usually have limited output current and voltage capability, they can
control devices such as power transistors which can in turn control more demanding loads.
• A simplified diagram of the General Purpose Input-Output (GPIO) circuitry
• Therefore this circuitry would be replicated seven more times to accommodate a single
byte-wide port, of which there may be several.
• Also, in order to reduce the external pin count of the IC, pins may be multiplexed with other
functions.
• Pxn, at the far left of the schematic, represents the physical output pin.
• The data bus shown along the right edge is the source for data being written to the physical
pin and also the destination for data being read from the pin.
The function decoded Arduino pin 8 as being bit 0 of port B (i.e., PORTB). It also determined that the
corresponding DDR is DDRB. The bit 0 mask is 0x01 and it was ORed with the contents of DDRB, thus
selecting the direction for that bit as output mode.
digitalWrite( )
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the
corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is
configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup
resistor (see the tutorial on digital pins). Writing LOW will disable the pullup. The pullup resistor is
enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely cause. The
remedy is to set the pin to an output with the pinMode() function.
Note: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED
and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k
pull-up resistor, it will hang at around 1.7 V instead of the expected 5V because the onboard LED and
series resistor pull the voltage level down, meaning it always returns LOW. a digital input, use an
external pull down register.
Syntax
•digitalWrite(pin, value)
Parameters
• Returns none
delay( )
• Pauses the program for the amount of time (in miliseconds) specified as parameter.
Syntax
delay( ms )
Parameters
• Returns nothing
digitalRead ()
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead( pin )
Parameters