Last time we looked at measuring temperature using a digital temperature sensor. So we have several modules generating data, would be great to be able to access that data remotely. Let’s take a look at communication via Bluetooth with Arduino, as it turns out this is very easy!

JY-MCU sensor

The Bluetooth module we’ll be using is called JY-MCU Bluetooth Board Module (4-pin). It’s pretty cheap, at this time costing only $5.97. Considering how ubiquitous Bluetooth is (most smartphones and laptops have it), it will turn out to be extremely useful in our future projects.

Here is the datasheet for the main module: HC-06 serial wireless module.

  • The main module operates at 3.3V, but requires 3.6-6V to power the full package;
  • Sleeping current is <1mA, working current is 40mA;
  • Has a working range of about 10 meters
  • Defaults:
    • Baud rate: 9600
    • Device name: linvor
    • Passkey: 1234

It also provides some information on how to change the baud rate, device name and passkey, so make sure to check the data sheet.

Wiring

The wiring is really straightforward, no extra components are needed. Simply connect the 4 pins to your Arduino board.

Check the other side of the module, there you should find screenprinted markings of pins.

  • Connect VCC pin of the module to Arduino’s 5V pin
  • Connect GND pin of the module to Arduino’s Gnd pin
  • Connect TXD pin of the module to Arduino’s digital pin 10
  • Connect RXD pin of the module to Arduino’s digital pin 9

JY-MCU wiring

And that’s it. Let’s take a look at the code.

Code

Libraries

For transmitting information through the module we will use the default SoftwareSerial library. It is already bundled with Arduino, so no further downloads are necessary. Reading through the docs is, again, encouraged, as we will use very similar code to drive our JY-MCU module.

If you open up SoftwareSerial constructor docs, SoftwareSerial constructor is defined as such:

SoftwareSerial(rxPin, txPin, inverse_logic)

Only two pins to drive the module, other than that the interface is exactly the same as serial communication to PC via USB. We don’t need to worry about inverse_logic parameter.

Program code


/**
 * JY-MCU Bluetooth module communication example for Arduino.
 *
 * Connect RX_PIN to TX pin of the module,
 * Connect TX_PIN to RX pin of the module.
 *
 * Based on SoftwareSerial example by Tom Igoe and Mikal Hart.
 * Adapted for JY-MCU module by Tautvidas Sipavicius.
 *
 * This example code is in the public domain.
 */

#include <SoftwareSerial.h>

const int RX_PIN = 10;
const int TX_PIN = 9;
const int BLUETOOTH_BAUD_RATE = 9600;

SoftwareSerial bluetooth(RX_PIN, TX_PIN);

void setup() {
   Serial.begin(9600);
   bluetooth.begin(BLUETOOTH_BAUD_RATE);
}

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

First we include the library and set our RX and TX pins. We’re setting communication pins to 10 and 9, but you can choose other ones if needed. Let’s leave the default baud rate of 9600 for this example.

Bluetooth Serial initialization works exactly the same way regular Serial communication works, we call begin method with the baud rate.

loop() logic is simple. If there is any data received by the bluetooth module, send it over the regular Serial connection to our PC. If PC sends information via Serial, then we send that information over via Bluetooth.

Testing

Now that we have wired up the module and have the program loaded on our Arduino, we’d like to test it out.

If you have an Android device, there is this Blue Serial app for testing serial communication over Bluetooth. For Apple devices there should be something similar on the App store as well.

Once we turn on Arduino, a red light on Bluetooth module should be flashing continuously. Once connection is established, that red light will stay on constantly.

If you open up your Bluetooth testing application of choice (I used “Blue Serial”) and search for devices, you should be able to find a device (by default) named “linvor”. Pair with it and use the (default) pairing key “1234”. Once paired with the device, click “Connect”.

If it connected properly, the red light on the Bluetooth module should stop flashing.

Now you can send information from the device over bluetooth to Arduino, and, if you used the example program above, Arduino will send it over to computer’s Serial window. Arduino Serial Monitor can be opened up on your Arduino IDE by clicking “Tools > Serial Monitor (Ctrl + Shift + M)”

Here I sent some information from my PC to the phone and vice versa: JY-MCU message sent from PC JY-MCU message sent from the phone

Further reading

If you’re interested to learn more about this, here are some extra resources:

And if you want to buy the JY-MCU bluetooth module, here’s the link: Buy JY-MCU Bluetooth module