Categories
Arduino

How To Make Android App To Connect Arduino via Bluetooth

In this project, we are going to make an Android App that will connect with Arduino Nano via the HC-05 Bluetooth module. This app will control the LED, motor, and read the temperature-humidity of the environment. We will use a free MIT App Inventor website for building app via blocks. We will give all the project files for this project. If you are ready let’s get started.

Schematic

Firstly we made an example circuit to make bidirectional communication. You can see in the picture below which components we use. The main component is Arduino Nano which controls all the system. We connect the red LED to the D2 pin. We use the L293D motor driver to control the DC motor. We added AHT10 to get temperature and humidity from the environment. Finally, we added HC-05 to communicate with the Android App.

Circuit Schematic

Android App

We made an Android App via MIT App Inventor which is easy to make basic apps. This app will get and send data via Bluetooth. Let’s start to explain what we did.

App GUI Design

You can see what we add to the GUI in the picture below. These basic components were used to control LED-Motor and get sensor data. On the top of the screen, we added a list picker and button components for Bluetooth connectivity.

MIT App Inventor Screen Viewer

App Blocks

In the MIT App Inventor, we use blocks to make programs.

Firstly we made some initialization blocks. In this block, we disabled all components excluding the Bluetooth list picker.

After this, we added two blocks to get Bluetooth elements before picking and make the Connect button enabled after selection.

We added some logic blocks after the user pressed the Connect or Disconnect button. We use the same button to connect and disconnect the Bluetooth. When the Connect button pressed this button will convert to the Disconnect button.

We used a procedure (function) to send commands and data to the Arduino. This procedure will make our blocks less complex.

After that, we added some commands after the motor direction is selected. This block will send a direction or the motor to the Arduino. There are 3 direction commands. They are; clockwise, counterclockwise, and stop.

To change motor speed we use a slider. This block will send the SPD command to the Arduino.

For the LED control we used a switch. This block will send the state of the LED either on or of.

Finally, we added the clock to get sensor data from the Arduino. Clock interval set to 500ms. So, we get sensor data for every 500ms.

App Screenshots

You can see how it looks like on the phone in the pictures below.

Arduino Software

Arduino will get and send data from/to Android App. We added explanations in the code. You can read them and make sense of how this code works.

#include <SoftwareSerial.h>
#include <Adafruit_AHTX0.h>

// Pins
#define LED           2
#define IN1           4
#define IN2           5
#define EN12          6

// Modes
#define RESET_MODE    -2
#define READ_MODE     -1
#define SELECT_MODE   0
#define LED_MODE      1
#define DIR_MODE      2
#define SPD_MODE      3
#define TMP_MODE      4
#define HUM_MODE      5

// Variables
sensors_event_t humidity, temp;
int mode = -2, counter = 0;
int dataStr[10];
String str = "";
char ledOnOff = 0, motorDir = 0, motorSpeed = 0, tempSend = 0, humSend = 0;

SoftwareSerial bluetooth(9, 10); // TX of Bluetooth, RX of Bluetooth
Adafruit_AHTX0 aht;

void setup() {
  // Set baud rates
  Serial.begin(9600);
  bluetooth.begin(38400);

  // Set pin modes
  pinMode(LED, OUTPUT); // LED
  pinMode(IN1, OUTPUT); // IN1
  pinMode(IN2, OUTPUT); // IN2
  pinMode(EN12, OUTPUT); //EN12

  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  Serial.println("AHT10 or AHT20 found");
}

void loop () {
  // Get Bluetooth data
  if (bluetooth.available())
  {
    // Read avaliable data
    char data = bluetooth.read();
    // Check for start character
    if (mode == RESET_MODE) {
      if (data == '$') {
        str = "";
        mode = READ_MODE;
        counter = 0;
      }
    }
    // Read mode
    else if (mode == READ_MODE) {
      // Check for end character
      if (data == '#' && counter == 3) {
        mode = SELECT_MODE;
        // Reset counter
        counter = 0;
      }
      // Get mode characters and add to str
      else {
        str += data;
        counter++;
      }
      // Select mode
      if (mode == SELECT_MODE) {
        if (str == "LED")
          mode = LED_MODE;
        else if (str == "DIR")
          mode = DIR_MODE;
        else if (str == "SPD")
          mode = SPD_MODE;
        else if (str == "TMP")
          mode = TMP_MODE;
        else if (str == "HUM")
          mode = HUM_MODE;
        else
          mode = RESET_MODE;
      }
    } else if (mode == LED_MODE) {
      ledOnOff = data;
      mode = RESET_MODE;
    } else if (mode == DIR_MODE) {
      motorDir = data;
      mode = RESET_MODE;
    } else if (mode == SPD_MODE) {
      motorSpeed = data;
      mode = RESET_MODE;
    } else if (mode == TMP_MODE) {
      tempSend = 1;
      mode = RESET_MODE;
    } else if (mode == HUM_MODE) {
      humSend = 1;
      mode = RESET_MODE;
    } else {
      mode = RESET_MODE;
    }
  }

  // LED control
  if (ledOnOff == 1) {
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }

  // Motor direction control
  if (motorDir == 1) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
  }
  else if (motorDir == 2) {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
  }
  else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }

  // Motor speed control
  analogWrite(EN12, motorSpeed);
  //Serial.println(motorSpeed);

  // Temperature send mode
  if (tempSend == 1) {
    // Read and calculate temperature
    aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
    //Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");

    int tempNum = round(temp.temperature);
    bluetooth.write(tempNum & 0xFF);
    // Clear temperature send mode
    tempSend = 0;
  }

  // Humidity send mode
  if (humSend == 1) {
    // Read and calculate humidity
    aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
    //Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");

    int humNum = round(humidity.relative_humidity);
    bluetooth.write(humNum & 0xFF);
    // Clear humidity send mode
    humSend = 0;
  }
}

Working Video

Finally, you can see how it works in real.

Share This Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *