How can I increase baud rate for bluetooth in Arduino? - bluetooth

I am doing a project in Arduino and want sensor data on my Android. For this purpose I am using HC-05 BT module and Bluetooth Terminal app on Android. But Bluetooth Terminal is receiving data at constant rate (1 sec, I think). And I want to receive data in 10 millisecs. How can I do this?
Here is my code:
#include <SoftwareSerial.h>
int RX=0;
int TX=1;
SoftwareSerial Bluetooth(RX,TX);
int i=0;
void setup() {
Bluetooth.begin(9600);
Bluetooth.println("The controller has successfuly connected to the phone");
}
void loop() {
Bluetooth.write(i);
i++;
delay(5);
}

From here:
The bluetooth module baud rates are set by an hexadecimal index from
‘1’ to ‘C’. Indexes are: 1:1200, 2:2400, 3:4800, 4:9600, 5:19200,
6:38400, 7:57600, 8:115200, 9:230400, A:460800, B:921600, C:1382400
To set a default baud rate, enter this command
AT+BAUD
For example for multiwii, we need 115200 as the BT baud rate, we would
enter AT+BAUD8.
If successful you should be returned “OK” in the serial monitor.

You can change the baud rate of the HC-05 by using AT commands.
Use:
AT+UART=57600,1,0
here is a tutorial

If it is the full code then you sending not printed characters. And also after you will get i=127 you will send characters from upper part of ascii table. And think what will happens when i>255. Try this code in loop:
Bluetooth.println("this is test code")
instead of writing Bluetooth.write(i);
You can get more info about ASCII and more about Serial.print and Serial.write

You can change the baud rate of the HC-06 by using AT commands
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 3); // RX, TX
/*
* Connect pin 2 Arduino to pin TX HC-06
* Connect pin 3 Arduino to pin RX HC-06
*/
void setup() {
Serial.begin(9600);
Serial.println("Enter AT commands:");
btSerial.begin(9600);
}
void loop()
{
if (btSerial.available())
Serial.write(btSerial.read());
if (Serial.available())
btSerial.write(Serial.read());
}
AT
AT+BAUD4==>9600
AT+BAUD8==>115200

Related

Transmit/Receive data Nodemcu(V3) + Bluetooth Module HC-05

I am trying to communicate with the HC-05 Bluetooth module for quite long time now, but with no success.
I am using the Nodemcu(V3) ESP8266 module.
I connect the HC-05 to Nodemcu in following sequence:
HC-05 Nodemcu
----- -----------
RX --> Pin 1 (Tx)
TX --> Pin 3 (RX)
Vcc --> +3.3V
GND --> GND
For starters, i want to check if my Nodemcu is communicating properly with my HC-05 module.
I have written the following code to read the response of AT commands:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(3, 1); // RX | TX
char Bluetooth_Name = ' ';
void setup()
{
// Arduino IDE serial monitor
Serial.begin(115200);
// HC-05 default serial speed for AT mode is 38400
BTserial.begin(38400);
// Wait for hardware to initialize
delay(1000);
// Print debug string
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
reading = BTserial.read();
Serial.println(reading);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
reading = Serial.read();
BTserial.write(reading);
}
}
However, i do not get response for any AT command at all. The serial monitor just shows blank.
Thanks in advance
EDIT:- I connected the "EN" pin on the HC-05 to Vcc. Noe, the led on the HC-05 blinks slowly, which means that the HC-05 is configured in command mode. However, i am still not able to receive the response for any AT commands. I have also selected "Both NL & CR" in the serial monitor, configured the baud rate correctly and double-checked the hardware connections.
Everything seems to be correct except that i don't get response for the AT commands.
Please help!!!
I changed the line
SoftwareSerial BTserial(3, 1); // RX, TX
to
SoftwareSerial BTserial(D4, D3); // RX, TX
And got it to work!

Serial Monitor not responding to input while trying to set HC-05(ZS-040) to AT mode

I can't run AT commands in Serial Monitor. (Both NL & CR + 9600 baud are set)
I followed this tutorial here.
Hardware:
Arduino MEGA 2560
Bluetooth Module HC-05 (ZS-040)
Ubuntu 16.04
This is the way I wired them:
I want to mention that before this I wired them multiple times without the resistors but not for more than 3-4 minutes.
This is the code I use:
// Basic Bluetooth test sketch 5a for the Arduino Mega.
// AT mode using button switch
// HC-05 with EN pin and button switch
//
// Uses serial with the host computer and serial1 for communication with the Bluetooth module
//
// Pins
// BT VCC to Arduino 5V out. Disconnect before running the sketch
// BT GND to Arduino GND
// BT RX (through a voltage divider) to Arduino TX1 (pin 18)
// BT TX to Arduino RX1 (no need voltage divider) (pin 19)
//
// When a command is entered in to the serial monitor on the computer
// the Arduino will relay it to the Bluetooth module and display the result.
//
char serialByte = '0';
const byte LEDPIN = 13;
void setup()
{
pinMode(LEDPIN, OUTPUT);
// communication with the host computer
Serial.begin(9600);
Serial.println("Do not power the BT module");
Serial.println(" ");
Serial.println("On the BT module, press the button switch (keep pressed, and at the same time power the BT module");
Serial.println("The LED on the BT module should now flash on/off every 2 seconds");
Serial.println("Can now release the button switch on the BT module");
Serial.println(" ");
Serial.println("After entering AT mode, type 1 and hit send");
Serial.println(" ");
// wait for the user to type "1" in the serial monitor
while (serialByte !='1')
{
if ( Serial1.available() ) { serialByte = Serial1.read(); }
}
// communication with the BT module on serial1
Serial1.begin(38400);
// LED to show we have started the serial channels
digitalWrite(LEDPIN, HIGH);
Serial.println(" ");
Serial.println("AT mode.");
Serial.println("Remember to to set Both NL & CR in the serial monitor.");
Serial.println("The HC-05 accepts commands in both upper case and lower case");
Serial.println(" ");
}
void loop()
{
// listen for communication from the BT module and then write it to the serial monitor
if ( Serial1.available() ) { Serial.write( Serial1.read() ); }
// listen for user input and send it to the HC-05
if ( Serial.available() ) { Serial1.write( Serial.read() ); }
}
The flash on the LED should change to on/off every 2 seconds, 1 second
on, 1 second off. This indicates AT mode. You can now release the
button switch.
Even though the LED is blinking like described (quote from above)
I get no response from typing 1 in Serial Monitor.
This is how my Serial Monitor looks after running everything as supposed:
Solution
Connection:
Bluetooth RXD to Arduino Mega pin 11
Bluetooth TXD to Arduino Mega pin 10
Bluetooth VCC to Arduino Mega 5V
Bluetooth GND to Arduino Mega GND
I used this code:
#include <SoftwareSerial.h>
#define RxD 10
#define TxD 11
SoftwareSerial BTSerial(RxD, TxD);
void setup(){
// replace BAUDRATE as suggested
BTSerial.begin(BAUDRATE);
Serial.begin(9600);
BTSerial.print("AT\r\n");
}
void loop(){
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}
after setting AT mode as described in the tutorial above.
I tried running the code with BAUDRATE starting from 9600 to 460800:
For me 9600 was the right baud rate of my HC-05.

Configuring and pairing 2 HC-06 Bluetooth modules as Master and Slave using Arduino UNO

I have been trying to establish connection between two HC-06 Bluetooth modules. Pairing has been done. The two modules are communicating. My aim is to send a letter from one module and receive acknowledgment from the other module. The code for the master module is below.
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX, TX
char c;
char s[]="Matched";
int t[]="NotMatched";
void setup()
{
// start the serial communication with the computer
Serial.begin(9600);
Serial.println("Arduino with HC-06 is ready");
// start communication with the HC-06 using 38400
BTserial.begin(38400);
Serial.println("Bluetooth serial started at 38400");
}
void loop()
{
// Read from HC-06 and send to Arduino Serial Monitor
if (BTserial.available())
{
c=(BTserial.read());
if (c=='a')
{
Serial.write(s);
}
else
{
Serial.write(t);
}
}
// Read from Arduino Serial Monitor and send to HC-06
if (Serial.available())
{
c = Serial.read();
Serial.write(c);
BTserial.write(c);
}
}
Similar code is used for the slave module. Except for the 'else' part in the code everything runs right. I receive acknowledgement along with the else portion being printed twice for both the if and else portion of the code i.e 'matched not matched not matched' is printed when it receives char 'a' and 'not matched not matched not matched' is printed when it receives anything other than 'a' . Can you please give me suggestions on what could be wrong.
Have you printed the characters that you are reading?
I had a similar problem and I found that the Bluetooth was receiving garbage characters. The reason was because the standard baudrate, of HC-06 modules, is 9600, so if you change:
BTserial.begin(38400);
to
BTserial.begin(9600);
In both master and slave, it might work.

