Remote Debug GPIO on Raspberry Pi

Recently I’ve been getting into embedded Linux, particularly the Raspberry Pi and have consequently been learning Python. I really don’t like programming directly on these small devices since the environment is typically spare and slow.

At first I used Cyberduck to open and update files along with a text editor. That became exhausting particularly as my project began to grow and span multiple directories. It was also a lot of Alt+Tabbing back and forth between Cyberduck, the text editor, and PuTTY.

What I really needed was something that I could work on in my main dev environment but deploy and execute on the RPi. This is particularly important to me since my RPi is way out in the garage. It also needed to be able to run as root because all RPi GPIO requires root privileges.

I decided on PyCharm, since I have a JetBrains Toolbox subscription and I’m familiar with CLion which uses the same base IDE framework. Most importantly, PyCharm has a remote debugging feature which coupled with automatic deployment makes everything super easy.

Setting Up Remote Debugging

Below is how I set up my environment. Much of this is found in the PyCharm help documentation. You can find how to set up remote debugging particularly the section on setting up a remote interpreter via SSH.

Automatic Deployment

First we need to setup automatic deployment of our files to the RPi. This part isn’t strictly required but if you don’t do it you’ll have to manage uploading your changes.

  1. Create a new project called RemoteDebugEx.
  2. Click on File -> Settings. Then go to Editor -> Code Style and change line endings to “Unix and OS X (\n)”. You want this since your target environment is Linux and different line endings may cause issues.
    rd2-03 Settings
    rd0-Set Line Endings
  3. Click on Tools -> Deployment -> Configuration.
    rd1-01 Configure Remote
  4. Add a new SFTP connection called “My RPi”. This will use SSH to copy files to and from the Pi.
    rd1-02 Create SFTP
  5. Provide connection settings. You can use a server name or IP address.
    rd1-03 Configure Connection
  6. Press the “Test SFTP connection…” button to make sure everything is correct. You may be prompted to accept the new fingerprint. Click Yes.
    rd1-03.5 Test Connection
  7. Set paths to say where we want to keep this on our remote. You can choose temporary or permanent locations. (I use temporary, but PyCharm’s sync features allow you to make changes on the RPi and download them back to your computer.)
    rd1-04 Set Paths
  8. Now click Tools -> Deployment -> Automatic Upload. This makes it so files will automatically upload whenever you save.
    rd1-05 Set Automatic Upload

Remote Interpreter

The next task is to set up a remote interpreter. In this section I’ll also show you how to download changes you make on the Pi.

  1. Now we need a virtual environment on the remote. Create an empty requirements.txt file. Save and it should be uploaded automatically. This also creates the remote debug folder.
  2. Log into the Pi using SSH (I like PuTTY but there are other options). We should be able to see our newly uploaded file.
    ls /home/pi/remote_debug/remote_debug_ex
  3. Create the virtual environment. First make sure virtualenv is installed.
    sudo pip install virtualenv
    Then navigate to your project root folder on the remove and create the virtual environment.
    cd /home/pi/remote_debug/remote_debug_ex
    virtualenv venv
    source venv/bin/activate
    pip install RPi.GPIO
    pip freeze > requirements.txt
  4. Now you want to get the updated requirements.txt file back to your computer. Right click the file and choose “Sync with Deployed to My RPi…”. You’ll see a compare dialog that allows you to copy the file back to your computer.
    rd2-01 Sync Requirements
    rd2-02 Sync Dialog
  5. The next step is to add the virtual environment as a project interpreter. Click on File -> Settings.
    rd2-03 Settings
  6. Then go to Project: RemoteDebugEx -> Project Interpreter. Click the gear icon and choose Add Remote.
    rd2-04 Add Remote Interpreter
  7. Choose to use SSH credentials and enter your host and login information. For “Python interpreter path”, choose the “python” file within your virtual environment folder.
    rd2-05 Configure Remote Interpreter
    Important: The login you use here is the credentials that the remote process will be run as. You can use the “pi” user, as we have another way of gaining root privileges to access GPIO detailed below.

Run Configuration

Still with me? Now that we’ve got deployment set up and an interpreter that will use our remote virtual environment, the final step is to create a run configuration to actually run a script.

  1. Create a simple python script and call it Give it the following contents.
    import RPi.GPIO
    print "hello world!"
  2. Click in the upper right of the main window and choose “Edit Configurations…”.
    rd3-01 Run Configuration
  3. Click the plus button and choose Python.
    rd3-02 Add Python Interpreter
  4. Give the new configuration the name “hello_world (remote)”. For the script, choose the script we just created. For Python interpreter, choose the remote interpreter we created in the last section.
    rd3-03 Run Configuration
  5. Now add a path mapping to map from your local project path to the remote path. This lets the interpreter find the source file for what’s executing remotely.
    rd3-04 Edit Path Mappings
    rd3-05 Run Configuration With Paths
  6. Save the new configuration and click the run button. You should see PyCharm connect and the hello world print on the debug console.
    rd3-06 Run Success

Running as Root

Rather than enabling logging in as root over SSH, there’s another approach that will work without opening that security hole. Using permissions, we can cause our python interpreter to simply run as root.

  1. Reconnect using PuTTY and navigate to the project root folder.
    cd /home/pi/remote_debug/remote_debug_ex
  2. Change ownership of the python interpreter to root and cause it to be executed as its owner whenever it’s run.
    sudo chown -v root:root venv/bin/python
    sudo chmod -v u+s venv/bin/python

This will cause pip to act a bit funny when you want to install anything later on so just reverse the above changes from step 2 as needed. Just use the same commands but with pi instead of root and with u-s.

I have a couple of scripts that I keep in the project root for just this purpose. You can download them here.


Now you have a way to remote debug your RPi with the interpreter running as root, allowing access to the Pi’s GPIO. You can do this with multiple projects and even have multiple projects or instances of projects open and debugging remotely.

PyCharm is a great IDE and I encourage you to look into using it to improve your development environment. For example, check out Zeal and the Dash plugin that will cause PyCharm to perform a documentation lookup when you press Ctrl+Shift+D. There are also plugins to provide support for Markdown and bash scripts.

2 thoughts on “Remote Debug GPIO on Raspberry Pi

Leave a Reply

Your email address will not be published. Required fields are marked *