Using arduino for BT module configuration

Hello all,

After some research regarding using arduinos with Bluetooth modules I have deduced:

You CANNOT serially talk to the arduino AND to the BT module if you have wired the BT module to the default TX/RX gates. You either have to connect the BT module without the arduino being powered by USB and talk to it or mask 2 other pins to "impersonate" TX/RX.

However I am OK with just being able to talk to one device at a time the problem is when I want to use the arduino to configure the module with AT commands. Can I do this using the default TX/RX or do I have to go about it the serial software way?

In simple terms I guess what I am asking is:

Is it possible to connect a BT module to the default TX/RX ports of the arduino and then connect the arduino to the PC and then open up a serial monitor (arduino IDE, PuTTY, Hyperteminal, etc) with the AT mode settings (38400 baud, etc) in order to configure the bluetooth?

Not sure if you have already purchased your hardware, but what your describing looks like you would want something similar to this. I personally have one, and it allows the board to be switched into AT mode so you can send it it AT commands via a serial monitor. I have successfully sent several commands to the board using the Arduino IDE serial monitor.

BUMP! So new faces can see this...

A definitive answer is all I'm looking for as there seems to be so much conflicting information out there :/ ...

EDIT as I only just saw the reply:

ipatch: Not sure if you have already purchased your hardware, but what your describing looks like you would want something similar to this. I personally have one, and it allows the board to be switched into AT mode so you can send it it AT commands via a serial monitor. I have successfully sent several commands to the board using the Arduino IDE serial monitor.

Thanks for your reply ipatch! I have looked into this and am considering it as possible alternative solution, however even with BLUNO it seems that it's primary use is for receiving wireless data and then the board acts according to that data. I want the opposite: I want the board to act as a means of communication to connect the module with a PC serial port (through default TX/RX [pins 0 + 1]) so I can then configure the modules' baud rate, password, etc (AT commands).

Unfortunately I have already bought the hardware, hence I would like to know if I it is possible to this or not. A thought that just occurred to me though would be to check out the schematics (if there are any) of the BLUNO to see how they have integrated the BT module to work both in simple communication method and AT mode....

Why not just send the AT commands over the bluetooth connection? If you enter and exit AT mode quickly enough the connection will not be lost.

