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

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");
}
}

Related

SPI conflict between SD.h and U8x8.h (LCD)

I want to reproduce a .wav file on my Arduino Uno via an SD card while at the same time using an LCD display (Nokia 5110). I'm having trouble trying to make the double SPIs work and since I'm not really familiar with the libraries that I used I decided to ask for help
The problem is that once the program execute SD.begin() the display stops working and the SPI communication is working only on the SD.
In this setup that I'm showing I will initialize the SD in the setup() and in the loop using a button I'm supposed to write stuff on the display:
//Libraries
#include <U8x8lib.h>
#include <SPI.h>
#include "SD.h"
#include "TMRpcm.h"
//Display (pin 13, pin 11, pin A1, pin 12, pin A0 )
U8X8_PCD8544_84X48_4W_SW_SPI lcd(LCD_CLK, LCD_DIN, LCD_CE, LCD_DC, LCD_RST);
//Speaker
TMRpcm music;
void setup() {
//Serial port -> Open
Serial.begin(9600);
while (!Serial) {}
//LCD
display_setup();
//SD and Speaker
sdAndSpeaker_setup();
}
void loop() {
topButton.poll();
if (topButton.buttonClicked) {
display_something();
}
}
Ignore the function related to the button, I removed most of its code to avoid cluttering the page.
Here's when the problem occurs:
void sdAndSpeaker_setup() {
music.speakerPin = 10;
// (pin 4)
if (!SD.begin(SD_CS)) {
Serial.println("SD fail");
Serial.flush();
abort();
}
music.setVolume(4);
music.play("Daybreak.wav");
music.quality(2);
}
I know that by changing the pins of display to other value (rather than 11, 12 and 13) the problems is solved but most of the pins in my board are already used and I don't have this luxury... I think this has something to do with the U8x8.h and SD.h library but I don't have the skills to tweak those.
If anyone has a suggestion I'll appreciate!
Thanks!
EDIT:
Thanks to Juraj for the clarification, here's my new changes for the corrected constructor:
U8X8_PCD8544_84X48_4W_HW_SPI lcd(LCD_CE, LCD_DC, LCD_RST);
And for the setup_display()
void display_setup() {
lcd.begin();
pinMode(LCD_LIGHT, OUTPUT);
digitalWrite(LCD_LIGHT, HIGH);
lcd.setFont(u8x8_font_chroma48medium8_r);
lcd.drawString(1,2, "Hello!");
}
Unfortunately it still doesn't work...
The constructor
U8X8_PCD8544_84X48_4W_SW_SPI(uint8_t clock, uint8_t data, uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE)
creates a 'driver' which uses software SPI. But you supply pins of hardware SPI as parameters. Hardware SPI is used by the SD library over the SPI library to access the SD card, so hardware SPI conflicts with the software SPI of the display library.
There is a
U8X8_PCD8544_84X48_4W_HW_SPI(uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE)
constructor which uses the hardware SPI over the SPI library. The SPI library 'knows' the pin numbers of the SPI pins. (SPI library is part of the boards package. It can't be installed separately.)
If you want to use the software SPI, use free pins. The hardware SPI uses pins 11, 12, 13 on Uno.

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.

How can I increase baud rate for bluetooth in Arduino?

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

Resources