stack twitter tryhackme rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Basic process for running Python scripts on the LEGO Mindstorms EV3

    The official ev3dev Getting Started guide provides very clear and thorough instructions on how to load ev3dev (an OS based on Debian Linux which comes with Python bindings included) on your EV3.

    Example setup:

    1. LEGO Mindstorms EV3 (typical UK price: £229.99)
    2. SanDisk 16 GB MicroSDHC card (typical UK price: £7.99)
    3. COTOP Card Reader Hub (typical UK price: £16.99)
    4. TP-Link TL-WN821N Wi-Fi dongle (typical UK price: £9.87)

    Once you're set up with the EV3 on, ev3dev booted, and a connection established (whether it be Ethernet, Wi-Fi, or Bluetooth), you have three options:

    1. Run commands from a Python shell on ev3dev
    2. Write a Python script on ev3dev
    3. Write a Python script on your PC and transfer it to ev3dev

    Let's go over the three options.

    Run commands from a Python shell on ev3dev

    1. Start an SSH client, such as PuTTY
    2. Create a session with:
    • Host Name: ev3dev
    • Port: 22
    1. Make the connection, and when prompted authenticate with:
    • Username (default): robot
    • Password (default): maker
    1. Start Python with python3
    2. In the shell, load the ev3dev library with from ev3dev.ev3 import *
    3. Start using commands

    Write a Python script on ev3dev

    1. Start an SSH client, such as PuTTY
    2. Create a session with:
    • Host Name: ev3dev
    • Port: 22
    1. Make the connection, and when prompted authenticate with:
    • Username (default): robot
    • Password (default): maker
    1. Start writing a script with nano example-script.py
    2. Begin the script with #!/usr/bin/env python3
    3. Import the ev3dev library with from ev3dev.ev3 import *
    4. Write your Python script
    5. To exit and save the script, use CTRL + X and select Y. Press Enter when satisfied with the filename.
    6. Make the script executable through chmod +x example-script.py
    7. Execute the script through python3 example-script.py

    Write a Python script on your PC and transfer it to ev3dev

    1. Start writing a script in your editor of choice, such as IDLE or Atom or Notepad++
    2. Begin the script with #!/usr/bin/env python3
    3. Import the ev3dev library with from ev3dev.ev3 import *
    4. Save the script somewhere on your local drive.
    5. Launch an FTP client, such as WinSCP
    6. On the programmable brick, go to Wireless and Networks
    7. Navigate to the network you're connected to
    8. Select Status
    9. Note the IP.
    10. In your FTP client, create a new connection with:
    • File protocol: SFTP
    • Host name: the IP you noted down
    • Port number: 22
    • Username (default): robot
    • Username (default): maker
    1. Once connected, transfer the file(s) to /home
    2. Make the script executable through chmod +x example-script.py
    3. Execute the script through python3 example-script.py

    Example commands

    Assigning the EV3 components to variables

    ts = TouchSensor()
    cs = ColorSensor()
    us = UltrasonicSensor() # always to be connected to `in4`
    ir = InfraredSensor()
    gs = GyroSensor()
    
    l1 = LargeMotor('outC') # if connected to `outC`
    l2 = LargeMotor('outD') # if connected to `outD`
    mm = MediumMotor('outA') # if connected to `outA`

    Text-to-speech

    ev3.Sound.speak('Hello. I am a robot.').wait()

    Keep active until cancelled

    while True:
      # code

    Run a motor for a specific time at a specific speed

    m.run_timed(time_sp = 5000, speed_sp = 500)
    • time_sp: time in milliseconds
    • speed_sp: tacho ticks per second = 1.4 rotations per second = 1 tick per degree = 500 degrees per second

    Keep running motor

    m.run_forever(speed_sp = 900)

    Run multiple motors at once

    l1 = LargeMotor('outC')
    l2 = LargeMotor('outD')
    motors = [l1, l2]
    
    for m in motors:
      m.run_forever(speed_sp = 200)

    Stop motors

    m.stop(stop_action = "coast")

    Save debugging messages to a log

    import logging
    
    logging.basicConfig(filename='runtime.log',
    level=logging.DEBUG)
    
    if do_something():
      logging.debug("Starting to do something.")

    Shell commands

    Get kernel version

    uname -r
    

    The uname command reports basic information about a computer's software and hardware. The -r option gives the kernel version number and release level.

    Or, in Brickman, select About and scroll down.

    Get volume

    amixer get Playback,0
    

    Change volume

    amixer set Playback,0 100%