Use the BeagleBone Black Serial Port to Connect to a Device

This example shows how to create a connection to a serial device, write data to the device, and read data from the device.

Caution

Excessive voltage and current can damage the BeagleBone Black hardware. Observe the manufacturer precautions for handling the BeagleBone Black hardware and connecting it to other devices. For more information, see the local copy of the BeagleBone drivers and documentation in the BeagleBone Black Getting Started folder on your host computer, or https://beagleboard.org/Support/Hardware%20Support.

  1. Create a connection to the BeagleBone Black hardware.

    bbb = beaglebone;
  2. Show the location of the Tx and Rx pins on the GPIO header.

    showAllPins(bbb)

  3. Connect the BeagleBone Black hardware to a +3.3V serial device.

    BeagleBone Black hardware uses +3.3V. Do not connect BeagleBone Black hardware directly to devices that use higher voltages.

    • To receive data, connect the P9_26 (UART1_RXD) pin on the BeagleBone Black hardware to the TxD pin on the serial device.

    • To transmit data, connect the P9_24 (UART1_TXD) pin on the BeagleBone Black hardware to the RxD pin on the serial device.

    • Connect a ground pin, GND, on the BeagleBone Black hardware to the GND pin on the serial device.

    • Connect a +3.3V pin on the BeagleBone Black hardware to the VCC pin on the serial device.

  4. Restart the BeagleBone Black hardware. After the hardware restarts, you can use serialdev to exchange data with serial devices.

  5. Before continuing, research the manufacturer product information to determine which baud rate, data bits, parity, and stop bit settings the serial device supports.

  6. Use enableSerialPort to reconfigure GPIO pins P9_26 and P9_24 as UART1_RXD and UART1_TXD.

    enableSerialPort(bbb, 1)
    bbb.AvailableSerialPorts
    ans = 
    
        '/dev/ttyO1'

    In '/dev/ttyO1', the 'O' is the capital letter O, not the number zero.

  7. Use serialdev to create a connection to the serial device and assign the connection to a object.

    serial = beaglebone.serialdev(bbb,'/dev/ttyO1')
    serial = 
    
      serialdev with properties:
    
        BaudRate: 115200
        DataBits: 8
          Parity: 'none'
        StopBits: 1
         Timeout: 10
  8. In this example, the connection uses the default values for baud rate (115200), data bits (8), parity ('none'), and stop bit (1).

    If the serial device requires nondefault values, use a set of optional arguments to override those defaults.

    serial = beaglebone.serialdev(bbb,'/dev/ttyO1',115200,8,'none',2)
     serialdev with properties:
    
            Port: '/dev/ttyO1'
        BaudRate: 115200
        DataBits: 8
          Parity: 'none'
        StopBits: 1
         Timeout: 10

    This example overrides the default value of StopBits by setting it to 2. The other arguments maintain the correct sequence of arguments to the left of the rightmost overriding value.

  9. You can write values to the serial device.

    write(serial,[10 12],'uint16')

    In this example, you write two values to the serial device. The values override the default precision, uint8, by setting it to uint16.

  10. You can also read an array of values from the serial port.

    output = read(serial,100)

    This example reads a 100-element array of uint8 values from the serial device.

  11. If the serial connection times out during read operations, you can adjust the time out period by assigning a new value to the Timeout property.

    serial.Timeout = 20
     serialdev with properties:
    
            Port: '/dev/ttyO1'
        BaudRate: 115200
        DataBits: 8
          Parity: 'none'
        StopBits: 1
         Timeout: 20
    

When you are finished using the serial interface, restart the hardware to make additional GPIO pins available.