Connecting Arduino Uno and the JY-MCU Bluetooth module using SoftwareSerial

Connecting Arduino Uno and the JY-MCU Bluetooth module using SoftwareSerial
JY-MCU-Bluetooth-Module-Arduino-Uno

My previous post covers the basics of setting up a Bluetooth connection between an Arduino Uno and an Android phone using the JY-MCU Bluetooth module. We connected the  JY-MCU Bluetooth module to the Arduino via a serial connection on (digital) pins 0 (rx) and 1 (tx).

This uses Arduino’s built in hardware support for serial communication (via something called UART) and requires no additional libraries. The Arduino has a 64 byte serial buffer and can receive and store data in it, while executing other tasks in your program. For some great examples of using serial communication, as well as wealth of other Arduino related info see Nick Gammon’s web-site.

There are a couple of downsides of this set-up for Bluetooth connectivity:

  • You cannot use the Arduino serial console for debugging your code in the Arduino IDE console anymore. This is because the same two Arduino pins used of serial communication with the module are needed to communicate with your computer, via the USB port.
  • For the same reason, you can’t program your Arduino, while the JY-MCU Bluetooth module is connected and powered.
  • Last, but not least: you can’t use the Arduino IDE serial console to configure some of the settings of the JY-MCU Bluetooth module (like name, default baud rate, pairing code etc.).

The SoftwareSerial library helps address these problems and comes pre-installed with the latest version of the Arduino IDE. It has been developed to allow multiple serial communication on (almost any) digital pin of the Arduino, using software to replicate Arduino’s native serial support. See the SoftwareSerial library page for more details on its functions and limitations.

Only a couple of changes are needed from our original Arduino / JY-MCU Bluetooth module set-up to implement SoftwareSerial:

  • Change the wiring to free up digital pins 0 and 1 and replace them with other pins supported by the SoftwareSerial library. I chose to use digital pins 2 and 4 on my Arduino Uno.
  • Modify our Arduino sketch to import and use the SoftwareSerial library and the alternative pins.

Here is a picture of the updated wiring. Note that I am still using a voltage divider to shift the TX signal from the Arduino to 3.3 volts, as in my previous post:

Arduino-JY-MCU-Bluetooth-Android-SoftwareSerial

And here is the modified sketch.

As in the original post, you can use Blueterm to send 1, or 0 (zero) from your Android phone to the Arduino and switch the LED on or off.

Tagged with: , , , ,

11 Comments on “Connecting Arduino Uno and the JY-MCU Bluetooth module using SoftwareSerial

  1. if im using software serial, how can I change the name baudrate and pin of the Jymcu?

    im planning to use this module for a reprap 3D printer im making, and in it it uses serial port to send and receive data to and from an arduino mega, they have a software called pronter face that sends and receives the codes
    there is also a page abt this, but ive had no luck following his tutorals, but following ur tutorials I was able to easily connect my phone to this module and arduino to control LED, so im hoping that u will make a tutorial abt controlling or uploaded sketches to arduino mega from laptop through Bluetooth and this module.

  2. Hi Stan,

    Thanks a lot for the tutorial. Very helpful and easy to follow.

    I tried the SoftwareSerial to control my bluetooth as you described. I observed that as long as I power my board with a battery, it runs perfectly. But when I power it with the USB cable connecting to a computer, the software TX pin seems to send me some error data. In my sketch, it supposed to send out 0 all the time, but occasionally it would send out some other seemingly random value. Have you ever observed anything like that?

    For example when powering with battery it prints “0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000…” but when I unplug the battery and plug the board to the computer it prints “0.000 0.000 0.000 0.000 94.81916 0.000 0.000…”

      • Yes, Stan. Supposedly :). Supposedly I use a simple voltage divider to convert the resistance of a sensor into voltage. But in this case I just short circuit the sensor so supposedly the voltage between two leg of the “sensor” should be 0. But interestingly it sometimes has some error. Today I notice that even using battery I would have some similar error sometimes, but the error rate when using USB as power source seems to be a lot more. Anyway, this error probably has nothing to do with your configuration.

  3. To clarify more: At first I suspected that the SoftwareSerial lib might have some bug. But even when I switch back to hardware Rx and Tx pin and observe the output long enough I would see similar error, or “noise” I guess. So this is, I think, an error somewhere in the analog part of my circuit. Just for some reason this error happen more often when I connect my JY-MCU into pin 2 and 4, use SoftwareSerial lib, and power my board with USB cable.

  4. Great article! I ended up connecting the bluetooth device to the 3.3v on my arduino and it worked flawlessly. I never heard about the android app, and that was really awesome. Thanks again for taking the time to write this.

    Chris

  5. Hi,

    When I run the Bluetooth sketch I get the following errors:

    avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
    avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
    avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

    Your help is very much appreciated.

    Tom Evans

    • Hi Tom, you need to try and isolate the issue. Can you upload sketches if you disconnect the BT module from the Arduino? This error can be caused by a number of things, so it is hard to pinpoint the problem without more info.

  6. Great tutorials you provide! I can connect to the HC-05/06 with the Android. I’m unable to connect to the HC-05 through Tera Term and send AT commands. I’ve verified that the correct com port is selected in Tera Term setup. Any help is appreciated.

  7. Hi chief
    thank u so much. this very great but i dont have Ardoino and i shoud use it with AVR controler.
    con you put program with codVision or basic (bascom Avr) to help me.
    please help me if you can.

Leave a Reply to Tom Cancel reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.