Skip to main content

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, and motor, and read the temperature-humidity of the environment. We will use a free MIT App Inventor website for building apps 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 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 making 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 is 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 what 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.

Comments

Popular posts from this blog

Ultrasonic Sensor HC-SR04 With PIC Microcontroller

How Ultrasonic Sensor Works An ultrasonic sensor is a very useful sensor that measures distances with sounds. We used the PIC16F628A microcontroller for this project. This microcontroller has TMR1 which we are going to use. We need 2 I/O pins for TRIG and ECHO pins. For showing distance, we used an LCD. For more information about the LCD library and usage click the button below. LCD Interfacing with PIC Using XC8

How to Make Blink LED with PIC16F628A

How to Make Blink LED With PIC16F628A In this project tutorial, we are going to make blink-led projects with a pic microcontroller. After you read this tutorial you can build a PIC microcontroller project with XC8 on MPLAB IDE. We are going to make 3 different blinking projects. These projects are basic. Just learn how to make a basic project with a PIC microcontroller. You can see below which projects we are going to make; Blinking LED with 1s delay, LED with button, Blinking LED with a button.

DC Motor Speed Control With PIC16F628A

DC Motor Speed Control Simulation In this project, we will control the speed and direction of a DC motor using the PIC16F628A microcontroller. We will write the software in MPLAB IDE with the XC8 language that will send a PWM signal to L293D to control the motor. Finally, we will simulate it through the Proteus software. PWM (Pulse Width Modulation) You can get more information about the PWM signal from the link above. Now let’s examine the L293D motor driver.