Wolfram Language & System 10.4 (2016)|Legacy Documentation

This is documentation for an earlier version of the Wolfram Language.View current documentation (Version 11.2)

GPIO (General Purpose I/O)
This feature is not supported on the Wolfram Cloud.

provides low-level general-purpose input and output through digital pins directly connected to the processor running the Wolfram Language.
Note: By default, allows any pin to be used interchangeably in read and write mode. DeviceConfigure can be used to configure specific pins as read or write.
Note: To access GPIO you must be logged in as root or be running the Wolfram Language as root.
  • The possible values of GPIO pins are taken to be 0 and 1, corresponding to "low" and "high".
  • allows pins to be specified by integers.
  • On a Raspberry Pi, the following pins are available for reading and writing: 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 28, 29, 30, 31.
  • On the Raspberry Pi, pins can also be referenced using the WiringPi numbering scheme by specifying them in the form .
  • Device DiscoveryDevice Discovery

    Opening the DeviceOpening the Device

    • If GPIO is present, it is always accessible and does not require a connection step.
    • DeviceOpen["GPIO"] is not required, but can be used to obtain a DeviceObject whose properties can be queried.

    Configuring the DeviceConfiguring the Device

    • Configuration is not required for GPIO. Without configuration, any pin will automatically switch between input and output on read and write. Note that if a pin is used in a direction that is not intended, this may damage your system.
    • DeviceConfigure["GPIO",n->dir]
      sets the direction of pin n to be dir.

      DeviceConfigure["GPIO",{n1->dir1,n2->dir2,}]
      sets direction for pin .

    • Possible directions are , , and None.
    • Setting the direction to None releases a pin if it has previously been configured.
    • DeviceConfigure ignores requests to configure any pin that is being accessed by an asynchronous task.
    • If an attempt is made to use a GPIO pin in a direction inconsistent with its configuration, an error will be generated.

    Reading DataReading Data

      DeviceRead["GPIO",p]
      gives the value of GPIO pin p.

      DeviceRead["GPIO",{p1,p2,}]
      gives a list of the values of GPIO pins .

    • The value of a GPIO pin is always either 0 or 1.
    • Note: By default, allows any pin to be used interchangeably in read and write mode. DeviceConfigure can be used to configure specific pins as read or write.

    Writing DataWriting Data

      Note: To access GPIO you must be logged in as root or be running the Wolfram Language as root.

      DeviceWrite["GPIO",p->v]
      writes the value v to GPIO pin p.

    • The value v must be either 0 or 1.
    • Note: By default, allows any pin to be used interchangeably in read and write mode. DeviceConfigure can be used to configure specific pins as read or write.

    Closing and Releasing ResourcesClosing and Releasing Resources

    • DeviceClose["GPIO"] is not required, but can effectively be used to reset all pins to their default state and marks the corresponding DeviceObject as closed.

    ExamplesExamplesopen allclose all

    Basic Examples  (2)Basic Examples  (2)

    Find GPIO devices on the system:

    In[1]:=
    Click for copyable input

    Configure pin 4 for writing and pin 17 for reading:

    In[1]:=
    Click for copyable input

    Read the digital value on pin 17:

    In[2]:=
    Click for copyable input
    Out[2]=

    Set pin 4 to digital :

    In[3]:=
    Click for copyable input

    Read the digital value on pin 17:

    In[4]:=
    Click for copyable input
    Out[4]=