[App Inventor x Lego EV3 robot] Button control


This tutorial demonstrate how to control your Lego EV3 robot with buttons. There are five buttons to make robot moving forward, backward, right-turn, left-turn and stop. You can also drag the slider to adjust motor power.

Notice: You don’t have to write any program for EV3, just power it on and pair with your Android phone. App Inventor’s EV3 components can talking with Lego EV3 brick with a special protocol called Lego EV3 Direct Command. These commands are actually byte arrays which  can be executed directly by Lego EV3 brick. Lego EV3 components are developed by CAVEDU Education and merged to official AI2 in 2016.

App Inventor Gallery of this project

Reference: Sensor Panel of Lego Robot

Basic dual-wheel robot platform

Please assemble you robot like Figure 1. If your robot is somehow different, don’t worry, just make sure the two motors (attach to port B and C)are on the opposite side of robot. This kind of robot base is called differential drive platform. Because robot’s behavior can be easily adjusted according to the motor speed. For instance, robot will go forward with both motors moving at same speed; and will turn right with left motor moving a little slower than right motor.

This robot need no sensors. Power on your EV3 and pair it with you Android phone (default key: 1234). You can check whether robot’s EV3 is on by the bluetooth icon at the upper-left screen corner.


Figure 1. dual-wheel robot platform

App Inventor


Please add these components and place them as shown in Figure 2. Details please refer to Table 1.

For connection, a listpicker is used to selected paired bluetooth device, your Lego EV3 in this case. A Disconnect button to close the bluetooth connection with Lego EV3.

You can see there are five buttons to make robot move forward, backward, turn left, turn right and stop. We also add a slider to adjust motor power (0~100). Please add more interesting moves to your robot~

Figure 2. Designer

Table 1.  Required components of this project






User Interface

set Text to “Choose your EV3


User Interface

set Text to “Slide to adjust motor power


User Interface



User Interface

set Text to “Forward

Click to make robot go forward.


User Interface

set Text to “Backward

Click to make robot go backward


User Interface

set Text to “Left

Click to make robot turn left.


User Interface

set Text to “Right

Click to make robot turn right.


User Interface

set Text to “Stop

Click to make robot stop.


User Interface

set Text to “Disconnect

Click to disconnect from EV3 brick.



set BluetoothClient to BluetoothClient1

Control and get status of Ev3 Motor, MotorPorts is BC.



Communication between AI2 and Lego EV3 brick


STEP1 Initialize:

First we use a variable (power) to represent motor power, with initial value 100. We will set it’s value in Slider.positionChanged event to further control motor power.

In ListPickerConnect.BeforePicking event, we have to set its element to BluetoothClient1.AddressAndNames. After that we can connect(BluetoothClient1.Connect) our paired EV3 in ListPickerConnect.AfterPicking event. If connect sucessfully, Listpicker will be set to unable, but other button will be set to enabled for user to operate. (Figure 3).ev3002

Figure 3. Initialze and connection

STEP2 Forward and Backward button:

Except for Button Stop, other four buttons will call Ev3Motors1.RotateSyncInDuration command when clicked, with different turnRatio and power. Take Button_Forward for instance, we have these parameters: power variable, 2000(ms), 0 (means two motor with same speed and  true (break when execution complete).

Notice that everytime you click the button will let robot do something for 2 seconds, you can modify the millisecond field or change to RotateSyncIndefinitely command. It will let your robot more gentle。

Figure 4. RotateSyncIndefinitely command

As for Button_Backward, we simply multiply variable power with -1 to make motors turn in the opposite direction (same power). Your robot will then move backward (Figure 5).


Figure 5. Forward and backward.

STEP3 Left, Right turn and stop:

Button_Left and Button_Right are almost the same idea, but this time we set turnRatio to  -200 (turn left) and 200 (turn right). You can set turnRatio to higher value(max is 100) to make a sharper turn.

Finally click Button_Stop to stop the robot, you can use RatateSync command with 0 power to achieve the same effect. (Figure 6).


Figure 6. Turn left, right and stop.

STEP4 Slider to control motor speed

In slider.positionChanged event, we set power variable to slider’s thumb position, therefore the motor power is changed as well (0~100).


圖7  set power variable to slider’s thumb position.

STEP5 Disconnect:

When Button_Disconnect is pressed(Button_Disconnect.Click event), we ask BluetoothClient to disconnect from EV3 brick, set buttons and listpicker to original status, waiting for next connection. Like Figure 7.


Figure 8. Disconnect from bluetooth device


You must pair with your EV3 robot first, then press its power button and check EV3’s BT is switched on. Press the [Choose your EV3] listpicker and choose the name of the paired EV3, then all the control button will be enabled to click. Please click each button to see how robot moves and also drag the slider to adjust motor power. You can build a maze and see who is the best maze conqueror in the class!!

Connect your EV3

App starts up like Figure 9, please click  Connect listpicker and select your EV3 (figure 10). Click and connect successfully, you can click five buttons in the middle of the app to control your robot. Enjoy~

Click Disconnect button to closebluetooth connection with EV3,

Screenshot_2016-04-19-09-29-48-72   Screenshot_2016-04-15-14-50-53-02

Figure9. 10 Start screen and select EV3