Bluetooth Shield and Arduino connection help

Arduino : ATMEGA328
Bluetooth : e-gizmo Bluetooth shield (Using EGBT-045MS)
http://www.e-gizmo.com/KIT/images/bluetoothmodule/EGBT-045MS-046S%20Bluetooth%20Module%20Manual%20rev%201r0.pdf

Hi there!,

i want to interface my arduino and bluetooth shield. I tried many sample code and here is what i got.

#include <SoftwareSerial.h>
#define recvpin 0
#define transpin 1

SoftwareSerial bluetooth(recvpin,transpin);

void setup()  
{
  Serial.begin(9600);
  pinMode(recvpin, INPUT);
  pinMode(transpin, OUTPUT);
  InitializeBT();
}

void loop() {
  char recvChar;
  if(bluetooth.available()){
    recvChar = bluetooth.read();
    Serial.print(recvChar);
  }
  if( Serial.available() ) {      
    recvChar = Serial.read();
    bluetooth.print(recvChar);
  }
}

void InitializeBT() {
  bluetooth.begin(9600);
  bluetooth.write("AT\r\n");
  bluetooth.write("AT+NAME=testBT\r\n");
  bluetooth.write("AT+LINK=1D35:94:95B2C5\r\n");
  delay(2000);
  bluetooth.flush();
}

Could someone tell me what i am doing wrong? i was able to successfully change the Bluetooth name but this is intermittent (like 1 out of 10 tries i get to change the name! =()

a few questions please:

  1. I am confused between “Serial.begin(9600);” and “SoftwareSerial bluetooth(recvpin,transpin);”. what is the difference between the two? can i use Serial only? or do i really need to create a SoftwareSerial object?

  2. I want to check the response of the bluetooth when i issue an AT command (ex. AT<\r\n> → resp : OK) but i don’t see any response in the Serial Monitor. i checked the pin mapping and i am pretty sure that Rx->TX and TX-RX connections in my Arduino and BT shield. The code in the loop are supposed to print the response in the serial monitor but i don’t why its not working. :frowning:

i found a similar topic http://arduino.cc/forum/index.php/topic,105045.0.html. i tried his code but it still doesn’t work properly.

help is much appreciated. thank you :slight_smile:

Every AT command is supposed to return a response. Reading the responses would be a good thing. Dumping any responses with reading them is NOT a good idea. At least not until you are absolutely certain that the commands are correct, and that the device does what you expect when issued a command.

  1. I am confused between "Serial.begin(9600);" and "SoftwareSerial bluetooth(recvpin,transpin);". what is the difference between the two? can i use Serial only? or do i really need to create a SoftwareSerial object?

The Serial.begin() call tells the Serial instance what speed to use when communicating with whatever is on the other end of the serial port.

The SoftwareSerial declaration creates an instance of the SoftwareSerial class, using the specified pins. You also need to call bluetooth.begin() to specify a baud rate, to start/enable communications on the software serial port.

but i don't see any response in the Serial Monitor.

Calling flush() was not a good idea.

The SoftwareSerial instance knows the mode that the input and output pins should be in. It doesn't need your "help" in setup().

ok, thanks for the response paul! ^_^

i did remove the following code.

  pinMode(recvpin, INPUT);
  pinMode(transpin, OUTPUT);
bluetooth.flush();

i am still not getting any response from the bluetooth shield using serial monitor. Do i have to setup a separate serial terminal for getting the response from BT Shield?

Here are my test setup:

  1. Correct Pin assignment RX -> TX TX -> RX on my Arduino and bluetooth shield
  2. Correct Pin assigned in the RX,TX of SoftwareSerial Object (pin 0 and pin1 using ATMEGA328)
  3. For Hardware side, My Arduino is powered by USB (connecte to PC) while my Bluetooth shield is powered by an external supply
  4. the Bluetooth Shield is mounted on my Arduino board
  5. Checked the cmd pin (pin34) of bluetooth module and it is around 0v to 3.3v when i ran my circuit
  6. the BT shield is visible on my HTC Phone and i am able to pair the device but not connected
  7. Tried baud rate 9600 and 38400 -> still no response

what i want is to view the response from my bt shield so that i will know if the command was accepted :(

is there something wrong with my setup?

  1. Correct Pin assigned in the RX,TX of SoftwareSerial Object (pin 0 and pin1 using ATMEGA328)

There is NOTHING correct about trying to use the hardware serial pins for software serial.

There is NOTHING correct about trying to use the hardware serial pins for software serial.

ok, i think i got.

Removed the SoftwareSerial object and used the Serial object only modified the code as follows:

if( Serial.available() ) {    
    recvChar = Serial.read();
    Serial.print(recvChar);
}

^i am able to see a response in the serial monitor. :)

One question though, Do i assume that every at command that i send is successful? or should i check the response first?

Serial.print("AT+VERSION\r\n");
if (Serial.read() == 'OK'){
   // Proceed 
}

Thank you very much paul... :sweat_smile:

Do i assume that every at command that i send is successful?

Absolutely not.

or should i check the response first?

Yes. Not that way, though. The Serial.read() function returns one character at a time. 'OK' is NOT one character.

The response will not be immediate, either.

hello again, :blush:

i've manage to receive the response from bluetooth but it is displaying garbage characters

AT+ROLE?
ER+ROLE:0LE:0EDSTATE:PAIREDRABLE  ???Uì~Ï

i tried different terminators for readBytesuntil. but it still displays the above msgs.

 if ( Serial.available() > 0 ) { 
    Serial.readBytesUntil(10, serialdata, 80);   // also tried "OK\r\n" 
    Serial.println(serialdata);
  }

Also is there a sequence of at command to execute to be able to connect to my android phone? I tried the following: 1. AT+ROLE = 1\r\n // set mode = master 2. AT+INIT\r\n 3. AT+PAIR=1C48,94,92C1C5,60\r\n 3. AT+LINK=1C48,94,92C1C5\r\n

It is able to send pairing request to my phone. but after pairing, it is displayed as "Paired but no connected" status :(

i also tried setting mode as slave (AT+ROLE=0). still unable to connect. any help is appreciated.. =(

Thank you...