Hello,

For the first part in the communication-series we will talk about bluetooth.

Introduction

Thanks to it's proliferation on mobile phones, We have all come across bluetooth in the past. It is a very useful wireless technology to transfer information over short distances. We have all seen Bluetooth Headsets and Speakers, and in the past we have transferred contacts and images over bluetooth as well, nowadays most wireless keyboards and mice also utilize this technology. In this post we will learn how to add Bluetooth Functionality to an Arduino, so that it can receive commands remotely.

The module we will be using is the HC-06 Bluetooth Transceiver module, which just outputs any data received over Bluetooth to the Serial Pins.

Things Needed

  • An Arduino
  • An LED or any device that can show output from an Arduino
  • An HC-06(or equivalent) bluetooth module
  • A computer with bluetooth and a serial terminal program(like Coolterm) installed.

Wiring It Up

We will first wire everything up. We need to connect the RX and TX pins of the bluetooth module to the TX and RX pins of the Arduino. We will then connect it's VCC and GND pins to 5V and GND of the Arduino. Remember to make sure that your module supports 5V!! If not, you can use the 3.3V Pin of the Arduino.

We will finally connect the positive pin of an LED to pin 13 and the negative pin to the GND pin of the Arduino.

The schematic:

My Setup looks like:

Code

The code for this is pretty easy. We will open up the Serial Port on the Arduino, and start waiting for data, if the input data matches a predefined pattern, we can perform actions on the Arduino. In this example, if the input is "1", we will turn on the LED, and if it is "0", we will turn it off. We will also send an acknowledgement back to the device that sent out the command. The full sketch is:

char input;
char lastValue;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}


void loop()
{
  if (Serial.available())
  {
    input = Serial.read(); //read it
  }
  if (input == '1')
  {
    digitalWrite(13, HIGH);
    if (lastValue != '1')
      Serial.println(F("LED has been turned on"));
    lastValue = input;
  }
  else if (input == '0')
  {
    digitalWrite(13, LOW);
    if (lastValue != '0')
      Serial.println(F("LED has been turned off"));
    lastValue = input;
  }
  delay(1000);
}

We should now Upload the sketch to the Arduino. If there is an Error uploading, you can try by disconnecting the RX and TX pins as sometimes that can interfere with the sketch upload.

Usage

Now, the first thing you need to do, is pair your Computer with the Bluetooth module. Usually, you can go to your system settings and open up bluetooth settings. You can then search for devices(mine was called HC-06), and click pair. If it asks for a passcode, the default is 1234.

Once the pairing has completed, lets open up CoolTerm and create a new connection. To this you can go to options and Setup the serial port information, my options screen looks like:

Once you have set it up, you can just click connect, and the connection should succeed and the LED on your module(if it has one) should go from Flashing to a solid red color. Once that has happened you can just start typing the keywords defined in the sketch(0 or 1 in the example), and you should see the Magic Happen! Here is my terminal:

You can also do this from an Android Phone using an app like Bluetooth Terminal, unfortunately it does not work for iOS devices as these require a special chip to enable Bluetooth communication.

Conclusion

There you have it, you can now control any GPIO on your Arduino using your computer or Android Phone! If you are looking for ideas, I have used this method in the past to control robot cars as well as relays.