Set motor speed and stopping action


Simulink Support Package for LEGO MINDSTORMS NXT Hardware


This blocks controls the speed, direction, and stopping action of the LEGO 9842 Interactive Servo Motor.

You can use this block with the LEGO MINDSTORMS NXT Encoder block, which outputs the rotation of the servo motor.

The Motor block input value controls the motor speed and direction.

  • Forward: 1 to 100 (slowest to fastest)

  • Reverse: –1 to –100 (slowest to fastest)

  • Stop: 0

Using the Stop action parameter in the block, you can configure the motor to coast or brake when it receives an input of 0 (stop). As a side effect, when you change Stop action from Brake to Coast, the servo motor requires higher block input values to start moving and to maintain speed. The following graph shows this relationship.

This block automatically converts the data type of the data it receives. You do not need to perform data-type conversion on the input signals.

If you run a simulation of a model that contains this block without the target hardware, this block does nothing. See Block Produces Zeros or Does Nothing in Simulation.

Use the Motor

  1. Open the legonxtlib block library and copy the blocks shown to a new model.

  2. Connect the Encoder block to the Motor block and the LCD block, as shown.

  3. Double click the Motor block to open the block mask, and change the NXT brick output port parameter to B.

  4. Connect one servo motor to Port A on the NXT brick. Connect another servo motor to Port B on the NXT brick.

  5. Prepare and run the model on the NXT brick.

  6. Slowly rotate the wheel of servo motor connected to Port A in each direction. Observe the servo motor connected to Port B drive in one direction and then in the other direction as you input positive or negative values. Using the input value displayed on the LCD, observe that values larger than 100 or -100 do not increase the motor speed.

  7. In the Motor block, experiment with changing the Stop action parameter to Coast.

    Run the model on the NXT brick again.

    Observe that the motor requires higher block input values both to start and to achieve the same speed as when the Stop action parameter was set to Brake.


NXT brick output port

Select the NXT brick output port to which the encoder is connected. The options are A, B, or C.

The LEGO 9842 Interactive Servo Motor contains both a motor and an encoder. If your model uses a Motor block and an Encoder block for the same LEGO 9842 Interactive Servo Motor, configure the NXT brick output port parameter in both of those blocks to use the same NXT brick output port.

Stop action

Select the stopping behavior of the motor:

  • Coast stops the motor gradually through loss of momentum. When Stop action is set to coast, getting the motor to start and run requires higher values at the block input than when Stop action is brake.

  • Brake stops the motor immediately.

Was this topic helpful?