Raspberry Pi Projects For Dummies
Book image
Explore Book Buy On Amazon

Need a new Raspberry Pi project? You can run quite a few third-party languages on the LEGO brick, but ev3dev puts a cut-down version of Linux on the brick. You don’t need to change anything on the brick because ev3dev runs off a micro SD card inserted into the brick. You can easily revert your LEGO brick back to the original state by simply unplugging the SD card.

The bulk of the installation work involves creating the SD card with the Linux distribution on it. It’s sort of like creating the SD card for the Raspberry Pi itself. At ev3dev.org, you can find instructions on creating the SD card under the Mac, PC, or Linux operating system.

After you create the SD card, you can plug the brick into the Raspberry Pi. You have to connect the brick directly into one of the Raspberry Pi’s USB sockets (as opposed to a USB hub) in order for it to work.

You communicate with the LEGO brick from the Raspberry Pi by using a Secure Shell (SSH) session. Open up a command-line window and set up a session. This is what you’ll see:

ssh [email protected]
[email protected]’s password:
             _____     _
   _____   _|___ /  __| | _____   __
  / _   / / |_  / _` |/ _   / /
 |  __/ V / ___) | (_| |  __/ V /
  ___| _/ |____/ __,_|___| _/
Debian GNU/Linux jessie on LEGO MINDSTORMS EV3!
The programs included with the Debian GNU/Linux 
system are free software;
the exact distribution terms for each program 
are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, 
to the extent permitted by applicable law.
Last login: Wed Nov  5 11:45:33 2014 from

To Linux, everything is a file, and the LEGO motors are no exception. You talk to them by writing to a file, and you get back information by listing a file. Because this is Linux, the normal command-line prompts still work and you’ll find it useful to use cd to change directory and ls to see what files are at the current directory level.

To use this system from within Python, you have to manage an SSH session from within a program. Most people recommend using Pexpect, but you may have more success with Paramiko. Install it using the following:

pip install paramiko -–upgrade

The following code shows you how to open, run, and close an SSH session from Python.

ssh Sesson for EV3dev
import paramiko
import sys
import math, time, os
# Create an SSH client
ssh = paramiko.SSHClient()
shell = " ; debug = True
def main():
   print"Wait for brick to be contacted"
   print "Now talking to brick"
   send_command("lsn") # Linux list directory
   print "Now shutting down"
# functions for accessing EV3dev
def login():
    global shell
    # Make sure that we add the remote server’s 
    # SSH key automatically
    # Connect to the EV3 brick 
    # change password to your own
    ssh.connect("", username="root",
    # Create a raw shell
    shell = ssh.invoke_shell()
    send_command("cd /sys/class/tacho-motorn") 
    #makes commands shorter
def wait_for_prompt():
    # Create a new receive buffer
    receive_buffer = "
    while not "#" in receive_buffer:
        # Get the receive buffer
        receive_buffer += shell.recv(1024)
    if debug :
        print receive_buffer
def send_command(command):
    receive_buffer = "
    while not "#" in receive_buffer:
        receive_buffer += shell.recv(1024)
    if debug or "-bash" in receive_buffer :
        print receive_buffer
if __name__ == '__main__':

About This Article

This article is from the book:

About the book authors:

Mike Cook is a lifelong electronics buff, a former lecturer in physics at Manchester Metropolitan University, and the author of more than 300 articles on computing and electronics. You'll often find him monitoring technology forums under the moniker Grumpy Mike. Brock Craft is a lecturer in creative coding in the Department of Computing at Goldsmiths, University of London, and a senior tutor at the Royal College of Art. He is a specialist in physical computing, data visualization, and the Internet of Things. Jonathan Evans is a distinguished IT professional with more than 20 years of experience. He enjoys sharing his ideas at http://projects.privateeyepi.com, where he continues to explore the endless possibilities of this computing platform.

This article can be found in the category: