Incomplete Guide
This page is incomplete to cover all the functionalities of the motors. Please navigate to see the JavaDoc which has a completed API documentation.
Introduction to LegoPort
To begin with any library Device child classes, it is required to create a LegoPort instance to point the connected input/output port to the sensor/motor. Here is a list of constant fields of inputs LegoPort.INPUT_* and outputs LegoPort.OUTPUT_*
  • LegoPort.INPUT_1 for input/sensor port 1
  • LegoPort.INPUT_2 for input/sensor port 2
  • LegoPort.INPUT_3 for input/sensor port 3
  • LegoPort.INPUT_4 for input/sensor port 4
  • LegoPort.OUTPUT_A for output/motor port A
  • LegoPort.OUTPUT_B for output/motor port B
  • LegoPort.OUTPUT_C for output/motor port C
  • LegoPort.OUTPUT_D for output/motor port D
An example to list information of a LegoPort.OUTPUT_A port:
package org.ev3dev.examples;
import org.ev3dev.hardware.LegoPort; //Necessary class LegoPort
import java.util.Arrays; //Just for this example to list port's modes

public class LegoPortDemo{
    public static void main(String[] args){
        try {
             LegoPort port = new LegoPort(LegoPort.OUTPUT_A); //Creates an instance pointing to Output A
             System.out.println("Address: " + port.getAddress()); //Outputs the port address
             System.out.println("Driver: " + port.getDriverName()); //Outputs the port driver (current)
             System.out.println("Modes: " + Arrays.deepToString(port.getModes())); //Outputs a list of modes supported by this port
             System.out.println("Current Mode: " + port.getMode());//Outputs the current mode
             System.out.println("Status: " + port.getStatus()); //Outputs the port status
             //Set Mode (Depends on the function, port.getModes())
             //Set device, writing the name of a driver
        catch (InvalidPortException e){ //Optional, Only thrown if the specified argument is invalid
Controlling Motors
Each Motor instances and its childs require the developer to specify a LegoPort instance to let the Motor to know which port and device is pointing to.
LegoPort port = new LegoPort(LegoPort.OUTPUT_A); //Output A
Motor motor = new Motor(port); //Creates a Motor instance

//or (it is the same as)

Motor motor2 = new Motor(new LegoPort(LegoPort.OUTPUT_B); //Motor with Output B

//or (1.0.1 or older)

Motor motor3 = new Motor(LegoPort.OUTPUT_C);
The motor can be commanded with the following methods:
  • motor.runForever()
    • Causes the motor to run util another command is sent
  • motor.runToAbsPos()
    • Runs to an absolute position specified by setPosition_SP(int pos)
  • motor.runToRelPos()
    • Runs to a a position relative to the current position value
  • motor.runTimed()
    • Runs the motor for the amount of time specified in setTime_SP(int ms)
  • motor.runDirect()
    • Runs the motor at the duty cycle specified by setDutyCycle_SP(int dutycycle_sp)
  • motor.stop()
    • Stops any of the run commands before they are complete using the command specified by setStopCommand(String command)
  • motor.reset()
    • Resets all of the motor parameter attributes to their default value. This will also have the effect of stopping the motor.
An example to run a motor for 5000 ms (5 seconds):
package org.ev3dev.examples;
import org.ev3dev.hardware.LegoPort; //Necessary class LegoPort
import org.ev3dev.hardware.Motor; //Necessary class Motor
import java.util.Arrays; //Just for this example to list port's modes

public class MotorDemo{
    public static void main(String[] args) throws IOException, InvalidException{
        LegoPort port = new LegoPort(LegoPort.OUTPUT_A); //Output/Motor Port A
        Motor motor = new Motor(port);
        motor.setTime_SP(5000); //Set time to 5000 ms
        motor.setDutyCycle_SP(50); //Set speed to 50
        motor.runTimed(); //Send command