Introduction: How to Control Arduino Board Using an Android Phone and a Bluetooth Module
Here is a short video showing an example app I created. I'll describe how to do the wiring, write an arduino sketch that can accept basic commands and send them and how to write the app itself. I asume you're already familiar with some of the basics, you made a few arduino projects and are familiar with the arduino IDE. If you have attempted serial communication with the arduino board everything should be fully comprehensible. If you have any problems or questions feel free to ask them in the comments section.
...and here is an example of receiving the data from the arduino board on the mobile. The temperature sensor connected to the arduino board is a ds18b20 sensor. Just to show how awesome app inventor is I added text to speech functionality - the app says the temperature every 15 seconds. The arduino code and the app is described in more detail in steps 4,5 and 6.
Step 1: Wiring & Part List
The circuit is as simple as it gets so you probably already have most of them.
You will need:
- arduino board
- bluetooth serial module (I used a btm222 module on a breakout board with an inbuilt regulator )
- an LED
- resistor (100ohm)
- wires
- breadboard
The only problematic part here is the bluetooth module. There are different modules all over the internet so be sure you check the pinout in the datasheet of the one you get as it can differ.
Also notice that there are two general classes of bluetooth modules:
Class 1 has range of about 100 meter (300 feet)
Class 2 has range of about 10meter (30 feet)
In case you're wondering they are entirely compatible and you can only get 100 meter range if both of the devices (ie the mobile and the serial module) are class one. If one of it is class 1 the maximum range is lower.
The bluetooth serial module I got has the following pins from left to right (ground, RX, TX, not connected, VCC). Obviously ground and VCC goes respectively to ground and +5V pin on the arduino board. Since we will be receiveing the data through the module and then in turn sending it to the arduino board we only need to use the TX pin on the module. Run a wire from that pin to the RX pin on the arduino board. The Led is controlled through PIN 2 on the arduino.
Step 2: Arduino Code
Below is an arduino code I used. Feel free to modify it.
The important aspect here is the baud rate - make sure it matches the baud rate of your module - check the datasheet or use AT commands to do it.
const int ledPin = 2; // the pin that the LED is attached to
byte serialA;
void setup()
{
// initialize the serial communication:
Serial.begin(19200); //baud rate - make sure it matches that of the module you got:
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {serialA = Serial.read();Serial.println(serialA);}
switch (serialA) {
case 1:
digitalWrite(ledPin, HIGH);
break;
case 2:
digitalWrite(ledPin, LOW);
break;
case 3:digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
default:
break;
}
}
Step 3: The App Itself & App Inventor
If all you want is to get the app and see it running then download the app then upload it to your phone.
Download at -> https://drive.google.com/folderview?id=0B_PfPoEotOF8N2JwT3RSX011SjQ&usp=sharing
Your phone has to be set to allow apps from outside the android market/google play to be able to download them - so check the settings on your mobile.
Now if you want to modify the app go to >>
http://appinventor.mit.edu/explore/learn.html
to find out how to prepare your computer and install App inventor software. Once you have it running I suggest you do at least one or two of their basic tutorials.
Below is the source of the app that I used. You can upload it to the app inventor and then upload to your phone or modify it.
https://drive.google.com/folderview?id=0B_PfPoEotOF8N2JwT3RSX011SjQ&usp=sharing
Step 4: Receiving Data From Arduino
This step presents an example on how to receive data from the arduino board.
I decided to make something useful so I chose a ds18b20 temperature sensor. The arduino board communicates with the sensor using 1 wire interface, calculates the temperature with the help of OneWire library for arduino and sends the readings through the bluetooth module every 500 ms.
The app checks every 500 ms if there is any data available from the serial port. If data is present it is read and displayed on the screen. Additionally, there is an option to activate text to speech function and make the app say the temperature readings every 15 seconds.
Step 5: Remote Sensor - Arduino Code
The arduino code
Mind you that you need a OneWire library - you can find a link to it here:
http://playground.arduino.cc/Learning/OneWire
arduino code >>>>>>>
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup()
{
// initialize the serial communication:
Serial.begin(19200);
// initialize the ledPin as an output:
}
void loop() {
float temperature = getTemp();
Serial.println(temperature); delay (500);
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -100;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) {
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB);
float TemperatureSum = tempRead / 16;
return TemperatureSum;}
Step 6: Receiving Data - the Application Side
Here you can find links to the app that receives temperature reading. There is also a source code that can be uploaded into MIT's app inventor (when you're in 'my projects' click the button 'More Actions' - you have an option 'Upload Source')
feel free to modify it ;-)
The application >>
http://speedy.sh/RMFgg/bluetoothforarduino-app-temperaturereadings.apk
The source file >>
http://speedy.sh/SHpjj/bluetoothforarduino-app-temperaturereadings-3.zip
I'll try to add something more descriptive soon, but here are some tips you might find useful if you're trying to create your own app: Make sure you understand the terms 'delimiter byte', know how to set it from MIT's app inventor and that it is a byte so be careful what data types you use. Also it seems that there is no serial timeout function implemented in the app inventor so sending data more often than receiving it can cause the app to crash (probably because there isn't anything to flush the buffer)
190 Comments
5 years ago
Dear Sir
many thanks for your valuable site
is there any simple project for one led web control using meag2560+enc28c60 Ethernet module knowing that i ha succeed to run two leds using arduino uno with the same Ethernet module but regarding mega2560 i was unable to find any simple project on the web unless using an ethernet module equipped with an SD card which i don't have for now.
thanks
6 years ago
Hey bro. I have an problem regarding apk file for my arduino uno board i had make an apk file to control my arduino board using Bluetooth hc5.. The app is working only for 9 appliance not for the number 10,11,12..please help me i want to control more than 9 appliance in my home please help me
6 years ago
* I know this will sound very simple and maybe stupid to some users but i really need some help and simple instructions on making this project successful I really do not know much about bluetooth and electronics etc but i hope this makes since and would appreciate ANY HELP!
I want to build a device that can communicate via bluetooth with a simple off and on command to activate a motor full throttle.I have purchased a HC-05 model and need to know do i need a arduino board in order to program it to communicate with an android BT device.
* I will have a simple switch connected to a AAA battery holder connected to a simple 3v motor and need to add the bluetooth device within the circuit so that when the switch is on i can connect to the bluetooth model via and android app and be able to turn the motor off and on remotely.
*will this even work? wouldi have any voltage issues to where i would need to add a resistor? would the power from the AAA batter be to powerful?
* most importantly do I need an BOARD to use the bluetooth device or can I use an existing BT android app to control it?
6 years ago
Can we control four motors via motor shield with this module? If not then please suggest a way.
6 years ago
the link you provided to convert to aia file doesn't work do you have another one?
7 years ago
Hey man thanks for the awesome tutorial and package of files. Really helped me figure the way through the MIT app inventor and bluetooth interface around it !!!!
7 years ago
Hello,
I am trying to connect my smartphone with a genuino 101 board with bluetooth but I can't. Two devices are in pair and I put this code:
#include <Servo.h>
#include <CurieBLE.h>
#include<BLEPeripheral.h>
#include<BLEService.h>
BLEPeripheral blePeripheral;
Servo servos[13];
// creació del objecte servo per controlar els servos
void waitForInput(){
while(Serial.available()==0){
delay(500);
}
while(Serial.available()>0){
char incomingChar= Serial.read();
manageChar (incomingChar);
}
}
void manageChar (char c){
switch(c){
...
}
}
void setup() {
servos[3].attach(3);
servos[4].attach(4);
Serial.begin(19200);
blePeripheral.setLocalName ("LEDCB");
blePeripheral.begin();
}
void loop() {
waitForInput();
}
Could anyone help me?
Thaks a lot
Daniel
I am tryiy
7 years ago
i am here using MLX90614 instead of your temperature detector.
will this code will work fine for me??
7 years ago
Hi,
I followed the guidance step by step several times. The end result is always the same-- android is paired with BT, but when I want to connect the app, I always get error 507. I am using BT hm-10 and braud 9600
I laso tried with dc motors--the same result.
I also made my own app at appinventor--still error 507.
Could anyone guide me, how I could find my mistake?
I foll
Reply 7 years ago
hi the baud rate u are using might ne the problem the bluetooth module uses a different baud rate . i am not sure but i think its 38400. for more info i suggest you look at the data sheets
Reply 7 years ago
Hey RasmusHD,
HM-10 is bluetooth 4.0 module. It is not compatible with APP Inventor which works only with bluetooth 2.0.
Check out HC-06 or HC-06 they should work just fine.
Reply 7 years ago
I think you meant HC-05 or HC-06, non?
Be aware that the HC-06 is a slave unit and only receives BT. The HC-05 talks as well as listens and is best for most project methinks. Even if this project only listens, your next project may require the added direction. Street pricing for the units is within a couple of USD of each other. There are Arduino coding differences between the units, so I would opt to learn using the more flexible part.
Also, keep in mind that MIT's App Designer (MAD) which is great for a beginner's two-button app such as this one, is Android only and if you have *any* aspirations for using a different BT device, you might be better off building the phone-side app with a cross-platform tool, such as Apache Cordova
(formerly PhoneGap).
Just my tuppence...YMMV
Reply 7 years ago
HC-06 can talk and recieve! The fact that it is SLAVE only means that it cannot be used as a HOST device to which other devices are connecting. Still as a SLAVE connected to other HOS such as Android device, the HC-06 is able to communicate both directions - send and recieve data.
Reply 7 years ago
Thanks for that info. Back to the data sheets...
7 years ago
Do you also have the app inventor aia-file?
Reply 7 years ago
Download the .zip file from the google drive link provided above.
Since there is new version of app inventor, use this site
http://convert.appinventor.mit.edu/
to upload the .zip obtained(app inventor 1 - called classic) and it will return you aia-file compatible with the new version - app inventor 2.
There is a format change in the blocks file *.blk -> *.bky and this site will do the transform. Without this, there will be no blocks displayed after upload.
7 years ago
if i got HC-06 that works on 9600 bauds... in this case, should I change my BT bauds to 19200 or just change the arduino code to serial.begin(9600)??
7 years ago
dont know why I get RUNTIME ERROR
Need BLUETOOTH_ADMIN permission: Neither user 10064 nor current process has android.permission.BLUETOOTH_ADMIN.
Im using HC-06...
8 years ago on Introduction
Hola, como termina el diseño de la aplicación en App inventor porque en la imagen en la parte baja no se alcanza a ver?
Gracias
Reply 7 years ago
Si hace clic una vez en la imagen aparecerá una ventana con sólo la imagen. Haga clic una vez en esa imagen y verá una página con opciones de tamaño a la izquierda.
Perdonen mi pobre español.