A hassle-free guide to programming Arduino over Bluetooth

Source and original idea from Circuit Digest.

Arduino is a great little piece of technology that holds immense power for tech projects, especially DIY tech projects. But the problem is, you need to connect it to a PC through a USB cable to upload code. Wouldn’t it be much easier if you could do that over Bluetooth? In this guide, we are going to show you how to program an Arduino over Bluetooth without a hassle.

Let’s get started, or you can learn more about an Arduino Uno here first.

Step 1: Gather the components

For this project, you will need:

HC05 Bluetooth module
Arduino Uno
Breadboard
Jumpers wires
1K and 2.2K resistors
Power adapter
Capacitor (0.1uF)

This project uses an HC05 Bluetooth module that is designed to facilitate wireless communication for Arduino and other microcontrollers. The module comes with master and slave modes and communicates through 9600 baud rate serial communication. This allows the module to easily interface with the controller. Also, the module can work on 5V as it comes with 3.3 V and 5V onboard regulators.

Step 2: Programming Arduino over Bluetooth

First, we will make Arduino responsive to the AT commands. For that, connect it to your Laptop’s serial port and upload the following commands:

#include <SoftwareSerial.h>
SoftwareSerial HC05(2,3);
void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");
HC05.begin(38400);
}
void loop()
{
  if (HC05.available())
    Serial.write(HC05.read());
  if (Serial.available())
    HC05.write(Serial.read());
}

The first two commands define the software serial library and the receiver and transmitter pins where Bluetooth is connected. The software serial library allows serial communication with the digital pin. You can use multiple ports to create serial communication with the software serial library. See the code below.

#include <SoftwareSerial.h>
SoftwareSerial HC05(2,3);

Next, in the setup function, you need to define the baud rates for software and hardware serial ports. For hardware, it will be 9600 and for software, it will be 38400.

void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");
HC05.begin(38400);
}

Next, define if conditions in the loop function. There will be two if conditions: first will check if there is a command for HC05. If the condition is positive, then it should be sent to the Arduino’s serial monitor; the second will check if there’s a command for the Arduino’s serial monitor. If this is positive, then set it to send to HC05.

void loop()
{
  if (HC05.available())
    Serial.write(HC05.read());
  if (Serial.available())
    HC05.write(Serial.read());
}

In the next step, you will make the connection between Arduino and HC05 to initiate AT command mode. See the following circuit diagram for this connection.

Here, remember to press the key button before you connect your Arduino to your PC. Keep a finger on the key button until you see the Bluetooth LED blinking. It should blink at a 2-second interval. Note that the Bluetooth LED module will blink slower than normal in the command mode.

Next, enter the following commands in the serial monitor. You will see an “ok” message if the commands are successful.

AT+ORGL
AT+ROLE=0
AT+POLAR=1,0
AT+UART=115200, 0, 0
AT+INIT

After testing all the AT commands, remove all connections and use the following diagram to connect the components.

Use a 9 Volt battery or an adapter to power your Arduino. After connecting the battery, locate “devices” on your PC and turn the Bluetooth on. Next, connect the Bluetooth module to your PC.

After the connection to the device is successful, locate the COM port for the module. For that, locate “Device Manager” and search in COM ports option. You will see 2 COM ports: incoming and outgoing. You need to select the second port as we are uploading. See the image below.

Next, choose the clink program in Arduino IDE and choose the right COM port. Hit upload and wait for the program to install. If everything has been entered right, you will notice the LED blinking at a 1-second interval.

And you are all set!

See the complete code for this tutorial below:

#include <SoftwareSerial.h> 
SoftwareSerial HC05(2,3);
void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");
HC05.begin(38400);
}
void loop()
{
  if (HC05.available())
    Serial.write(HC05.read());
  if (Serial.available())
    HC05.write(Serial.read());
}

You can get started with your Arduino Uno here.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: