Subscribe to Make Magazine Today!

arduioo-week

bluetoothprogrammingshield_scaled

If you’ve ever wished you could program your Arduino without lugging a USB cable around, wanted to put an Arduino project somewhere out-of-reach but still be able to easily upload changes to it, thought about building a project that interfaces with an Android phone, or contemplated a way to get remote sensor data streamed to your computer — this project is for you.

It uses a common, low-cost Bluetooth module mounted onto a prototyping shield for durable and reliable use. Along with the module is a circuit that allows the Arduino IDE to automatically reset the Arduino when uploading a new sketch. This project also uses the latest version of the Arduino IDE which eliminates the need to hack in modified DLL files to get the serial programming to work.

Besides uploading sketches, the shield can be used for serial communication using the standard Arduino Serial library between multiple Arduinos, smartphones, computers, or even a Bluetooth-enabled Raspberry Pi.

You will need a computer with built-in Bluetooth or a USB Bluetooth dongle to use the shield to upload sketches to your Arduino. These instructions are specific to Windows 7, but should be similar for Windows 8.  Mac and Linux users can probably figure out how to get this working too. Smartphones need to support Serial Port Profile (SPP) to work (this excludes iPhones but includes many Android phones). This project is moderate to difficult, and requires soldering and previous prototyping experience.

Steps

Step #1: Install the latest, beta version of Arduino.

PrevNext
DIY Arduino Bluetooth Programming Shield

The current beta version of the Arduino IDE has fixed some of the issues that made Bluetooth programming difficult in the past. The most current version available today is Arduino 1.5.6-r2.

You can download the installer from: http://arduino.cc/en/Main/Software

Step #2: Breadboard the Bluetooth module setup circuit.

PrevNext
DIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming Shield
  • The Bluetooth module used in this project is the HC-05. It is inexpensive and one of the most common modules you'll see in a lot of other projects. The HC-05 is sold bare or mounted to a breakout board. You want one on a 6-pin breakout board. You can find them for sale on eBay and Amazon. The datasheet for the module is here.
  • Using a breadboard, temporarily wire the HC-05 Bluetooth module per the schematic.
  • With 3.3v on the Key pin, the module will enter 38400 baud command mode and will accept AT commands. The Key pin must be connected to 3.3v when the module is powered on. If it is connected afterwards, the module will enter command mode at the default baud rate (as set by the 'AT+UART=' command or 9600 baud from the factory).

Step #3: Upload the setup sketch

PrevNext
DIY Arduino Bluetooth Programming Shield

Get the sketch from here and upload it to your Arduino.

Step #4: Run the setup sketch and enter serial commands.

PrevNext
DIY Arduino Bluetooth Programming Shield
  • Open the serial console and make sure the baud rate is set to 9600 and line endings is set to be "Both NL & CR"
  • Enter the following AT commands into the serial console.
    
    AT+ORGL	 
    
    AT+ROLE=0
    
    AT+POLAR=1,0	
    
    AT+UART=115200,0,0	
    
    AT+INIT	 
    
    
    In order, these commands tell the module to reset to factory settings, switch to slave role (transparent serial bridge), set pin 32 low on Bluetooth connection, change baud rate to match the Arduino Uno programming rate, and to initialize.
  • Disconnect 3.3v from the Key pin and cycle the power to the module. It will now be running at 115200 baud and be in pairing mode.

Step #5: Setup the Bluetooth module in Windows.

PrevNext
DIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming Shield
  • From the Control Panel select Add a Device.
  • Add the Bluetooth module.
  • Select enter pairing code option.

Step #6: Setup the Bluetooth module in Windows (cont'd).

PrevNext
DIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming Shield
  • Enter "1234" for the pairing code.
  • At the Add a device success window, click Close.
  • Device ready to use. The OS will create two serial COM ports associated with the device. Always use the one with the lower number.

Step #7: Solder a jumper onto the Bluetooth module.