For my HC05(i'm guessing your using such moduke) i made some custom commands for setting baudrate,pincode,device name etc. one the commands is use is "AT-SETPIN=2153" The arduino processes it as follows: - listen for the string "AT-SETPIN=" - if that string found, read the next 4 characters and store it for later use - enter AT mode by pulling pin34 HIGH (use a digital pin for this) - delay for 50 milliseconds to things to settle - Send AT-Command: Serial.print(F("AT+PSWD=")); - Append the stored 4 digit pin: Serial.println("2153"); - Read all incoming bytes for one second and store them in a char array - Exit AT-command - delay for 50 milliseconds - Send all the characters in the char array over bluetooth.

Why not just send the AT commands over the bluetooth connection?

Another possible solution... still would be good to find out if they way I described is possible or not.

If I understood correctly you are saying connect the BT module to the arduino as in normal communication mode (bluetooth TX/RX connected to pins 0 + 1 respectively). The arduino will have a sketch where if it identifies AT commands and if valid it pulls pin 34 HIGH, and then writes the AT command with Serial.print(); ?

yes you understood it correctly. you have the arduino execute the AT command and it will pass along the response over bluetooth when the command mode has been disabled.

I tried it with no avail :( ... After the arduino pulls pin 34 HIGH it enters AT mode and then I do Serial.print("AT"); but no "OK" back.

To eliminate any abeyances I thought maybe I should write a sketch configured at 38400 baud, power Bluetooth with AT mode on (as opposed to entering AT mode after powering on) And set the loop() function to constantly send "AT" to the serial port. Logically if I connect to it through PuTTY I should see a constant feed like :

'OK' 'OK' 'OK' 'OK'

Any thoughts??

EDIT:

OK I may have had a breakthrough: http://www.youtube.com/watch?feature=player_detailpage&v=msYoxQ_NQZ0

Unfortunately my Russian is very poor :P, hence I can only deduce that this dude is saying that in order to send AT commands through the arduino you must remove the micro controller (doh?) and connect TX->TX, RX->RX. What baffles me is that in his comments he has attached a sketch which utilizes SoftwareSerial but that may just be additional information/resources.

I will try this method later on, along with my previously proposed sketch idea :grin:

OK so results:

  • You CAN configure the module through default TX/RX as long as the MCU is off.
  • Now the sketch way i described brings up one problem, the module has to be switched off after AT commands are sent in order to go back into normal communication mode which means i need to power it through a I/O PIN but this way only works if the arduino is powered through USB... if its powered through battery then nothing. Any thoughts? Not enough current?

Let me correct you. - You CAN configure the module through default TX/RX when the MCU is ON (i'm currently doing exactly that in the project i'm working on)

Why it is not working for you is probably because you're issueing the commands incorrectly. Every command has to end with a carriage return. instead of

Serial.print("AT");

use

Serial.print("AT\r\n")
// or 
Serial.println("AT");

Also do NOT attempt to power the HC05 directly through a IO pin when your using a 5V arduino . Doing so can damage the Bluetooth module. One of my modules was damaged in such a way to pin34 was constantly in a HIGH state resulting in a module which could not exit AT mode anymore. If the module is voltage regulated (breakout board) you should be OK. But there is catch. Putting a load on any IO pin of the arduino will result in a voltage drop. With a single IO pin the HC05 will draw so much current that the IO pin voltage will drop below the minimum for the HC05. You would need to power the HC05 through 3 or 4 IO pins for it to work properly.

In the case of:

Serial.print("AT\r\n")
// or 
Serial.println("AT");

This is done by the sketch though, and in order for me to be able to tell the sketch to do so the arduino cannot be plugged into USB connection. Unless I upload a sketch that constantly just loops Serial.println("AT"); but then that has no point. This is why I need to be able to turn the BT module on and off through the arduino:

  • Arduino is powered on but bluetooth module is not
  • User sends an AT command through serial monitor
  • Sketch recognises command
  • Arduino sets PIN 34 HIGH.
  • Arduino powers module by sending 5V through one of the I/O pins.
  • BT has powered on in AT mode
  • Sketch now sends the proper AT command and reads the response
  • Now we have to print the response back in serial monitor (but how? since we are still in AT mode..)
  • Arduino powers off BT so it doesn't interfere with SM
  • Sketch prints the response in SM

Now I know its possible to enter AT mode after the module has been powered on so you could say that eliminates the problem with step 5 but I still need to be able to restart the module in order to exit AT mode.

You mentioned its possible to power the module through pins 3 or 4 however I tried this and it didn't work and that still doesn't explain why it works when the arduino is drawing power through USB but not through DC.

but I still need to be able to restart the module in order to exit AT mode.

Pull pin34 LOW after your done to exit AT-mode, it will enter it's normal operational mode and any connection is resumed.

  • Sketch now sends the proper AT command and reads the response
  • Now we have to print the response back in serial monitor (but how? since we are still in AT mode..)

Now you know how to exit AT-mode without restarting, you should now the answer to this question. After you exit AT-mode just Serial.print back the response you read with your sketch.

You mentioned its possible to power the module through pins 3 or 4 however I tried this and it didn't work and that still doesn't explain why it works when the arduino is drawing power through USB but not through DC.

This shouldn't even matter anymore since you don't need to powercycle the module anymore. Just connect it directly to VCC. It is also way more reliable. Unlike powering it through the IO-pins,where the output voltage fluctuates based on the load, the output voltage will remain stable. It's generally a bad idea to power a BT module (with loads of around 30 to 40mA) through a single IO pin.

If you must powering it through 3 pins should work. that is only if the bluetooth module(which is 3.3V) is powered through a voltage regulator. Could you share which BT product you are using (i hope it's a module on a breakout board)

OK after a lot of tinkering I have managed to achieve what was said in above post. Thank you racquemis and everyone else who helped out! For future reference and archiving purposes, here's how i did it:

-Setup HC-05 with TX/RX going into Arduino RX/TX respectively and KEY pin going to PIN 2 on the arduino. -Set up arduino pin 2 in your sketch to be an output -Make your sketch so if a certain key pressed ('!' in my case) to activate AT mode:

else if( BluetoothData == '!' )
    { 
      answer = NULL;
      Serial.println("Entering AT mode..");
      delay(100);                                //delay otherwise digitalwrite below is accessed before the above println manages to finish
      //ENTER AT:
      digitalWrite(pinAT, HIGH);       //activate AT 
      delay(50);                                  //50ms delay to settle
      Serial.println("AT");                  //send AT command (should get back 'OK')
      delay(50);                                  //delay again to make sure we start reading after the response has finished being sent
      while (Serial.available() > 0)    //While there are bytes in the buffer, read each byte and add it to the string
      {
        answer = answer + Serial.read();
      }
      delay(100);                                          //delay again... just in case ;)
      //EXIT AT
      digitalWrite(pinAT, LOW);                  //exit AT mode so we can print our answer string
      delay(100);                                          //delay for things to settle
      Serial.println("Exited AT mode..");    //Notify user has exited AT mode
      Serial.println(answer);                        //Print the string
    }

The string shown will be something like '79751310' this is because it is printed in decimal and if you correspond each letter to their decimal representation you'll notice 79 = 'O', 75 ='K', 13 = Carriage return and 10 = New line which means '79751310' = 'OK\r\n'

HipTex: as there seems to be so much conflicting information out there :/ ...

and

OK after a lot of tinkering I have managed to achieve what was said in above post. Thank you racquemis and everyone else who helped out! For future reference and archiving purposes, here's how i did it:

-Setup HC-05 with TX/RX going into Arduino RX/TX respectively and KEY pin going to PIN 2 on the arduino.

Looks like you've hit some sort of jackpot here........

Could you pls publish the entire code.

Thank you

Good you got it working.

I would however change Serial.print(answer) to Serial.write(answer,length); this will stop the chars from being converted to a decimal.You however need to store the response NOT in a string but in a char array.

From the look of it you are using alot of if/else for all the different command. you could try using a switch statement to clean things up a bit more

I would however change Serial.print(answer) to Serial.write(answer,length); this will stop the chars from being converted to a decimal.You however need to store the response NOT in a string but in a char array.

Good advice, thanks! Think i prefer to use if/else statements through, they don’t affect efficiency do they?

Nick check it out:

/* This program will demonstrate how to control arduino 
   from PC Via Bluetooth using default TX/RX */
//  ARDUINO  : BLUETOOTH
// D1 (TX->) :    RXD
// D0 (RX<-) :    TXD

int BluetoothData; 
int pinLED = 8; 
int pinAT = 2; 
String answer = NULL;
char response = 'N';

void setup() 
{
  Serial.begin(9600);       
  Serial.println("Non-masked Arduino-Bluetooth configuration!");
  Serial.println("Test by typing '1' to turn LED on, '0' to turn LED off");
  pinMode(pinLED, OUTPUT);
  pinMode(pinAT, OUTPUT);    
}

void loop() 
{
  if(Serial.available())       
  {
    BluetoothData = Serial.read();   
    
    if( BluetoothData == '1' )               
    {
      Serial.println("LED ON!");
      digitalWrite(pinLED, HIGH);  
    } 
    else if( BluetoothData == '0' )
    { 
      Serial.println("LED OFF!");
      digitalWrite(pinLED, LOW);   
    }
    else if( BluetoothData == '!' )
    { 
      answer = NULL;
      Serial.println("Entering AT mode..");
      delay(100); 
      //ENTER AT
      digitalWrite(pinAT, HIGH); 
      delay(50); 
      Serial.println("AT");
      delay(50);
      while (Serial.available() > 0)
      {
        answer = answer + Serial.read();
      }
      delay(100); 
      //EXIT AT
      digitalWrite(pinAT, LOW); 
      delay(100);    
      Serial.println("Exited AT mode..");
      Serial.println(answer);
    }
    
  }
  delay(100);  // wait 100ms for next reading
}

It won't have much (if any) impact on performance. it's just more readable when you have tons of different commands

Thanks HipTex, I'm trying to find a spare HC-05 to try this out

I too am a bit suss about all those ifs.....

As much to the point is the LED. I'm not familiar with using LEDs but I think it is essentially redundant as the serial prints you already have suffice. Besides, can't you use a LED already available on pin13 rather than wire up a new one on pin8?

Nick the LED is there to test if the normal communication mode of the bluetooth works. So if for example you connect to the module using a serial monitor like PuTTY (arduino not connected to any usb ports) and press 1 the LED should light up, 0 to turn off. That establishes that we can talk to the arduino through BT. Then if u send '!', that establishes that we can also access AT mode and configure the BT module. You can use pin 13 but that also flashes for other stuff the arduino does and I wanted to isolate such interferences.

Hey all,

This is similar to what I am trying to do. Can someone assist me on how do I get the response? I can set the AT but I cannot grab the response. Please assist.

String answer = NULL;

void setup() {
    Serial.begin(115200);               //initial the Serial
    Serial.print("AT+NAME=BABYG\r\n"); //This works!
}

void loop() {
    Serial.print("AT+NAME=?\r\n");
    delay(50);
    while(Serial.available() > 0) {
        //Need to modify this to return RSSI
        answer = answer + Serial.read();
    }
    delay(100);  //exit AT
    Serial.println(answer);  //it prints AT+NAME=?
}