Program Arduino Over Bluetooth

Harness the power of Bluetooth to communicate with your Arduino!

1-16 of over 1,000 results for 'arduino bluetooth' Customers ultimately bought. Best Selling • Top Rated. Elegoo EL-KIT-003 UNO Project Super Starter Kit with Tutorial for Arduino. $35.00 $ 35 00 Prime. 4.5 out of 5 stars 819. Lowest Priced. Step #8: Program the Arduino With Bluetooth! Power on the Arduino from a battery or AC adapter. In the Arduino IDE, choose the serial port of the Bluetooth module (the lower valued one of the two(21 and 25) and then upload a sketch like normal.

This is part two of a series, which will focus on using Bluetooth to communicate with an Arduino. Bluetooth is one of the popular wireless communication technologies because of its low power consumption, low cost and a light stack but compensates on range.

Requirements

  • An Arduino
    Any model of the Arduino can be used, but all code and schematics in this article will be for the Uno.
  • An Android Smartphone with Bluetooth
    Check if your phone has Bluetooth by navigating to 'Wireless and Networks' under settings.

  • HC-05 Bluetooth Module
    This module is the most popular Bluetooth module out there. The basic version is pretty inexpensive. The hassle of having to connect wires between the module and the Arduino can be avoided by using a Bluetooth Shield or a version in the Bee shape, the BTBee and a Bee shield.

Setting Up the Hardware

Program Arduino Over Bluetooth

To use the HC05 module, simply connect the VCC to the 5V output on the Arduino, GND to Ground, RX to TX pin of the Arduino, and vice versa. [Editor's note: The RX pin is not officially 5 V tolerant; it is designed for 3.3 V signals. The Arduino generates a 5 V signal, so you should use a voltage divider or logic-level translator to ensure that the Arduino signal does not damage the HC05.] If you are using the BTBee module with the shield, set the jumpers on the board so that the DOUT pin and D0 pins are shorted and DIN and D1 pins are shorted. This is done because the RX pin on the Arduino is Pin 0 and the TX pin is Pin 1. You are free to use any other pins as the RX and TX Pins, but you will have to use the SoftwareSerial Library of the Arduino to enable that. Do remember to remove the jumpers while uploading code to the Arduino if you have selected Pin 0 and 1 as Serial pins.

Connecting the HC05 Module

Jumper connected to Pins 0 and 1 on the BTBee shield

If the module is being used for the first time, you'll want to change the name, passcode etc. To do this the module should be set to command mode. Connect the Key pin to any pin on the Arduino and set it to high to allow the module to be programmed. If you're using the BTBee, it's a little tricky. While the official wiki says that the Mode Button must be held to change it to Command Mode, and releasing it changes it back to Data Mode, it doesn't happen that way: instead, hold the Mode button, then quickly press and release the Reset button. You will notice that the Status LED blinks slower than usual.

To program the module, a set of commands known as AT commands are used. Here are some of them:

ATCheck connection status.
AT+NAME ='ModuleName'Set a name for the device
AT+ADDRCheck MAC Address
AT+UARTCheck Baudrate
AT+UART='9600'Sets Baudrate to 9600
AT+PSWDCheck Default Passcode
AT+PSWD='1234'Sets Passcode to 1234

All the set commands return 'OK' when they are executed successfully.

Arduino Bluetooth Module

Here's some code to change the name. The jumpers are connected to 4 and 5 because the response from the module will be printed on the Serial Monitor. The complete code is available at the end of this article in the 'download code' button.


Programming the Arduino

No extra library is used to connect to the Bluetooth module because the RX and TX pins of the Arduino are shorted with those of the module. All data--outgoing and incoming--will have to go through the module. Interfacing the module is that easy.
To see how this works, let us connect a DHT-11 Temperature Sensor to the Arduino. When the letter 't' is received, the temperature, humidity, and heat index will be transmitted back. To use the DHT-11, the DHT library by Adafruit is used.

Arduino

The Shield and the connected DHT Sensor

Below is the code used to read data from the DHT sensor, process it, and send it via Bluetooth.


The Android App

Take a look at the previous article for a quick intro about Android apps and also the layout for this app.

The program flow will be as illustrated above.

Editing AndroidManifest.xml

Since the extra hardware this app will be using is the onboard Bluetooth adapter, it will have to be mentioned in the Manifest.

Connecting to the Device

First check for the presence of a Bluetooth Adapter.

Bluetooth

If it is present, check if it's enabled. If it isn't enabled, ask the user permission to enable it.

The device must be paired before the app can use it. Check the section below for details on how to pair the BT Module and the Phone. Now that the adapter is enabled, check for paired/bonded devices. Using the AT Commands from the section above, give an appropriate name to the device. Also find out its MAC Address. Here, the MAC addresses are being compared for a match because names are liable to be changed often.

Arduino Bluetooth Connection

After getting the BluetoothDevice, a socket has to be created to handle the outgoing connection. Here a RFCOMM socket is used. RFCOMM--also known as Serial Port Profile--is essentially a Bluetooth protocol to emulate an RS232 cable.

Program Arduino Over Bluetooth Headphones

Then get the input and output streams of the socket.

Reading Incoming Data

Since data can be received at any point of time, running a thread to listen for data would be best. First, the input stream is queried for available data. Then, the bytes are converted to human readable UTF-8 format and the text is send to a handler to post onto the UI. This is done because the UI can't be updated from background threads.

Transmitting Data

To send data, pass the String to the OutputStream.

Download the Source Code for the App


Testing the App

First off, pair the phone and the module by scanning for new devices in the Bluetooth tab of the System Settings. The name which was set earlier should appear. Put in the set passcode (Default is either 0000 or 1234).

Arduino Program For Bluetooth Module

After you power on the Arduino, you may notice that the BTBee's status LED blinks periodically. Now open the app and tap Begin. The status LED must go off and the Conn LED will glow. This shows that a connection has been established.

When 't' is sent to the Arduino, it replies with the Temperature, Humidity, and Heat Index.

Arduino Program For Bluetooth Car

That was a simple exhibition of data transfer using Bluetooth. Note that you can also use an Arduino Bluetooth serial terminal app if you wish to not use the app provided here. Bluetooth can be used to control the automation of many everyday things, like switching a lightbulb on, which can be done by using a Relay with the Arduino. This concludes this two-part series on interfacing an Arduino with an Android phone. The first part on using a Serial Cable can be found here.

Give this project a try for yourself! Get the BOM.