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:
- LEGO Mindstorms EV3 (typical UK price: £229.99)
- SanDisk 16 GB MicroSDHC card (typical UK price: £7.99)
- COTOP Card Reader Hub (typical UK price: £16.99)
- 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:
- Run commands from a Python shell on ev3dev
- Write a Python script on ev3dev
- 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
- Start an SSH client, such as PuTTY
- Create a session with:
- Host Name: ev3dev
- Port: 22
- Make the connection, and when prompted authenticate with:
- Username (default): robot
- Password (default): maker
- Start Python with
python3
- In the shell, load the ev3dev library with
from ev3dev.ev3 import *
- Start using commands
Write a Python script on ev3dev
- Start an SSH client, such as PuTTY
- Create a session with:
- Host Name: ev3dev
- Port: 22
- Make the connection, and when prompted authenticate with:
- Username (default): robot
- Password (default): maker
- Start writing a script with
nano example-script.py
- Begin the script with
#!/usr/bin/env python3
- Import the ev3dev library with
from ev3dev.ev3 import *
- Write your Python script
- To exit and save the script, use CTRL + X and select Y. Press Enter when satisfied with the filename.
- Make the script executable through
chmod +x example-script.py
- Execute the script through
python3 example-script.py
Write a Python script on your PC and transfer it to ev3dev
- Start writing a script in your editor of choice, such as IDLE or Atom or Notepad++
- Begin the script with
#!/usr/bin/env python3
- Import the ev3dev library with
from ev3dev.ev3 import *
- Save the script somewhere on your local drive.
- Launch an FTP client, such as WinSCP
- On the programmable brick, go to Wireless and Networks
- Navigate to the network you're connected to
- Select Status
- Note the IP.
- 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
- Once connected, transfer the file(s) to
/home
- Make the script executable through
chmod +x example-script.py
- 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%