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.