PrevNext
DIY Arduino Bluetooth Programming Shield
  • Disassemble the breadboarded circuit. Solder a jumper wire directly to the HC-05's Pin 32.
  • Don't forget to use flux.
  • You may want to use hot glue or tape to protect the joint.

Step #8: Build the programmer circuit.

PrevNext
DIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming ShieldDIY Arduino Bluetooth Programming Shield
  • Build the programmer circuit onto the prototyping shield.
  • Solder on a female header for the Bluetooth module if you want to be able to easily remove it later.

Step #9: Program the Arduino With Bluetooth!

PrevNext
DIY Arduino Bluetooth Programming Shield
  • Power on the Arduino from a battery or AC adapter.
  • In the Arduino IDE, choose the serial port of the Bluetooth module (the lower valued one of the two) and then upload a sketch like normal.
Joshua Newell

Joshua Newell

Joshua Newell is a software developer by day, and an electronics and embedded systems enthusiast by night.


  • Sidhant

    Is it possible to program the Arduino Mega with the same procedure.

    • Zootalaws

      Yes

  • http://42bots.com Stan

    Nicely done! One question – I assume with this set-up every time we try to send data to the Arduino over Bluetooth the board will be reset, correct? In other words, the Bluetooth module can only be used to program the Arduino, but will not be usable to send data from or to the board?

    • http://dushyant.ahuja.ws Dushyant Ahuja

      You can use the module to send data to the module – it resets only when it gets connected. You just have to start the serial port at 115200 bps.

      • Michele

        Do you need to change module settings to use it as a communications port, rather than as a programming interface?

        • Zootalaws

          No.

          I loaded (over BT) a sketch that did a serial echo and once it loaded, I loaded the IDE serial monitor and it happily echoed back everything I typed over BT.

          One major difference is that you will be using the ‘native’ hardware uart0 for serial, not software serial for BT traffic. So instead of defining the serial port with SoftwareSerial mySerial(rxPin, txPin); and mySerial.begin in void setup, you just need to Serial.Begin in void setup.

          • Michele

            Great! Thank you so much for the prompt answer!

      • Alfredo Brand Rihm

        Dear Dushyant,
        So every time you connect vía BT, it resets. To not have to be permanently connected to not cause a reset during the operation of any shetch and transmit data, coud we include in every sketch a kind of “reset deactivator pin”? I imagine putting an Arduino-pin to High, which is connected to transistors to prevent operation of the reset coming from BT, unless a command sent by BT put this “reset deactivator pin” to Low and allow to program another sketch. This command should be programmed in the sketch. Is it an idea or not?
        Thank you!

    • Zootalaws

      I thought that would be the case, but it doesn’t.

  • Joseph

    This helped a lot .However, I didn’t need resistor 3 to get this to work. Actually, if i included R3 the circuit wouldn’t work. I am assuming that R3 will constantly output a HIGH to the arduino reset pin and will never rest.

  • Joseph

    Also, I didn’t need to solder a jumper wire to the HC-05. I just used the STATE pin on the HC-05, which happens to connect to Pin 32.

  • gugus

    Hello, I would like to know if this is safe, can the arduino be damaged (unable to upload new sketches) in case there are interferences while uploading (missing or corrupted data) ? In the Arduino IDE there is option “Check code after upload”, I suppose this might help detect upload errors, but does it work with BT? Thanks ;)

  • Fernando Mattoso Lemos

    How to configure HC-05 to program Arduino Leonardo via bluetooth ?

  • Biplab Roy

    Thanks for the Project. I have two questions.
    1. Do I need 2 ATMEGA328 chips? one in the BT programming shield & the other one is TARGET chip?
    2. Why the KEY pin is shorted & disconnected? What is the need of connecting & disconnecting the KEY pin with 3v3 pin?
    Any help shall be appreciated.

    • Joshua Newell

      1. no
      2. shorted=command mode, floating=transparent relay mode

  • Brian Impellizzeri

    Thanks a bunch for this page I connected the 3.3vpin and key pin together and removed the level shifting resistors and finally got at commands on HC-06!

  • Markus Eglin

    I had to use Baud rate of 57600 to program with Arduino 1.5.7 and Seeeduino.
    With the Baud rate 115200 it didn’t work.

    • Zootalaws

      Correct – the default for the 328P-based Arduino’s is 57600, I believe.

  • Jian Li

    the bluetooth module is always paired to the computer as a bluetooth device and is not assigned a port. How do i assign it to a port?

  • Aidas Zarskus

    Spent 2 evenings trying to replicate – no luck.
    Have perfectly operational and setup per instructions HC-05
    Get connection (slow blink) when sending sketch to the board
    But it is either “programmer not responding”, or “not in synch” errors
    At some point was able to upload sketches 2 times in a row (miracle!), but had to unplug the ground resistor (15k) to get board running, as it would remain in reset mode, I believe.
    Tried different values of divider bridge, and capacitor, to no avail.
    Anybody else have it working reliably?

    • ammy

      some time ago I was trying to look for out how to program the arduino wirelessly, I have found this https://play.google.com/store/apps/details?id=com.bluino.bluinohexuploader, maybe this can help.

    • Zootalaws

      Yes, it works, as described in the instructions, perfectly.

      The trick is to follow the instructions to the letter – don’t try and take any short cuts.

      I was getting nowhere until I started talking it over with someone else and he pointed out a few things I was missing… my enthusiasm overriding my ability to read, it seems :)

      In essence, there are two distinct phases to making this work: The ‘programming the bluetooth module’ phase – where you wire it up and load the sketch that allows you to configure the BT module in command mode. Then there’s the ‘loading the sketch over bluetooth’ mode, where you rewire your circuit for programming over BT.

      In my case, the ‘gotcha’ was not noticing that after the first phase, you had to move the TX and RX from the pins used to setup the bluetooth module to pins 0 & 1 of the hardware UART used to program the sketch wirelessly. You have to wire the BT module to pins 0 and 1 for it to work – the bootloader is expecting to sync using the hardware UART.

      I also didn’t have all of the components needed, like the rating of resistors and 100 picofarad capacitor in the diagram, but I substituted 3.3kohm and 5.1kohm resistors and a 220 picofarad electrolytic and it still worked, so the circuit is forgiving.

      I have loaded everything from ‘blink’ to the full 30k of Ardupilot over BT reliably.

      • Joshua Newell

        Thanks for helping people out here!

        • Zootalaws

          Thank you! You did all the heavy lifting, I just had to RTFM :)

    • Claudio Aguila

      Same problem. The error mesage is “avrdude: stk500_getsync()….”

  • techietav

    Thanks for this info.. I used a HC06 and soldered a wire to the STATE header (which did not have a pin) It doesn’t work on the HC-06 as there is only the STATE which pulses while disconnected in AT mode and then stays on when connected. Tried creating a simple circuit using a low pass filter to smooth the pulses and then a NPN transistor to pull the RST low, this works, but holds it low the whole time connected. Might have to get a HC-05 instead..

    • techietav

      An update to say after trying unsuccessfully to flash a HC-06 with HC-05 firmware (which is possible). I bought a HC-05 connected up as per instructions and it works perfectly! THANKS

      • Zootalaws

        What did you use to try to reflash the HC-06?

    • Zootalaws

      It does work on the HC06 – don’t use the ‘state’ pin, but follow the instructions and solder to the Pin34 of the HC module.

      • techietav

        Tried that, but the pin doesn’t stay low when connected as the module doesn’t accept the POLAR command to invert the pin 34 state. It goes high which is the default.

        • Zootalaws

          What exact module do you have?

          The ‘Hc-05′ and ‘HC-06′ designations are loosely attributed by every various Chinese assembler with access to a firmware tool. In reality, HC-05 and HC-06 are model numbers of Linvar Ltd. I’ve seen about three or four different firmwares with variations on the AT command set. I have 10 with the most awful limited firmware that I will be re-flashing as soon as I have the correct tool. The command set on that is limited to setting the UART and password.

          If it was an actual 05 or 06, it would accept the POLAR command. Maybe it was based on another chip other than the CSR-BC417 chipset?

          • Mikolaj.K

            Hey, I have a problem ’cause AT on Arduino doesn’t working. None confirmations from Ardu.

          • Sarchasm

            Not enough information to answer that

          • Mikolaj.K

            I don’t know what is important.

          • Sarchasm

            What have you done so far? What kind of Arduino board are you using? What connections have you made? What are you using to connect to the Arduino? Do you get any sort of indication that you are connected correctly?

            Simply: If you follow the instructions above to the letter, it works – so work through the instructions one step at a time and make a note of what works – when you reach a step that doesn’t work – come back and tell us what is wrong.

            There are two distinct stages of getting this to work. The first step is to set up the Arduino to talk to the BT module so you can program it.

            That includes loading a sketch that sets up the serial pins, wiring up those pins to the BT module, powering the pin on the BT module to set it in command mode, connecting your PC to the Arduino to connect to the BT module in command mode.

            Once the BT module is programmed correctly, you have to wire it up to act as the programming interface, using serial pins 0 and 1 (1&2, depending on how you number them).

        • Zootalaws

          PS – I have a module where the ‘state’ pin is actually tied to PIO2 – pin25. You never know until you find a supplier that gives you a consistent product that you can trust.

        • Zootalaws

          FWIW, I just checked my ‘good’ HC

          at+version

          +VERSION:2.0-20100601

      • Adrastos

        Why Pin34 instead of 32?

  • keven

    You can buy Bluetooth shields, Bluetoothduino, Bluetooth adatper here: http://www.elecfreaks.com/store/products_new.html

  • Guest

    I followed every step, but cant upload sketches. It says that COM-port is in use.

    Also when I check devices in configurationscreen. It says bluetooth disconnected.

  • Marijn

    Is it possible to program just the atmel328p chip with this procedure?

    • Alexandre Terentiev

      Yes if the 328 is loaded with the Arduino bootloader.

  • jb

    Good afternoon.
    Can I request for the whole details of this project? The schematics and the sketch if thats ok with you?
    Thanks. More power!

  • Sujeet Banerjee

    GOT IT WORKING IN SECOND ATTEMPT!

    Baudrate set using AT+UART is a critical piece (Step 4), which if done incorrectly will lead to avrdude: stk500_getsync(). The baudrate somehow depends on the Arduino/Processor.

    Arduino Uno: AT+UART=115200,0,0
    Pro Mini: AT+UART=57600,0,0

    In case you hit, stk500_getsync() while sketch upload via bluetooth, Check out how the Arduino Software loads it using a USB/FTDI. Go to preferences and turn ON verbose mode for compile and upload. This will show you what baudrate is expected for a particular make.

    And, after lots of experimenting I found out, the level-shif resistors’, capacitor values are really not rigid. Just ensure that the resistor to ground should be at least 1.5 times of the one to Vcc.

    • dumle29

      To elaborate a bit on this. If you want to see what baudrate the bootloader is expecting the programs to be loaded at, you can also check the make-file for the bootloader your arduino is running. For the Uno and other boards running the optiboot bootloader, it’s [Arduino install folder]/hardware/arduino/bootloaders/optiboot/Makefile

      Look for the line ISPSPEED = -b [BAUD RATE]

  • http://www.upverter.com yusefHD

    Hey Joshua,

    Awesome product. If you don’t mind taking a look, our team made an infographic for finding the right bluetooth module for you. Take a look if you’re looking to see the different types out there!

    http://blog.upverter.com/post/102541328427/bluetooth-module

  • Kim Hjorth

    hi im trying to get this to work and the hc06 and my arduino works togheter fine as long as i press reset on arduino nano right after i hit opload!!!
    but i have triet to to make your auto reset but it dont work i have triet both 34 and 32 but nothing can you tell me how i can find the right one?

    • Joshua Newell

      you need an hc05 to get autoreset to work

  • Rob Young

    I had everything set up correctly and then when uploading the sketch, I got the error

    avrdude: ser_setspeed(): tcsetattr() failed
    avrdude: ser_open(): can’t set attributes for device “/dev/cu.H-C-2010-06-01-DevB-1″: Invalid argument
    ioctl(“TIOCMGET”): Bad file descriptor
    avrdude: ser_close(): can’t reset attributes for device: Bad file descriptor

    anyone seen this or know how to fix it? I’ve been using a 9V battery for power and I’m trying to control a motor using an hbridge.

  • Tim

    I am having trouble with the serial command step. When I open the serial monitor, it sends the “AT” command, but I do not receive an “OK” response. Instead I receive “ÿÿ” at varying intervals. I am able to enter the rest of the commands, but all receive the same response.

    I tried moving forward, but when I got to the last step, I was unable to send a sketch over BT. I have a feeling that the serial commands aren’t working, but I’m not clear what the issue or solution is.

    • Tim

      Hardware:
      HC-05 module
      Uno board

      TX is run to Pin 8 (reading 5V signal)
      RX is run to Pin 9 (reading 5V signal)
      5V is run to 5V (reading 5V signal)
      GND is run to GND
      EN is jumpered to STATE (reading 3.5V signal)

    • Tim

      Addendum: Do you have to type the OK commands, or are those supposed to be responses from the Arduino?

    • Tim

      Got the HC-05 programmed! Also stumbled upon the full AT command list here, which shows the command AT+PSWD to change the passcode for your connection:

      http://www.techbitar.com/uploads/2/0/3/1/20316977/hc-05_at_commands.pdf

      I am successfully uploading a program to my Uno now; however, my Arduino software occasionally gives me an error such as:

      avrdude: stk500_recv(): programmer is not responding

      Even when it gives me that error, the program uploads successfully on my Uno. I’m running Arduino 1.6.5. Not sure if this is a bug in the software, or if something weird is happening with my Bluetooth connection.

  • Mümmel

    Hi, tried the steps of this interessting post. Until the soldering it all worked fine. But after the soldering I tried to upload the Blinking scatch for a test. I used the newest Arduino IDE (1.6.4?) and the Uno R3, I selected the lower of the serial ports of the HC-05.

    The test resulted in the red LED on the HC-05 blinked all the time and the code was not able to be uploaded (nothing happed and the uploading procedure could not stop).
    Afterwards I doublechecked the soldering but could not find the Issue. Could you help me out what the most probable mistakes are?

  • mvadu

    Thank you for this guide. For my HC-05 module (CZ-HC-05 gomcu) I had to use 4.7uF cap. I also did not use additional resistors, I just left with pull up resistors already present on Arduino Nano.

  • Mike

    Thank you. Upload works smoothly every time using Arduino 1.6.4 on OSX Yosemite. But on Windows 7 machine its really hard to get it to work, sometime it works after I restart Arduino, delete bluetooth device and pair again, but most of the time I get either “Access is denied” or Element not found. What I really want is to upload wirelessly using Visual Studio with Visual Micro Arduino plug-in but no luck yet, tried with virtual Windows 7 using Parallels and Win 7 using bootcamp, could also be Win 7 bluetooth communication issue, I’ll try XP/Win8, any helpful information?

  • Dave Beane

    OK guys. I think I have read all posts here. What this article covers is exactly what I want. I tried to basically follow instructions and it did not work. So I thought I best exactly follow the instructions. But despite my best efforts, and to potentially look like a fool, I can’t seem to follow the diagram in step 2 because I can’t find a 5 volt on the HC-05. My initial thought was to use a voltage divider and put it into the 3.3 pin but that is clearly not what is in the diagram. I am specifically using the CZ-HC-05 but I have read the spec sheet for the HC-05 and there is no 5 volt pin. What am I missing here? There have been so many people here saying this was great if you follow the instructions, I must believe it works and I am just missing something here.

  • Dave Beane

    Another thought, is there any chance that the 5V is on a specific breakout board and it simply divides the voltage which is then attached to the 3.3 volt pin? This would imply that either power supply could be used and It would be fine for me to supply the 3.3 volt with my own voltage divider on my breakout board (CZ-HC-05).