Monday, June 10, 2013

JY-MCU BT board V1.05 configuration with Arduino

Configuring the cheap bluetooth module from ebay is more difficult than appears at first, especially since datasheets provide conflicting instructions for programming the chip. I found useful another blog post, although they do not provide explicit Arduino code for configuring the bluetooth module.

Wiring
Arduino Mega 2560 JY-MCU
TX1 (18) RXD
RX1 (19) TXD
+5V VCC
GND GND
By default, the module uses 9600 baud rate. The task is to configure it for 115200 bps using the AT command set. If this is the first time module is being configured, change "Serial1.begin(115200);" to "Serial1.begin(9600);".

Code for Arduino

int led = 13;
/* used help from http://byron76.blogspot.com/2011/09/one-board-several-firmwares.html */
void setup() { 
 pinMode(led, OUTPUT);
 Serial.begin(115200);
 Serial1.begin(115200);//use 9600 if first set up
}
void loop() {
 digitalWrite(led, HIGH);//sanity check
 command("AT",2);// response: OK
 command("AT+VERSION",12);// response: OKlinvorV1.5
 command("AT+NAMEArduino",9);//response: OKsetname
 command("AT+BAUD8",8);//response: OK115200
 while(1);//stop execution
}
void command(const char* cmd, int num_bytes_response) {
 delay(1000);
 Serial1.print(cmd);
 delay(1500);
 for (int i=0;i<num_bytes_response;i++)
 Serial.write(Serial1.read());
}

Edit 2013-08-21: Fixed a typo in the code (character "<" not displayed properly)

Compile and upload the above program to the Arduino.  Open the serial monitor (at 115200 baud rate) to check whether the module responds properly. Do not pair to the bluetooth module until after configuration is complete. Without the delay commands, it appears that the bluetooth chip cannot keep up. This code can be easily expanded to send other commands to the module as well. 

10 comments:

  1. Thank you for this code. But "lt" generates an error.

    ReplyDelete
  2. What kind of error are you getting?

    ReplyDelete
    Replies
    1. Sorry, i got no Reply on Mail. The Error was myself. Is this a permanent adjustment?

      Delete
    2. I'm not 100% clear, were you able to get this to work successfully? The parameters (name and baud rate in this case) are changed permanently, and will remain if the device is unpowered.

      Delete
  3. error: Serial1 was not declared in scope. aparece essa mensagem, o que fazer?

    ReplyDelete
    Replies
    1. Serial1 is for Arduino Mega only. See more detailed description here:
      http://arduino.cc/en/Serial/Begin. It seems that you are using a different board, or did not specify the correct board during compilation. If you only have 1 serial on your board, you can try to use "SoftwareSerial" library http://arduino.cc/en/Reference/SoftwareSerial as a replacement for Serial1.

      When reporting errors, please include line numbers so that it's obvious where they are coming from.

      Delete
  4. Now it works fine. You have to add 2 lines.

    pinMode(40, OUTPUT);
    digitalWrite(40, HIGH);

    PIN 40 is connect to KEY. After the setting, disconnect PIN 40.
    Can you tell me the second Parameters. "command (" AT + PIN1956, "?);

    ReplyDelete
    Replies
    1. Glad it's working for you. The second argument to the "command" function is the number of bytes you expect to receive in response. You can just set it to a large number if you are not sure, count how many bytes you receive, then change program accordingly. Also, no spaces. You can try:
      ----------
      command("AT+PIN1956",8);
      ----------
      It seems that the response should be "OKsetPIN".

      Delete
  5. Why do i need to set it to 115200 baud rate?

    ReplyDelete
    Replies
    1. It depends on your application. For situations where data comes in quickly and needs to be sent to the PC real-time, serial port speed makes a difference.

      Delete