DIY and Tinkering

Project 01: Bluetooth controlled LED

In this project I will establish a Bluetooth connection between my computer and the Arduino. Once the connection is established I will switch on/off a LED.

Required Hardware

  • Arduino uno
  • HC 05 Bluetooth module
  • 1 x LED
  • 1 x resistor 220 Ohm
  • 1 x resistor 1 kOhm
  • 1 x resistor 2 kOhm
  • Some Jumper cables


Connecting the LED

Connect the LED’s anode with pin 13 and it’s cathode with a 220 Ohm resistor and GND.

Connecting HC 05 –> Arduino


VCC –> 5V

TXD –> Pin 2

RXD –> Pin 3; Do not forget the voltage divider!


/* Working with the HC 05 Bluetooth module
* This example code is in the public domain.
* This sketch uses a Bluetooth connection to switch off/on a LED
* It uses a Software Serial interface

#include <SoftwareSerial.h>

// The pins we will use for the Serial
const int BTRX = 2;
const int BTTX = 3;

// The LED pin
const int LED = 13;

// The message we will send using BT
String strBtCommand;

SoftwareSerial SerialBT(BTRX, BTTX);

* Function setup
* Establish the connection to the HC 05
void setup() {
SerialBT.println("Bluetooth connection successfully established.");

pinMode(LED, OUTPUT);

* Function loop
* Wait in every iteration on receiving a command via Bluetooth
* command "on" --> switch on LED
* command "off" --> switch off LED
void loop() {
if(SerialBT.available()) {
strBtCommand = SerialBT.readString();

if(strBtCommand.indexOf("on")>=0) {
digitalWrite(LED, HIGH);
SerialBT.print("LED on pin ");
SerialBT.println(" is switched on");
} else {
if(strBtCommand.indexOf("off")>=0) {
digitalWrite(LED, LOW);
SerialBT.print("LED on pin ");
SerialBT.println(" is switched off");
} else {
SerialBT.print("Unknown command: ");

I used a Software Serial in this code to make some experiences with it. Arduino comes with a build in Hardware Serial on pin 0 and pin 1. If you want to use other pins than those you need to use Software Serial.

Once you uploaded the code to the Arduino, the built in LED of HC 05 starts blinking in a high frequency. That means, the module is ready to establish a Bluetooth connection.

Connecting HC 05 to a Mac

Open System preferences –> Bluetooth

You’ll find an entry like HC 05 or – as I did when I tried to connect the first time – the MAC address of the module. Click „connect“.

Your Mac tries to connect to HC 05. After a while there will be a „option“ button right to the name of the Bluetooth device. Click it, enter the code 1234 and press connect.

After entering the code your Mac will establish a connection with HC 05.

In the Arduino IDE select Tools –> Port /dev/cu.HC05-SPPDEV and open a serial monitor. See the picture below for set up details. „No CR“ seems to be important.

Enter „on“ in the line and send the data to the board. The LED will light up. Enter „off“ in the line and send the data. The LED will switch off.

How to mess up this project

Leave the serial monitor switched to HC05

This will result in a error message, because the IDE tries to connect to Arduino using the Bluetooth connection. You need to switch back to USB port (Tools –> Port /dev/cu.usbmodem….). The error message will look like this:

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x55
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x6e
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x6b
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x6e
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x6f
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x77
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x6e
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x20
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x63
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x6f
Beim Hochladen des Sketches ist ein Fehler aufgetreten

Sometime the message is more like (same solution like above):

avrdude: ser_open(): can't open device "/dev/cu.HC05-SPPDev": Resource busy
Problem beim Hochladen auf das Board. Hilfestellung dazu unter

Pick the wrong Baud rate

The serial communication uses 9600 baud. There are quite a lot tutorials online using 38400 baud. This is the baud rate for the AT mode of HC 05. If you use this, you will get a output in the monitor which is not human readable.

String comparison

In my example code I use String.indexOf() to find my key strings on and off. I first tried using the == operator, but for reasons I don’t know this did not work. According to the reference page == is the same as the equals-Methode, which is right – at least both seem not to work in my project.

2 Kommentare

  1. Me

    Update: In the Article I wrote that the „no CR“ setting of the serial monitor is important. I found out that not using the „no CR“ option was the reason why I had difficulties with string comparison. If you use „no CR“ in the Monitor, you can change the statement
    strBtCommand.indexOf(„on“)>=0 to
    strBtCommand.equals(„on“) or even shorter
    strBtCommand == „on“
    In fact strBtCommand.indexOf(„on“)>=0 to is not a good option as it returns true for all string which contain „on“, like „oonn“ or „nnnnnooon“.

  2. wictro


    Tried atleast 8 different codes to no avail (got to point that i could read AT-commands on pc-serial monitor, but the arduino/bluetooth module did not send back „ok“.

    This one works, and I can continue on my robot build 🙂

    Thanks for the tutorial, alles Beste!


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

© 2022 Lochnerweb

Theme von Anders NorénHoch ↑