Introduction: Arduino Bluetooth Part 1 - Bluetooth Modules
This project aims to set up a remote control link between two Arduinos using Bluetooth to communicate. Part 1 will look at how to set up BT modules. It would be best to read through this part before you start unless you are already familiar with BT modules.
Why BT? Bluetooth is cheap and reliable. The modules are small and use very little power and they work straight out of the box. They are simple to use as they basically just replace a serial cable. (A new version of Bluetooth like Bluetooth Smart works quite differently though.)
I will describe what I did and the results I got and also what I believe is going on.
I am using Arduino Uno and Nano (any Arduino will work), Bluetooth modules labelled ZS-040, and a Windows PC connected by USB cable.
Be aware that there are various Bluetooth modules around and some of them work a bit differently to mine.
ZS-040 is a "class 2" Bluetooth device with a range of about 10 metres. (Don't use it for model aircraft!) It's a commonly supplied module which you should be able to source.
Step 1: What We Need
To work through this project we need:
Two Arduinos, ideally one would be a Nano for a remote device.
Two Bluetooth modules. I am using two ZS-040 where they are both HC-05 type. I could have one HC-05 and one HC-06 instead.
2 resistors value 1K approximately
2 resistors value 2K approximately
Two breadboards so that the master can be set up on one and the slave on the other.
Jumper wire in various colours.
A PC loaded with the Arduino IDE
USB cables for the Arduino boards.
A cellphone or other Bluetooth capable device
Step 2: Test the Bluetooth Module
To test the BT module I can use any bluetooth device such as a cellphone or notepad that has bluetooth capability. I'm using an Android tablet.
I set up an Arduino Nano on the board and plugged the BT module in next to it. Both of these devices are straight out of the box. The Arduino should not cause any issues but if the BT module has been used before it could have been configured internally so at this stage it's safest to use a new one.
Power up the BT module by connecting 5V to the VCC pin and Ground to the GND pin.
When you plug in the USB cable from your computer the LED on the Arduino board should come on.
The LED on the BT module should be flashing rapidly.
Turn on your Android device and search for available Bluetooth devices. You should find a name like HC-05 or HC-06. You may also be able to see the 12 character BT id number of the module. This would look like 12:34:56:78:9A:BC or similar. Make a note of this number for future reference.
Pair with that device using password 1234. That's the default password so it should work on a new BT module.
If that's all good, your BT module is working fine and you know what type it is. At the moment it is set up as a slave device.
Bluetooth communication needs one module set up as a "master" and one as a "slave". HC-05 works as a master or a slave while HC-06 works only as a slave. Where I am they only sell HC-05 which is OK as they will do either job.
Step 3: Prepare to Receive Data From Bluetooth Device
I disconnected the USB cable then added some wires and resistors to the circuit.
I connected the TX pin on the BT module to pin D2 on the Arduino (green wire in the picture).
I connected the RX pin of the BT module to pin D3 on the Arduino (white wire in the picture).
The RX is connected by a voltage divider. This is two resistors where one resistor has twice the value of the other. This drops the signal voltage from the Arduino down from 5V to 3.3V
This is advisable because the BT module uses 3.3V logic and we don't want to be hammering it with 5V from the Arduino all the time. It probably works fine using the 5V signal but I'm going by the advice from martyncurrey.com and anyway two resistors is easy enough to do.
The supply voltage shown on my module is 3.6V - 6V. This indicates that the module has an on-board voltage regulator to supply the 3.3V needed. A voltage regulator always needs a little more than the delivered voltage as it causes a drop in the supply.
Next get a program for the cellphone or tablet that can send data values to the BT module.
https://github.com/mayoogh/arduino-bluetooth-basic has an Android program "LED controller.apk"
I downloaded this to the my tablet and installed it.
Step 4: Note on the Voltage Divider
The exact value of the resistors is not critical, just that the top one is half the resistance of the bottom one.
1K and 2K gives an idea of the appropriate size. There is almost no current so the wattage of the resistors can be minimal, very small size resistors can be used.
Step 5: Copy Sketch to Arduino and Connect Up
Copy the program BT_talk into the computer and open it.
The sketch can be verified and uploaded to the Arduino board.
Run the Serial Monitor and it should say that the Arduino board is ready.
Over to my tablet, I connected to the BT module then ran the program I installed before.
When I press the control buttons on the tablet screen I get 1's and 0's appearing on the Serial Monitor.
If you don't get the data coming through, check that:
Serial Monitor is set for "Both NL & CR" and it is running at 9600
Check the cellphone/tablet are connected via Bluetooth
Check the wiring on the board, specially the tx and rx connections
Step 6: Communication Speeds
Now I have the tablet sending data to the BT module, which sends it to the Arduino, which sends it to the PC.
It's important that the comms are set up properly for each step or the data will get scrambled.
Communications between Bluetooth devices happens automatically so we don't have to do anything. The comms from BT module to Arduino are at the default of 9600. The comms from Arduino to PC are also at 9600 and that's fine for our purposes.
The comm speeds can be set in the Arduino program,
Serial.begin(9600); //sets the speed between the Arduino and the PC.
BTserial.begin(9600); //sets the speed between Arduino and Bluetooth module.
The Serial and BTSerial speeds don't have to be the same.
Step 7: Change Settings in BT Module
The internal settings in a BT module can be changed by putting the module into what's called "AT mode". This allows me to change parameters stored inside the module.
It might be useful to give the modules different names if you have more than one connected at one time. The master can connect to up to 7 slaves, though I'm only going to do 1.
If the BT module has a button next to the EN pin that can be used to set up AT mode. Otherwise it can be done using a piece of wire touching pin34 of the BT module.
The way to get the module into AT mode is to hold down the module button while applying power to the module then release the button.
If there is no button a wire can be touched to pin34 while power is applied to the BT module then the wire removed. I have added another wire connected to the Arduino 3.3V pin, yellow in the picture.
When the module is in AT mode the LED blinks slowly, about once every two seconds.
Step 8: Turning the BT Module on and Off
Simply pulling the 5V supply wire will disconnect power from the module while the Arduino is still running.
Pushing it back in will power up the module.
Step 9: Getting AT Mode With a Wire
Disconnect the power from the module by pulling the jumper wire.
Touch the bare end of the 3.3V yellow wire against pin34 of the BT module. This is the top most pin on the EN side of the module. I am using ZS-040 BT modules.
While the wire is touching pin34, put the power wire back in, then remove the yellow wire. The LED should flash slowly on the BT module.
Step 10: Changing the Bluetooth Settings
Once the module is in AT (Attention) mode it can be queried from the Serial Monitor.
Load the program BT_config.ino into the Arduino and run it. Open the Serial Monitor. Notice that in AT mode the BT module has a default speed of 38400 when talking to the Arduino.
Type in capitals on the Serial Monitor
and you should get the response
If you don't get a response, unplug the BT module power and reconnect it, in case the module is hung up. Restarting the Serial Monitor can also help to resolve problems. The wiring is probably OK since it was working with the previous exercise.
Note which COM port the USB connection is using.
Try the following dialog where your entries are in bold and the responses from the BT module are shown after them:
+ROLE:0 (ROLE 0 is slave and ROLE 1 is master)
I had no response. As long as it does not report an error then it is probably good.
AT+NAME (If your module responds to the command you can check the name.)
Disconnect the power from the BT module and reconnect. The BT light should flash quickly again.
Now if I search for the module using the my tablet I find one called MOD01.
So now I know how to talk to the module and change settings in it.
So far I have had the tablet connecting to the BT module. In the next part I will get the two modules to talk to each other.
Step 11: About the BT_Talk Program
When I plug the USB cable from my PC into the Arduino, a serial communications link is set up. The speed of the link is shown on the serial monitor and the program will configure the Arduino to talk at the same speed. This is the purpose of
This communication link uses digital ports 0 and 1 as rx and tx.
Now we need to set up a serial communications link between the BT module and the Arduino. This needs to be on different pins so that we don't have both the PC and the BT module trying to talk to the Arduino on the same connection.
loads some software that allows us to set up additional serial ports. Putting
creates a new instruction called BTSerial which works the same as the Serial instruction but uses pins 2 and 3 for rx and tx. So when the program uses Serial it is talking to the Serial Monitor and when it uses BTSerial it is talking to the BT module. I need to say what speed the Arduino should talk to the BT module so I have put the BT default comms speed of 9600,
When the BT module receives data it buffers it and waits for the attached device to ask for it.
BTSerial.available() is true if there is data waiting and
c = BTSerial.read();
puts buffered data into variable c which is then sent to the Serial monitor with
The loop keeps trying to read the BT module and write to the Serial Monitor as long as the program is running.