How to make bluetooth app for arduino projects

Introduction

One of the coolest parts of building an Arduino robot or home automation system is controlling it from your own smartphone. While there are many generic Bluetooth apps on the Play Store, they don't always look or work exactly how you want.

In this guide, I will show you how to design and build your very own Android App using MIT App Inventor. We will create an app with buttons to control an Arduino project wirelessly.



What is MIT App Inventor?

MIT App Inventor is a web-based platform that allows you to create Android apps using a "Block-based" programming language. Instead of typing lines of code, you drag and drop blocks (similar to Scratch). It is perfect for beginners and works seamlessly with Bluetooth modules like the HC-05 and HC-06.



Designing the User Interface (UI)

  1. Go to MIT App Inventor and start a new project.
  2. Add a ListPicker: This will be your "Connect" button. Rename it to Connect_BT.
  3. Add Buttons: Drag two buttons onto the screen. One for "ON" and one for "OFF".
  4. Add a Label: Use this to show the connection status (e.g., "Disconnected").
  5. Non-Visible Components: From the "Connectivity" tab, drag a BluetoothClient onto the viewer. From the "Sensors" tab, drag a Clock.



The Logic (Block Programming)

Now we need to tell the app what to do. Switch from the "Designer" view to the "Blocks" view.

A. Scanning for Devices

We need to populate the ListPicker with the Bluetooth devices paired with your phone.

  • Before Picking: Set ListPicker.Elements to BluetoothClient.AddressesAndNames.
  • After Picking: Set BluetoothClient.Connect to ListPicker.Selection.

B. Sending Data to Arduino

This is the most important part. We want the app to send a character when a button is pressed.

  • Button_ON Click: Use BluetoothClient.SendText and attach a text block with the letter "1".
  • Button_OFF Click: Use BluetoothClient.SendText and attach a text block with the letter "0".



Preparing Circuit




Installing the App on Your Phone

  1. In MIT App Inventor, go to Build > App (.apk).
  2. Once the QR code appears, scan it with your phone to download the file.
  3. Install the APK (you may need to allow "Install from Unknown Sources" in your phone settings).
  4. Pair your HC-05 module in your phone's Bluetooth settings before opening the app.

.apk File ðŸ‘‡



Understanding the Code: The Bluetooth "Listener"

This sketch is a simple yet powerful example of Wireless Serial Communication. It tells the Arduino to sit in a constant loop, listening for specific "characters" sent from your custom smartphone app.

1. Defining the Variable char t

We start by declaring a char (character) variable named t. Since Bluetooth communication sends data one byte at a time, a char is the perfect data type to hold single letters or numbers like '1' or '0'.

2. The setup() Configuration

  • pinMode(13, OUTPUT): We use Pin 13 because most Arduino boards have a built-in LED connected to this pin. It allows you to test your app immediately without even using a breadboard.
  • Serial.begin(9600): This opens the communication line at 9600 bits per second. This is the "Baud Rate" that almost all Bluetooth modules (HC-05/HC-06) use by default.

3. Listening for Data (Serial.available)

The loop() function runs thousands of times per second.

  • if(Serial.available()): This line asks, "Is there any data waiting in the buffer?"
  • t = Serial.read(): If data exists, the Arduino reads that character and saves it into our variable t.
  • Serial.println(t): This "echoes" the command back to your Serial Monitor. It is a great way to verify that your phone is actually talking to your Arduino.

4. Execution Logic

The Arduino then compares the value of t to our defined commands:

  • The '1' Command: If the app sends '1', Pin 13 goes HIGH, and the LED turns on.
  • The '0' Command: If the app sends '0', Pin 13 goes LOW, and the LED turns off.

5. Pro-Tip: The "Case-Sensitive" Trap

Computers see '1' and '1' as the same, but they see 'A' and 'a' as completely different things. When designing your app in MIT App Inventor, ensure the text you send matches exactly what is in your code. If your code looks for '1' but your app sends 'One', the Arduino will ignore the command!

CODE

char t;

void setup(){
 pinMode(13,OUTPUT);
 Serial.begin(9600);
}

void loop() { 
 if(Serial.available()){
    t=Serial.read();
    Serial.println(t);
 }
 if(t=='1'){
  digitalWrite(13,HIGH);
 }
 else if(t=='0'){
  digitalWrite(13,LOW);
  }
} 


Troubleshooting: Common App Errors

1. "Error 507: Is the device turned on?"

This means the app tried to send data but isn't connected to the Bluetooth module. Make sure you have selected your HC-05 from the ListPicker first.

2. The App Crashes on Startup

Ensure you have granted the app Bluetooth Permissions. On newer Android versions, you must also enable Location Services (GPS) for Bluetooth scanning to work.

3. Data is Sent but Arduino Doesn't Respond

  • Check your baud rate. Both the app and Serial.begin must be at 9600.
  • Ensure your HC-05 TX/RX pins are connected correctly (TX to RX, RX to TX).



Conclusion

Creating your own app gives you full control over your Arduino projects. You can add sliders to control motor speed, voice recognition to give commands, or even a canvas to draw patterns that your robot will follow. The possibilities are endless!

Post a Comment

Post a Comment (0)

Previous Post Next Post