phatIO has 20 pins which can be used for direct Input/Output with the real world numbered 0 through 19.
For each IO pin there is a file in the
PHATIO/io/mode directory indicating the current mode of the pin, and in
PHATIO/io/pins indicating the current value of the pin (which depends on the mode).
|IN||The pin is acting as a digital Input|
|HIGH||The pin is acting as a digital Input and is pulled high to 5V through a resistor|
|ADC||The pin behaves as an analogue Input, converted to digital through an ADC Analogue to Digital Convertor)|
|OUT||The pin behaves as a digital Output|
|PWM||The pin behaves as a PWM Input (pulse width modulation)|
When a pin is acting as a digital input, the contents of the pins
PHATIO/io/pins/<pin> will represent the value input on the pin. “0” for 0 volts and “1” for 5 volts (technically “0” if the input voltage is less than 1.0V, “1” if greater than 2.0V and undefined for anything in between or if the pin is unconnected).
On startup pins default to IN_.
This is the same as digital input except that the pin is connected to 5V with a resistor (of between 20K and 50K). This will pull the value to “1” unless the pin is connected to 0V.
When the value file is read the pin input voltage is sampled and and converted to a 10 bit digital value and output as a decimal integer between “0” (0 volts input) and “1023” (5 volts input).
If a non ADC pin (a list of ADC pins is in the file
PHATIO/io/etc/adc_pins) is set to ADC mode, its value will read “–1”.
When a pin file is written to from the host computer the contents is converted to an output voltage for the relevant pin: 0 volts if the first character of the value file is “0”, 5 volts otherwise.
When in PWM mode the pin will oscillate between 0 volts and 5 volts. The contents of the value file will specify the duty cycle of the output - what percentage the signal is at 5 volts. The value should be a decimal integer between “0” - output pin always at 0 volts - and 255 - output pin always at 5 volts. A value of 127 would give a duty cycle of 50% - a square wave.
If a non PWM pin (a list of PWM pins is in the file
PHATIO/io/etc/pwm_pins) is set to PWM mode, any value other than “0” will turn on the pin.
On a Unix system the following (where PHATIO is set to the phatIO filesystem location) would turn pin 0 into an OUTPUT and turn it high:
echo "OUT" > $PHATIO/io/modes/0 echo "1" > $PHATIO/io/pins/0
The following would make pin 10 a PWM output and set it to 170 (a duty cycle/intensity of 170/255 = 2/3).
echo "PWM" > $PHATIO/io/modes/10 echo "170" > $PHATIO/io/pins/10
Inputs are a little tricky - as the filesystem will often cache the contents of the value files after reading them, so:
echo "ADC" > $PHATIO/io/modes/11 cat $PHATIO/io/modes/11
Would turn pin 11 into an Analogue input can read the value of the Analogue to Digital Convertor on it, but further reads may be reading the Operating System cached version rather than phatIO. The host computer section discusses this. One approach is to use phatIO conditions to do something when an input pin changes. Basic ADC gives some examples of using the ADC pins.