Bluetooth Module AT Commands Setup

pmolsen

Senior Member
Not sure if this has been posted before. This is how to do the initial setup of one of those cheap bluetooth modules from a Picaxe (18M2+) using AT commands. It is necessary if you want to change the defaults, which is often highly desirable. eg. the Picaxe struggles to handle any significant volume of data at 9600. The default pairing password is also no good for anything requiring a bit more security. A meaningful name is also desirable when searching for the device, especially if you have several of them.

This is the module I bought:
http://www.ebay.com/itm/ws/eBayISAPI.dll?ViewItem&item=130545269839 which has both 5V and 3.3V input plus a switch for selecting program (AT) mode or normal running mode. It comes with 3 different pin headers to choose from to solder in, or you can solder wires directly into the four holes.

The module is connected to the Picaxe board's gnd and 5V. Tx from the module goes to an input on the Picaxe. Rx from the module goes to a Picaxe output. You don't have to connect both Tx and Rx. You only need one if you will be doing one-way transmission only. You do not have to acknowledge transmissions etc. The Bluetooth part handles all of that.

You can direct-connect TX/RX to the pins on the Picaxe. No need for a pull-up or pull-down resistor. I actually have a 10k pull-up resistor on the Tx line from the module to the Picaxe (ie. other side of resistor connected to 5V) due to modifying an existing board that previously used 433.92MHz radio receiver. It needed the pull-up resistor. It works fine with it left in place. I do not think it would work if it was a pull-down resistor (connected to Gnd) since the lines idle high. (ie. when not transmitting/receiving the Tx/Rx lines from the module are at +5V).

One tip when transmitting, as per the Serout description in the manual, set the line high just before transmitting otherwise the first byte may be corrupted. See code below.

NB!!! The module has a switch to select AT or normal plus another switch to select 5V or 3.3V. If you are running at 5V then glue or Blu-tack the switch in the 5V position, otherwise one day you will DEFINITELY switch the wrong switch by mistake and let the magic smoke out of the module.

To program it, POWER OFF, set the switch to AT, power up, then run the following program. It is a one-time setup to set the values you want so change them to your own values. They are stored in non-volatile EEPROM. You could include the code at the start of your program but it is not really needed again.

After you have set it up how you want it, POWER OFF, switch the switch away from the AT position, then power up and download your actual Bluetooth program to the Picaxe and enjoy yourself.

Note that the 8000 timeout value (1sec at 32MHz) on each serin lets the Bluetooth module complete each command and also lets all of the serial data finish. Without any delay it does not work. I tried 1000 delay (125ms) but it did not work properly. Somewhere in between may work if you want to experiment, or else just leave it as is.

Note also that the first "at" command sent to the module is not really necessary. I found that sometimes the initial command failed, due possibly to the initial line state, so like this it will be that unnecessary command that will fail.

NAME can be up to 22 bytes long and can contain any characters including spaces.

PSWD can be up to 16 bytes long and can contain any characters, not just numbers.

Code:
' Program to initialise bluetooth module
' NB. Set slide switch to AT before run and set it other way after
' Change the NAME, UART, PSWD values and tx/rx pin numbers as required

symbol tx = b.5
symbol rx = c.0

setfreq m32
high tx  ; idle high

' Set the required values
serout tx,t38400_32,("at",cr,lf)  ; not really necessary
serin [8000],rx,t38400_32,b0,b1,b2,b3  ; OK cr lf

serout tx,t38400_32,("at+name=My Module",cr,lf)
serin [8000],rx,t38400_32,b0,b1,b2,b3  ; OK cr lf

serout tx,t38400_32,("at+pswd=2468",cr,lf)
serin [8000],rx,t38400_32,b0,b1,b2,b3  ; OK cr lf

serout tx,t38400_32,("at+uart=4800,1,0",cr,lf)  ; 4800, 2 stop bits, no parity
serin [8000],rx,t38400_32,b0,b1,b2,b3  ; OK cr lf


' Confirm specified values were actually set
serout tx,t38400_32,("at+name?",cr,lf)
serin [8000],rx,t38400_32,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27
setfreq m4
sertxd (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,cr,lf)
setfreq m32
bptr = 0
do while bptr < 27
  @bptrinc = 0  ; clear fields since serin doesn't
  loop

serout tx,t38400_32,("at+pswd?",cr,lf)
serin [8000],rx,t38400_32,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27
setfreq m4
sertxd (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,cr,lf)
setfreq m32
bptr = 0
do while bptr < 27
  @bptrinc = 0  ; clear fields since serin doesn't
  loop

serout tx,t38400_32,("at+uart?",cr,lf)
serin [8000],rx,t38400_32,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20
setfreq m4
sertxd (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,cr,lf)
 
Last edited:

pmolsen

Senior Member
Just a note about the setup. In testing from a PC (using Tera Term) I found that 9600 was too unreliable. I also found that 4800 with 1 stop bit was also unreliable.

By unreliable I mean that if the Picaxe program tried to do any processing in the middle of receiving data (for example incrementing a counter) it would miss a few incoming bits and get out of sync and corrupt the data.

I found that 4800 with 2 stop bits (thanks Hippy for the suggestion) worked much better and allowed me to safely do a few statements in between serin statements.

I therefore programmed the module that way, as per the sample code above.

I was worried that transmission from the Picaxe to the Bluetooth the other way would not work, since the Picaxe always uses 1 stop bit and can't be changed, but both the Bluetooth module and Tera Term accepted the return data with no problems.

There is another big pain with testing Bluetooth from a PC (Windows 7). Every time you power the module off and back on again it loses the Bluetooth connection to the PC and won't re-pair. There is no option in Windows to force it to re-pair. The only way to reconnect is to Remove Device then Add Device again in the Bluetooth menu, which is very slow. You also have to shut Tera Term down before doing it otherwise the COM port seems to get stuck. When you restart it the COM port to access Bluetooth sometimes changes so you have to go searching for it. (Click Bluetooth icon on task bar, Show Bluetooth Devices, right click the module, Properties, Services to see the COM port, or Click Bluetooth icon on task bar, Open Settings, COM Ports).

Even though it shows two COM ports, one for outgoing and one for ingoing, you can just use the outgoing port in TeraTerm and the incoming data appears in the same window.
 
Last edited:

SAborn

Senior Member
Considering the default pin is 1234 for most of these modules, it is a wise move to change the pin, once all is functioning correctly.
Often we are so happy to have it working we over look these detail.
In my case if they hacked in, i think they might get a little board with watching battery voltages and wind speed data.
 

pmolsen

Senior Member
If it works and doesn't need security then just use it in the default setup that comes with it. That includes leaving it at 9600bps. As long as your serin reads the transmitted data in one statement then it works fine.
 
Top