Introduction
This project will demonstrate how to update Lego EV3’s 3 color, ultrasonic and touch sensor status on your AI2 screen.
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 are 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.
App Inventor Gallery of this project
HardwareLego EV3 Robot SensorsLego EV3 Robot has 5 different kinds of sensors, including:
- Color Sensor: return light level (0-100) or color code of object ahead.
- Ultrasonic Sensor: return distance (cm/inch).
- IR Sensor: return IR message from IR controller(text and number).
- Touch Sensor: return whether it is pressed or not(logic).
- Gyroscopic Sensor: return robot’s rotational motion and changes in its orientation(number). Angle mode measures angles with an accuracy of +/- 3 degrees Gyro mode has a maximum output of 440 degrees/second. Sample rate of 1 kHz.
Note: There are encoders within EV3’s motors. We’ve included the GetTachoCount function in EV3Motors component. We will discuss this function in later topics.
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.
App InventorThis app will update Lego EV3’s sensor value (color sensor, touch sensor and ultrasonic sensor) value on screen.
DesignerPlease add these components and place them as shown in Figure 2. Details please refer description below:
- ListPicker1: set Text to “Choose your EV3” (empty)
- Button_Touch: set backgroundColor to light gray
- Label1: set Text to “Distance (Ultasonic)”
- TextBox_Ultra: set Text to “” (empty) / Show distance by ultrasonic sensor.
- Label2: set Text to “Light level”
- TextBox_Light: set Text to “” (empty) /Show light intensity by light sensor.
- Button_Disconnect: set Text to “Disconnect” / Click to disconnect from EV3 brick.
- Ev3TouchSensor1: set BluetoothClient to BluetoothClient1 / Get status of Ev3TouchSensor
- Ev3UltrasonicSensor1: set BluetoothClient to BluetoothClient1 / Get status of Ev3UltrasonicSensor
- Ev3ColorSensor1: set BluetoothClient to BluetoothClient1 / Get status of Ev3ColorSensor
- BluetoothClient1: Communication between AI2 and Lego EV3 brick
- Clock1: set timerinterval to 200, means updating 5 times per second.
STEP1 Preparation
First we need to set ListPicker1.Elements to BluetoothClient1.AddressAndNames, therefore we can choose the paired EV3 brick in this ListPicker.
In ListPicker1.Elements.AfterPicking event, we connect with what we chose (ListPicker.Selection). If connect successfully, then activate Clock.Timer and set Screen.Title to “Connected”.
STEP2 Show sensor value regularly
In Clock1.Timer event, we are polling sensors status 5 times per second. Then use each sensor component’s related function to get sensor status. For ultrasonic and color sensor, we simply show the value directly on Textbox. And we have one special button to change its background color according to whether the touch sensor is pressed or not.
STEP3 Disconnect
When Button_Disconnect is pressed(Button_Disconnect.Click event), we ask BluetoothClient to disconnect from EV3 brick, disable Clock Timer and set related message on Screen title.
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, you should see varying number on your screen. Try to press Lego EV3’s touch sensor to check whether the button has been red or not~
Comments