Skip to content

General Purpose Input Output

Tushar Semwal edited this page Oct 18, 2017 · 1 revision

The arguments passed to a predicate can take three forms - Input, Output or Both, and are denoted by symbols +, - and ? respectively.

  1. Set the mode of a pin to input or output using the pinMode command:
    Syntax: pinMode(+Pin,+Mode)
    • Pin: GPIO pin number according to WiringPi.
    • Mode: INPUT (0) or OUTPUT (1) mode.
  2. Use digitalRead command to read the input of a particular pin.
    Syntax: digitalRead(+Pin,-ValueReturned)
    • Pin: GPIO pin number according to WiringPi.
    • ValueReturned: Returns a value - either HIGH (1) or LOW (0) for the pin.
  3. Use digitalWrite command to write a value to the particular pin.
    Syntax: digitalWrite(+Pin,+Value)
    • Pin: GPIO pin number according to WiringPi.
    • ValueReturned: set value HIGH (1) or LOW (0) for the pin.
      Example for LED:
    1. Connect positive of led to GPIO pin 1(see AgPi pin mapping) along with a resistance in series.
    2. Connect negative of led to GPIO pin 0(see AgPi pin mapping).
    3. Write the following code to light up the LED:
      ?- pinMode(0,0). --set pinmode of pin 0 to 0 for output.
      ?- pinMode(1,0). --set pinmode of pin 1 to 0 for output.
      ?- digitalWrite(0,0). --set value of pin 0 to 0 for LOW/ground.
      ?- digitalWrite(1,1). --set value of pin 1 to 1 for HIGH/(3.3v).
    4. This will light up the LED.
    5. To turn it off use the command digitalWrite(1,0) to write value 0 to pin 1.
Clone this wiki locally