Building a Mobile App to Control Arduino Board Via Bluetooth
by arduinocelentano in Teachers > 7
9559 Views, 29 Favorites, 0 Comments
Building a Mobile App to Control Arduino Board Via Bluetooth
In this instructable I'll show you how to build a custom mobile app to control your Arduino project via Bluetooth. You don't need mobile programming experience to start since a visual App Inventor IDE will be used.
This is an easy to follow step-by-step tutorial to engage students who are not very familiar with programming. It is ideal for Hour of Code or other short coding activities. I tried to keep the Arduino sketch and the mobile app as compact as possible, yet sufficient to demonstrate the concept. You could easily extend them to control motors, visualize sensors data on your smartphone etc. If you need an inspiration, scroll to the last step.
Supplies
- Arduino board. Most of them would work. I used Arduino UNO. Serial interface will be used.
- Bluetooth module. HC-06 or HC-05.
- Wires.
- Android device with camera, Internet access and QR-code scanner.
- A computer or laptop with Arduino IDE and web browser.
Wiring
Upload the attached sketch to Arduino board and connect a Bluetooth module to your Arduino board. Note that TX is connected to RX and vise versa. The sketch should be uploaded BEFORE wiring since Arduino uses the same serial interface to communicate with HC-06 and for sketch uploading.
int val; int LED = 13;//built-in LED void setup() { Serial.begin(9600);//Opening serial port pinMode(LED, OUTPUT);//Initializing LED pin digitalWrite(LED, HIGH);//Turning built-in LED on } void loop() { if (Serial.available())//If connected { val = Serial.read(); if (val == '1')// If "1" received, turning LED on { digitalWrite(LED, HIGH); Serial.println("LED: ON");//Sending the confirmation } else if ( val == '0')// If "0" received, turning LED on { digitalWrite(LED, LOW); Serial.println("LED: OFF");//Sending the confirmation } else { Serial.print("Unknown command");//Sending a message about unsupported commands Serial.println(val); } } }
The protocol is very simple. There are two supported one-character commands, '0' and '1' to turn built-in LED off and an. Read the comments for further explanations. In the following steps we'll create a mobile app to send these commands.
Downloads
New Project
Register on http://ai2.appinventor.mit.edu or sign in with your Google account and create new project.
Vertical Arrangement
Drag a VerticalArrangement onto app's main screen and make it fill all available space. It will align all the controls vertically.
List Picker
Drad a ListPicker onto Layout. This widget will be used for Bluetooth device selection.
Bluetooth Client
Drag a BluetoothClient onto your screen. This invisible object will be used to control Bluetooth communication.
Coding
Switch to Blocks mode and create code for available devices list generation.
First Run
Now you should be able to upload your app to Android device. The easiest way is to generate QR code and download apk file. First go to Anrdoid Bluetooth settings and allow connecting to HC-06. If it asks password, the default one is "1234". Now you could run your app and open devices list. Make sure your HC-06 module is available there.
"Connect" Button
Return to Designer mode and drag Button just below the ListPicker. Make it fill all the screen width and call it "Connect".
Label
Drag a Label just below your "Connect" Button. The label will be used for connection status indicating. Make it red and give it a text value "Disconnected".
More Coding
Switch to Blocks mode and add Button.Click event handler. It will try to establish connection with the device user selected in the ListPicker. It will modify the Label's text and color if the attempt was successful.
Second Run
Now you should be able to download your app and make sure you are able to connect to HC-06 module.
Switch
Return to Designer mode and drag Switch between your Button and Label. We'll use it to control the Arduino LED.
Final Coding
Switch to Blocks mode and add Switch.Changed event handler. It will send '0' or '1' commands t the board. Note that we used ASCII codes of these characters.
Testing
Now you may download your final app and test it.
Another Hour of Code
If you have extra time, you could extend this project. For example, this mobile app is based on my initial example. It reads data from DS18B20 temperature sensor and draws a plot on Canvas. The sketch has to be modified too.
#include <OneWire.h> //a library for 1-Wire interface OneWire ds(2); // OneWire object to interact with DS18B20 sensor void setup(){ Serial.begin(9600); } void loop(){ // reading temperature from DS18b20 byte data[2]; // an aray for storing temperature value ds.reset(); // reset the sensor ds.write(0xCC); // command for searching DS18b20 by address ds.write(0x44); // command to make DS18b20 measure temerature and store it in its internal memory delay(1000); // giving DS18b20 some time to process our command ds.reset(); // getting ready for temperature reading ds.write(0xCC); ds.write(0xBE); // command to make DS18b20 send us data from its internal memory registers // reading the response data[0] = ds.read(); // reading the minor byte data[1] = ds.read(); // and the major byte // merging them to one value // 0,0625 is sensor's resolution float temperature = ((data[1] << 8) | data[0]) * 0.0625; // sending obtained value to the serial port Serial.println(temperature); }