Bluetooth HC-05 returning raw values to serial Monitor

I am trying to connect Bluetooth HC-05 to Arduino but facing the following problem.
This is Arduino code:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(19, 18);
char data=0;
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission
Serial.println("Arduino is ready");
Serial.println("Remember to select Both NL & CR in the serial monitor");
BTserial.begin(38400);
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data and store it into variable data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n"); //New line
if(data == '1') //Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') //Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
The out put displayed in the serial monitor is below:
<x⸮x⸮xxx⸮⸮x⸮x⸮x⸮x⸮x⸮x⸮⸮⸮xxx
You are using a software serial which is used to make any gpio pins as 'Rx and Tx'. Now the data you receive will come to your board through these pins in this case '19 and 18'but you are checking for data from your other Serial. Check for data availability from 'BTserial' like this 'BTserial.available()' and similarly for 'serial.read' too.
Hope this helps.
You can learn more about software serial from this link
Look like wrong baudrate try the following code BTserial.begin(9600);
What is the baud rate you are using on Serial Monitor.Select
9600 baud rate
on serial monitor

Can't find HC-05 communications speed (findBaud)

I am trying out Arduino Uno + HC-05 using this library. I don't think it is working properly. This is the Serial Monitor's output of the example "echo":
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... x
Trying 57600... x
Trying 115200... x
No connection
No Connection, waiting...OK
None of the Communication speed works, but I manage to connect my Android phone (w/ Bluetooth Terminal) to the HC-05, which is why you see "OK" at the end of the output. But it is not able to echo my input from the Bluetooth Terminal.
The Arduino code:
#include <Arduino.h>
#include "HC05.h"
#include <SoftwareSerial.h>
HC05 btSerial = HC05(A2, A5, A3, A4); // cmd, state, rx, tx
void setup()
{
DEBUG_BEGIN(57600);
btSerial.findBaud();
}
void loop()
{
btSerial.println("Echo Server- type something");
while (btSerial.connected())
{
if (btSerial.available())
{
btSerial.write(btSerial.read());
}
}
}
How I connect the HC-05 to Arduino:
I just follow the instruction(5V and GND; State, Rx and Tx to A5, A3 and A4 respectively) , except I don't have pin "cmd", but I do have pin "CFG", so I just assume that should be cmd and connect it to A2
(I know I should comment instead of writting an answer, but I need 50 reputation)
Same here.
I tried with and without the lib and with different pins. I also tried an arduino micro and uno.
Always the same: the module's LED blink (when I send data trough RX/TX it seems) but I can't have any answer (neither when switching to command mode with "$$$" and neither with AT commands with KEY pin - on my module it is named EN, maybe for enable, I assume it is the same)
EDIT:
I should have RTFM. Especially this.
Here are my findings :
The bluetooth module is called HC-05 and the whole board I have is ZS-040.
The EN pin is used to switch off the module, if set to LOW
The small button switch can be pushed to enter command mode (I don't have a KEY pin)
Speed is 9600 bauds by default and stay the same when entering command mode
To debug your connection you can use the Android app called BlueSerial
For the record, here is my code :
#include <SoftwareSerial.h>
#define HC05_STATE 5
#define HC05_RXD_ARDUINO_TXD 4
#define HC05_TXD_ARDUINO_RXD 3
#define HC05_EN 2
SoftwareSerial BTSerial(HC05_TXD_ARDUINO_RXD, HC05_RXD_ARDUINO_TXD); // RX | TX
void setup(void)
{
pinMode(HC05_EN, OUTPUT);
digitalWrite(HC05_EN, HIGH); // just to be sure to enable the module, if not plugged it still works
Serial.begin(9600);
BTSerial.begin(9600); // default speed
Serial.println("Ready");
}
void loop(void)
{
if (BTSerial.available())
{
Serial.print("< ");
while (BTSerial.available())
Serial.write(BTSerial.read());
Serial.print("\n");
}
if (Serial.available())
{
Serial.print("> ");
while (Serial.available())
{
char c = Serial.read();
BTSerial.write(c);
Serial.write(c);
}
Serial.print("\n");
}
}

Resources