Bluetooth connection lost (HC-05 Modul)

Hello everyone,
I'm working on my first Arduino project and I've come across a problem with the Bluetooth connection that is unsolvable for me.
Short description of the project:
I would like to remote control a lamp from an Arduino. It should switch on at sunset and then switch off again at a certain time. There are three modes available: Manual (on and off), switching according to the time, switching according to brightness. The change of modes and the manual control is done using a Bluetooth connection and a smartphone app.
Now to my problem:
The connection to my smartphone and the app with a HC-05 module is always breaking up. I can connect the Arduino to the smartphone. The module switches from continuous blinking to receiving. Then I can switch the lamp manually (on/off). But after about 30 seconds it is over. No further data is received. The Bluetooth module continues to flash normally as if it is connected, but no further data is received by the Arduino.
If I disconnect the Bluetooth module from the power supply and switch it on again, it can be reconnected and the connection is established again!
Replacing the module was not successful, so it's not the Bluetooth module.
Does anyone know where the error could be, or has a workaround?

Here is my sketch and the structure.
Thanks a lot for your help!

Lukas

#include <virtuabotixRTC.h> 
#include <Dusk2Dawn.h>
#include <RCSwitch.h>

RCSwitch sender = RCSwitch();

virtuabotixRTC myRTC(6, 7, 8);

char Incoming_value = 0;

const int sensorPin = A1;
int value = 0;
int sensorPinValue = 0;

void setup()
{
    Serial.begin(9600);
    sender.enableTransmit(3);
    sender.setProtocol(1);
    sender.setPulseLength(417);


}
void loop()
{
    if (Serial.available() > 0) 
    {
        Incoming_value = Serial.read();
        if (Incoming_value == '1') 
        {
            sender.send("000101010001010101010101"); 
        }
        else if (Incoming_value == '0') 
        {
            sender.send("000101010001010101010100");
        }
        else if (Incoming_value == '2') 
        {
            myRTC.updateTime();
            Dusk2Dawn oberwil(47.512152, 7.559374, 2);
            int laSunset = oberwil.sunset(myRTC.year, myRTC.month, myRTC.dayofmonth, false);            
            char time[6];
            int rtc_time = myRTC.hours * 60 + myRTC.minutes;
            if (rtc_time == laSunset)
                sender.send("000101010001010101010101");
            else if (rtc_time == 1380) 
                sender.send("000101010001010101010100");
            delay(1000);
        }
        else if (Incoming_value == '3')
        {
            myRTC.updateTime();
            int rtc_time = myRTC.hours * 60 + myRTC.minutes;

            sensorPinValue = analogRead(sensorPin);

            if (sensorPinValue < 400 && sensorPinValue > 380 && rtc_time > 720 && rtc_time < 1380)
                sender.send("000101010001010101010101");
            else if (rtc_time == 1380)
                sender.send("000101010001010101010100");

            delay(1000);
        }
    }

}

I think that your power supply is not sufficient for the Bluetooth Module. The Bluetooth module pulls more current than the Arduino. Please tell us that what power supply you use and waht other devices are using which also take power from the project. :slight_smile:

I have connected the Arduino to a 12V / 1.5 A power supply.
The other components are:

  • RTC module, model DS1302, Link

  • Radio transmitter 433 MHz, model XY-FEST, Link

  • Light sensor, model HW5P-1 with resistor 10k (Arduino Kit)

How could I connect the Bluetooth module with a separate external power source?

Hello Luke.

There's more than one thing amiss here.

The correct connections are: HC-05 TX to Arduino RX (pin 0). Your diagram shows TX to TX. That should not work.

As Arnav suggested: power supply. I power my HC-05s with 5v, not 3.3v. I also power them directly from the supply itself, not from the Arduino's 5v pin.

Perhaps you have too many sub systems involved here for troubleshooting purposes. I would prove that you can maintain continuous phone -- HC-05 -- Arduino communication indefinitely without the other components.

Try a BT serial terminal app on the phone and a simple sketch that receives & sends through the h/w serial port.

Do you have a USB/TTL converter? They are very handy.

John.

Ok, I have tried several things, but since I am a beginner I have so far gained more question marks than understanding.

For clarification regarding the TX / TX connections. I have connected it correctly (TX->RX) to the Arduino. This was an error in the diagram.

Here's what I tried:

  1. I recorded the input on my computer with a simple program (see below). It showed that the Arduino receives the input from my APP (0, 1 etc.) and then after 30 seconds nothing comes up.

  2. I disconnected all other devices from the Arduino and left only the HC-05 Bluetooth module running. This did not help.

  3. I connected the Arduino with a separate power source (Adafruit USB to TTL Serial Cable). Same problems.

  4. I downloaded the Serial Bluetooth Terminal for my smartphone. When connecting to the Arduino, it receives the following code (all 20ms):
    VMDPE_1:2:1XXXX:0:243|1XXXX_VMDPE
    While the x’s are changing.

So far I got no clue what to do next :frowning:

Lukas

char Incoming_value = 0;

void setup() {
    Serial.begin(9600);
                                                                             

}

void loop() {

    if (Serial.available() > 0)                                                                          
    {
        Incoming_value = Serial.read();                                                                 
        Serial.print(Incoming_value);                                                                  
        Serial.print("\n");
    }
    else 
    {
        delay(1);
    }
}

Have you kept the Serial monitor open while testing that, this also might be the problem. If you need to use the Serial monitor while the TX and RX are connected to the bluetooth, then you should use SoftwareSerial library to use any other pins as TX and RX for the bluetooth module.

Arnav

lu-ke:
4. I downloaded the Serial Bluetooth Terminal for my smartphone. When connecting to the Arduino, it receives the following code (all 20ms):
VMDPE_1:2:1XXXX:0:243|1XXXX_VMDPE
While the x’s are changing.

char Incoming_value = 0;

Lukas,

Do you have any idea why it is receiving that data? I've never seen it. What you want to see is the IDE and phone passing text that you enter.

As Arnav said, you must use software serial for the BT if you want to use the IDE monitor.

Consistent failure at 30 mins must be a clue to something happening.

Do you have a photo of yr HC-05 or a link to the supplier's page?

The above is not true. If you have Bluetooth on hardware serial, you already know that you must have it disconnected while uploading, but it is quite OK to have serial monitor running when the code is operational. If you do this, you cannot send anything, but you can watch what is going on - I believe that is called monitoring, which sounds like something monitors do.

I have no idea where that stuff is coming from, but you can be sure the monitor is innocent.

  1. I recorded the input on my computer with a simple program (see below). It showed that the Arduino receives the input from my APP (0, 1 etc.) and then after 30 seconds nothing comes up.

Suggests failing power. It also suggests your wiring is definitely OK, so you can safely ignore all that software serial nonsense, and your code is essentially OK, but a delay of (1) would have to be a really pointless waste of time, and it might help if you make it (1000). At least things would be less tiresome to read., indeed, I would scrap the else condition and just have the delay.

I connected the Arduino with a separate power source (Adafruit USB to TTL Serial Cable)

I don't understand this but, if you can find your way to use a plain ordinary 9v wall wart, everybody will understand. Further, I trust you have moved Bluetooth to 5v as advised above.

Thank you all for your suggestions.
As Nick pointed out - I disconnect the Bluetooth module while uploading. There should be no influence for the serial monitor running afterwards. Even if I don't use the serial monitor the Bluetooth stops after 30 sec.
I use the following HC-05 VMA302: HC-05 TRANSMISSION MODULE – Velleman – Wholesaler and developer of electronics

What I found out and find strange:

I have the following power sources:

  1. USB connection to my computer
  2. 9V wall wart
  3. Adafruit USB to TTL Serial Cable 5V only power connected to the breadboard Link

If I connect the Arduino with (1) and the Bluetooth with (3) it works for 30 sec.

If I connect the Arduino with (2) and the Bluetooth with (3) it doesn't work at all.

If I connect the Arduino with (2) and the Bluetooth with the Arduino 5V power it works for 30 sec.

If I connect the Arduino with (1) and the Bluetooth with the Arduino 5V power it works for 30 sec.

I'm not sure if my problem really comes from the power supply...

I can reconnect the HC-05 when I restart the module or when I turn off and on the Bluetooth on my phone.

Could it be a problem with my phones Bluetooth connection?

I just connected the Bluetooth to my computer and tried it. As it seems the connection is stable. But I still receive a constant string of data from the Arduino consisting of the same "VMDPE_1:2:1XXXX:0:243|1XXXX_VMDPE" data. Perhaps this sets my phone off?!

  1. Adafruit USB to TTL Serial Cable 5V only power connected to the breadboard

I have no idea what that it, but I'm sure you don't need it. Two power supplies is surely enough. On matters of power, I can only assume you have moved Bluetooth to 5v, you deserve all the grief you get if you refuse to do this.

Having said that, it seems to me that the problem is code, which I thought was suss, but I was only guessing. I now see that VMDPE is not gibberish, it is actually googlable, and it seems there is something that you are not telling us about what you are doing.

Does Visual Micro Pro ring any bells?

https://forum.arduino.cc/index.php?topic=414676.0

I'm guessing something lies there, and your phone is innocent.

Thanks Nick!
That was one big step forward in my project!

The stupid Visual Micro got my programming mixed up... or I just failed to write it properly...
Thanks to the original Arduino IDE, I can connect and the Bluetooth connection stays open!

But... I run into another problem:

As I programmed several modes: manual, time and sensor - I tried to separate them by "if else" depending on the "Incoming_value" from my Bluetooth.
As for the manual mode it works perfectly - the lamp turns on and off my the push of the buttons.
For the other modes it activates them once and if the time or sensor value does not meet the right value, it stops. I think it looses the "Incoming_value" and just waits for another input.
How can I update the code so it stays in a loop and doesn't loose the "Incoming_value"? It should only change the value if I press another button -> receive another value over "Serial.read".

Is that possible?

I appreciate all the help!

I'm sure it is possible, but I'm unable to comment on that sort of stuff. If a state is created by an incoming variable, it stays until a new incoming variable changes it.

I got it - I just defined another variable that changes according to the incoming_value. So when the incoming_value gets lost after the loop, the other variable keeps the information.
Thanks again for your help!