I have an HC-05 Bluetooth module setup with Arduino that controls an LED light through Bluetooth commands.
When the HC-05 is connected to a device, the red light on the HC-05 has a cycle of turning on and off twice at a constant rate, and then has the light off for the same time as half a cycle. Let's call this half cycle with the light off the "waiting period".
When the HC-05 begins the double on/off cycle, any command I send to the HC-05 is delayed until the red light on the HC-05 finishes its cycle.
During the "waiting period" any command I send to the HC-05 is executed instantly without delay.
Is there a solution to this delay?
Here is my Arduino code: It works with any Bluetooth terminal app on the app-store.
SoftwareSerial BTSerial(0, 1);
void setup() {
Serial.begin(9600);
BTSerial.begin(38400);
Serial.setTimeout(2000);
pinMode(7, OUTPUT); // put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
char data= Serial.read(); // reading the data received from the bluetooth module
switch(data)
{
case 'a': digitalWrite(7, HIGH);break; // when a is pressed on the app on your smart phone
case 'd': digitalWrite(7, LOW);break; // when d is pressed on the app on your smart phone
default : break;
}
//Serial.println(data);
//Serial.flush();
}
delay(1);
}```
1 Like
This is the first time I've used this, I deleted them.
Posted code will not compile. Missing library.
Why in the world are you using SoftwareSerial on the hardware serial pins?
I can not confirm this. When I see the first flash, I press send on the phone (with an a or d on the send line ready to go and no line endings) the led lights before or during the second flash.
I am using Kai Morich's Serial Bluetooth Terminal.
I'm not clear on how to access the state of the status led, but timing and observation is tricky so you will need a more precise method of measuring before you can convince me of a latency issue during the flash.
I put the HC05 on a software serial port HC05 TX to Uno pin 4, HC05 RX to Uno pin 7 through a voltage divider. The LED responds instantly to both commands. Serial Bluetooth terminal is the sender.
Having 2 things (HC05 and serial monitor) on the same serial port often does not work well.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4, 7);
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
// Serial.setTimeout(2000); not needed
pinMode(13, OUTPUT); // put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
if (BTSerial.available() > 0)
{
char data = BTSerial.read(); // reading the data received from the bluetooth module
//Serial.println(data);
switch (data)
{
case 'a': digitalWrite(13, HIGH); break; // when a is pressed on the app on your smart phone
case 'd': digitalWrite(13, LOW); break; // when d is pressed on the app on your smart phone
default : break;
}
//Serial.println(data);
//Serial.flush();
}
delay(1);
}
When my HC-05 is not connected to a device, it blinks 5 times a second as it should. However when it is connected, from all the videos I see of it working properly online, It should blink two times fast every few seconds.
When I connect my HC-O5, it blinks twice at a much slower rate. This causes a specific problem.
The specific problem is that when my HC-O5 is blinking twice, any 'a' or 'd' I send via a Bluetooth terminal is delayed until the HC-05 finishes blinking twice. When the HC-05 finishes blinking, the commands I send in the Bluetooth terminal are executed instantly. However the instant the HC-05 blinks twice again, every command waits until it finishes the blinking twice before executing.
Here is the code, the Arduino is being powered by a 9v battery. It is being run through a bluetooth tereminal on an android phone.
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT); // put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
char data= Serial.read(); // reading the data received from the bluetooth module
switch(data)
{
case 'a': digitalWrite(7, HIGH);break; // when a is pressed on the app on your smart phone
case 'd': digitalWrite(7, LOW);break; // when d is pressed on the app on your smart phone
default : break;
}
Serial.println(data);
}
delay(50);
}
What is wrong with the code in reply #7. I tested the code on real hardware and it works fine with no hesitations.
It may be my specific HC-05 doing this. Because it is blinking slower than it should when its connected.
Every single example I've seen online, when the hc05 is connected, it blinks twice and finished both blinks under a second and responds to commands instantly.
Mine takes over two seconds to finish the blinks and it delays and only responds after the two blinks.
Yes, that HC 05 sounds peculiar.
The flash pattern and non response is like it is going into command mode (AT mode) instead of staying in data mode. I have never heard of this before.
the Arduino is being powered by a 9v battery
If this is a PP3 smoke alarm battery then this can cause issues as it can not provide sufficient current. Make sure the Arduino and HC05 have a strong power supply. What happens if you run with a USB connection to the Arduino?
It is a standard Energizer 9v battery, and it does the exact same thing when plugged into a USB. Although with the USB don't I have to use software serial and pins besides 0 and 1 to not confuse it? This is the code I use for USB.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4, 5);
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
pinMode(7, OUTPUT); // put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
if (BTSerial.available() > 0)
{
char data = BTSerial.read(); // reading the data received from the bluetooth module
//Serial.println(data);
switch (data)
{
case 'a': digitalWrite(7, HIGH); break; // when a is pressed on the app on your smart phone
case 'd': digitalWrite(7, LOW); break; // when d is pressed on the app on your smart phone
default : break;
}
//Serial.println(data);
//Serial.flush();
}
delay(1);
}
It could be going into AT command mode. How would I know? The only experience I have with using AT command mode is by holding down the button before plugging the HC-05 in.
This is the code I've tried using for sending AT commands.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); // RX, TX
void setup() {
Serial.begin(38400);
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
Serial.println("Enter AT commands:");
mySerial.begin(38400);
}
void loop()
{
if (mySerial.available()){
Serial.write(mySerial.read());
}
if (Serial.available()){
mySerial.write(Serial.read());
}
}
This is the code I use to attempt to send AT commands however it doesn't respond to it, I have changed it to "Both NL & CR" and switched the baud rate to 38400.
What flash pattern do you see when you deliberately go into AT mode?
Are you cross connected Rx>Tx and Tx>Rx?
If you can actually get into AT mode and see an OK response to an AT command, you should try an AT+ORGL to go back to factory settings.
I do not see how that code can put the HC05 into AT mode. The EN pin must be held high before power is applied to the HC05. Does the module do the AT mode flash (2 second on, 2 second off)?
From Martyn Currey's Arduino with HC-05 (ZS-040) Bluetooth module – AT MODE
To activate AT mode on the HC-05 zs-040 modules pin 34 needs to be HIGH on power up. The small push button switch connects pin 34 to +3.3v so we can either:
– connect pin 34 directly to +3v3v and power on, or
– hold the button switch closed when starting the module.
So pulling EN high in setup does not follow the rule (needs to be HIGH on power up).
And besides that, the HC05 inputs are not necessarily 5V tolerant. Hence the voltage divider on RX.
Plug the jumper from EN to the 3.3V pin (NOT 5V) and power the module. The it will go into AT mode as soon as you power the Arduino.
Neither do I. The flash patterns that @kaiswim1 is seeing make no sense to me either.
My best hope is that the module has somehow gotten into a bad state and can be reset. My actual expectation is that the module is defective.
The 3 flash patterns that I see on my HC05 (ZS-040) modules are:
- rapid on, off flash (2 Hz ish) >> communication mode, not connected.
- 2 rapid flashes (on, off, on, off, takes ≈ 1 second) then a pause (off) of ≈ 2 seconds >> communication mode, connected.
- slow flash, ≈2 seconds on, ≈2 seconds off >> AT mode
OP, is number 2 the pattern that you are speaking of?
Can you post photos of the HC05 front and back, please?
I am not getting any OK when I send commands. Does the code for sending AT commands in #13 work for you?
Describe, in detail step by step, what you are doing. What pins are connected? A schematic would be welcome. What power supply? What is the Bluetooth module flashing at each step? Post the exact code that you are using.
Did you read the Martyn Currey page that I linked?
Yes.
You really need to document for use the flashing patterns you are seeing as described in #17.
Kind of but its too slow. When I set it to AT mode by pushing the button before it plugs in. It does number 3. However when I open the serial monitor and type AT it doesn't say anything. This is the code I sent to the arudino.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(5, 6); // RX | TX
void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
if (BTSerial.available()) // read from HC-05 and send to Arduino Serial Monitor
Serial.write(BTSerial.read());
if (Serial.available()) // Keep reading from Arduino Serial Monitor and send to HC-05
BTSerial.write(Serial.read());
}
This is what my HC-05 looks like