[App Inventor and Arduino] Lesson 3: Analog read potentiometer

App Inventor and Arduino: Lesson 3: Android phone received Arduino’s value of analog pins through Bluetooth

This article is going to teach you how to use your Android phone to read Arduino’s value of analog pins. In this case, we will connect the rotary variable resistor. In fact, not only the variable resistor, but also all analogical sensors can be used in this kind of experiment.


file name:ReceiveDatafromArduino.aia

Part List

1. Android phone (I think 1.6 or above are all OK) (will be fine).

2. Arduino and Arduino compatible board (We use Arduino MEGA2560, but Arduino UNO will be 100% fine)

3. Bluetooth module (We use JY-MCU04 or JY-MCU05 here)

4. Breadboard, x1

5. 1K ohm resistor, x1

Hardware connection:

Use the following diagram to connect the bluetooth and variable resistor to Arduino.

First, please connect the Bluetooth module to your Arduino board, since we are using SoftwareSerilal library, we are now using RX/TX of Arduino (digital pin #0, #1) And then, please connect module's RX pin to Arduino's digital pin #11, and module's TX pin to Arduino's digital pin #10. A

And for variable resistor. Here, we got three wires. The first goes to ground from one of the outer pins of the variable resistor(GND). The second goes to 5 volts from the other outer pin of the variable resistor(5V). The third goes from the middle pin of the variable resistor to analog input 0(A0).

螢幕快照 2016-04-06 下午1.10.03.png

App Inventor Designer:

it’s quite easy interface with one of ListPicker, one TextBox and one Button components. And one Bluetooth client components for Bluetooth communication. Please check the description below:

1. BTList (LIstPicker): It shows all the paired devices and the app will connect to the device that user selected.

2. (TextBox):It shows the information of Arduino analog pins.

3. Disconnect (Button): To disconnect from Arduino.

Note: Please change Clock1’s TimerInterval to 10, which updates every 0.01 seconds. As a result, it won’t miss the information signal of Arduino.螢幕快照 2016-04-06 下午1.09.40.png

How to play:

First you must pair the Bluetooth module with your Android phone. After you connect all the hardware, you can see that the Bluetooth module's red LED is flashing, means ready for connecting. Then please open your Android phone's Bluetooth setting page (which should under Settings... , differs on each phone), click the name of your module (something like HC-05). Please enter 1234 (or 0000) when there is a prompt asking the paring key. Done~

There is only BTList which is enabled when you first open the app. Please click BTList, it will show all paired Bluetooth devices on your phone.


Please click the name of your module, it should take 1~2 seconds to connect and back the main screen. You can see drag the slider from left to right, you should see LED is fading. Please click the [Disconnect] button when you don't want to play anymore, this will disconnect the connection of your phone and bluetooth module and the module's LED will flashing again.

App Inventor Blocks:

STEP1: Initialize global “number” to “0”, and global “text” to “  ”.

STEP2: Within Screen1.Initialize event, only BTList is enabled, and other textbox and button are both disabled. What’s more, the clock’s timer is disabled as well.  It enforces the user to select the device they want to use, otherwise, sending out Bluetooth message without connecting will result in errors on your screen (annoying...).

STEP3: Within ListPicker1.BeforePicking event, we set the BTList.Elements to BluetoothClient1.AddressAndNames. Therefore we can put paired devices pist into this ListPicker.

STEP4: Within ListPicker1.AfterPicking event, we first use an if statement to check whether the connection is established (BluetoothClient1.Connect -> BTList.Selection). If yes, then flip the textbox ,button and clock’s timer to be enabled, BTList to be disabled. (You CAN NOT connect again while you are connected, right? )

螢幕快照 2016-04-06 下午1.09.06.png

STEP5: In Clock.Timer event, it will send a confirmation code, which will be“49”. After that, Arduino will receive this data and start processing to transfer data.

The range of Arduino A0 pin’s values are 0 to 1023, but the packet is 8 bits (1 Byte) per unit, so we should combine the value that is set to be 8 bits per unit, and stored in “global number”.

Finally, you will see the global number displayed on TextBox.


STEP6: Click Disconnect button will disconnect the connection of your phone and bluetooth module. And flip all visible components to their initial settings.

Arduino sketch:

Arduino code is quite straightforward as well. First import necessary libraries: SoftwareSerial and Wire, and define digital pins 11, 10 as RX, TX pins.

In setup(), we set the communication speed of Serial Monitor (USB, 9600) and Arduino (Bluetooth, 9600).

In loop(), first we set i = analogRead(A0), which reads sensor’s value. And an array Data[] will save ‘a’, i/256 and i%256. If the serialA gets the number 49, the I2CBT will send back the datas of array Data[].

螢幕快照 2016-04-17 下午7.39.44.png