Arduino Projects For Dummies
Book image
Explore Book Buy On Amazon

Following is a handy reference that you can use to control and access your BeagleBone’s general purpose input/output (GPIOs) with the file system, BoneScript, and Python.

Controlling the GPIO with the file system

You can use the following commands to control the GPIO with the file system.

  • Exporting a pin:

    echo 40 > /sys/class/gpio/export
  • Setting a pin OUTPUT:

    echo out > /sys/class/gpio/gpio40/direction
  • Writing a pin HIGH:

    echo 1 > /sys/class/gpio/gpio40/value
  • Writing a pin LOW:

    echo 0 > /sys/class/gpio/gpio40/value
  • Setting a pin INPUT:

    echo in > /sys/class/gpio/gpio40/direction
  • Reading the value from an INPUT pin (returns 1 for HIGH and 0 for LOW):

  • cat /sys/class/gpio/gpio40/value

Controlling the GPIO with BoneScript

You can use the following BoneScript commands to control the GPIO.

  • Loading a BoneScript module:

    var b = require('bonescript');
  • Setting a pin OUTPUT:

    b.pinMode("P9_14", b.OUTPUT);
  • Writing a pin HIGH:

    b.digitalWrite("P9_14", b.HIGH);
  • Writing a pin LOW:

    b.digitalWrite("P9_14", b.LOW);
  • Setting a pin INPUT:

    b.pinMode("P8_11", b.INPUT);
  • Reading the value from a digital INPUT pin (returns HIGH or LOW):

    b.digitalRead("P8_11");
  • Setting a pin for pulse-width modulation (PWM) with 50 percent duty cycle:

    b.pinMode('P9_14', b.OUTPUT);
    b.analogWrite('P9_14', 0.5);
  • Reading the value from an analog INPUT pin (returns a value between 0 and 1):

  • b.analogRead('P9_40');

Controlling the GPIO with Python

You can use the following Python commands to control the GPIO.

  • Importing Adafruit’s BeagleBone Input Output Library:

    import Adafruit_BBIO.GPIO as GPIO
  • Setting a pin OUTPUT:

    GPIO.setup("P9_14", GPIO.OUT)
  • Writing a pin HIGH:

    GPIO.output("P9_14", GPIO.HIGH)
  • Writing a pin LOW:

    GPIO.output("P9_14", GPIO.LOW)
  • Setting a pin INPUT:

    GPIO.setup("P8_11", GPIO.IN)
  • Reading the value from a digital INPUT pin (returns HIGH or LOW):

    GPIO.input("P8_11")
  • Setting a pin for PWM with 50 percent duty cycle:

    import Adafruit_BBIO.PWM as PWM
    PWM.start("P9_14", 50)
  • Setting an analog INPUT:

    import Adafruit_BBIO.ADC as ADC
    ADC.setup()
  • Reading the value from an analog INPUT pin (returns a value between 0 and 1):

  • analogReading = ADC.read("P9_40")

About This Article

This article can be found in the category: