Using Raspberry Pi (Model B+) to interface to FTDI devices

I have always used Windows computers to interface to an FTDI based USB devices I built. Now due to the low cost and great availability of Raspberry Pi modules, it makes sense to see whether I could use a Raspberry Pi to talk to my FTDI based USB devices.

I have several Raspberry Pi Model B+ available from previous projects working on cluster computing and from my general hoarding of Rasberry Pi modules.

Googling the internet is the first thing I try to learn more about how to create an interface from Raspberry Pi to an FTDI device.

I found 2 ways: 1) Use PySerial and 2) Use pyLibFTDI. I’ll talk about both methods since they are both useful.

*Note:  I’ll use ‘$’ in command line to show that it’s in the console terminal and if there’s no ‘$’ then it’s a python code.  Don’t type $ in the console or the command won’t run.

PySerial Method:
This method is pretty straight forward since Raspberry Pi should already come with PySerial installed.  If not, run the install with the command line:

$ sudo apt-get install python-serial

Once the installation is done, you can launch into python by typing

$ python

You can also write a python file and just run it using the command below where <myfile> is your file name.  There are many text editor in Raspberry Pi, but I normally use nano.  I’ll talk about this later when you have to create a rule file.

$ python <myfile>.py

In order to figure out all the serial devices attached to your Raspberry Pi, you can run the following codes in Python.  I’m using Python 2.7 by the way.

import glob
ports = glob.glob('/dev/tty[A-Za-z]*')
for port in ports:

You might see a listing like this:


Now you can interface to your device using the proper named port from the list.  My USB device is the ttyUSB0.  The ttyAMA0 is the GPIO serial port that you can actually use to to communicate to Raspbery Pi via serial via your computer.  You can google more about using ttyAMA0 if you are curious since I won’t cover that here.

import serial
ser = serial.Serial ("/dev/ttyUSB0")    #Open named port 
ser.baudrate = 9600                     #Set baud rate to 9600
ser.write('command\r')                  #send command string               #read data in read buffer
#You could also use ser.readline() to read but need to know # lines.

You can refer to PySerial API website to get all the commands and various things you can do with PySerial.

pyLibFTDI Method:
This is another way to interface to a USB device but you need them to be an FTDI device to work.  The nice thing about this method is the speed and reliability of the communication due to the use of FTDI’s own driver to talk to the device.  You can even bitbang the port using this method.  I’m not covering that topic, but you can find more details on pyLibFTDI website.

To use pyLibFTDI in Raspberry PI, you first need to install LibFTDI by running the following command:

$ sudo apt-get install libftdi-dev

After that is done, install pyLibFTDI.  You need PIP to install but I think that should already be available in Raspbian.  I’m using Raspbian Jessie (2016-05-27).

$ sudo pip install pylibftdi

The thing to worry about is the port access permission.  You can’t really access the port unless you are a root account or use Sudo to send the command.  This is problematic if you want to be able to just run the interface from any account.  To address this issue, defining a new rule by creating one like this:

 $ sudo nano /etc/udev/rules.d/99-libftdi.rules

Nano is my favorite editor on Raspberry Pi. Add these lines to the rule file then Ctrl+o (letter ‘O’ not zero) to write the file then Ctrl+x to exit.

SUBSYSTEMS=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", GROUP="dialout", MODE="0660"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", GROUP="dialout", MODE="0660"

You want to reload the rules so that this new rules take effect.

# Reload rules after edit.
$ udevadm control --reload-rules<\pre>

Now you are ready to try pyLibFTDI. Try listing the devices available by typing in console (not in python):

$ python -m pylibftdi.examples.list_devices

It should list the device(s) assuming you have FTDI device(s) connected to the Raspberry Pi. If you see an empty listing like


There’s a permission problem to access the port. Restarting Raspberry Pi seems to help to reload the rules when the reload rules command doesn’t work.

Now we can test pyLibFTDI in python. If you have listed your devices, you can use the device identity to talk to it.  For example, when I ran the list_devices example, I have


These are the 2 FTDI USB devices I have attached to my Raspberry Pi.  I’ll try to talk to the first one.

from pylibftdi import Device
device = Device('AK003XYN')
device.baudrate = 38400
device.write('?\r')             #send an identity query command.
                                #This device needs \r to end command.
device.readlines()              #Return data in buffer
device.close()                  #Close the device

These two methods to interface to your FTDI devices should get you started on having an ability to talk to your serial port devices without the need of a Windows based computer.  Enjoy and happy building and coding!