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.
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: