I am using an android phone to connect to a HC-05 and then to send letters from an android phone. I am able to pair the two, but the HC-05 stops receiving data every 4 seconds. The two do not disconnect, just simply no data gets through. For example, if I send an "F" character to start the car, the car will start perfectly, but after 4 seconds it becomes unresponsive. (The car either keeps going since it does not receive a stop ("D") or if it has already stopped I am unable to start it again in that 4 second time frame). The cuts in responsiveness seem to coincide with the HC-05's onboard LED flashing at 2hz twice (It is responsive for two LED ON-OFF cycles but then it becomes unresponsive for two ON-OFF cycles). The HC-05 is running on 5v with a 135k ohm and 270k ohm resistor stepping down the voltage for the logic signal to 3.3v.
These are what I have tried:
Using Serial and Software Serial
Checking connections
Using only a small motor
Recharging the batteries which provide a total of 6.4v
Any help is greatly appreciated as I really need this for a school project due this week.
Thank you so much.
The code used is below (I am unable to find the original creator as many websites share this code snippet):
#include <AFMotor.h>
#include <SoftwareSerial.h>
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
const int bluetoothTx = 2; // Connect HC-05 TX to Arduino pin 2 (RX)
const int bluetoothRx = 3; // Connect HC-05 RX to Arduino pin 3 (TX)
SoftwareSerial bluetoothSerial(bluetoothTx, bluetoothRx); // Create a SoftwareSerial instance
char command;
void setup()
{
bluetoothSerial.begin(9600); // Start SoftwareSerial for Bluetooth communication
}
void loop(){
if(bluetoothSerial.available() > 0){ // Check if there is data from HC-05
command = bluetoothSerial.read(); // Read the command from HC-05
Stop();
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
delay(10); // Add a small delay after processing each command
}
}
void forward()
{
motor1.setSpeed(255);
motor1.run(FORWARD);
motor2.setSpeed(255);
motor2.run(FORWARD);
motor3.setSpeed(255);
motor3.run(FORWARD);
motor4.setSpeed(255);
motor4.run(FORWARD);
}
void back()
{
motor1.setSpeed(255);
motor1.run(BACKWARD);
motor2.setSpeed(255);
motor2.run(BACKWARD);
motor3.setSpeed(255);
motor3.run(BACKWARD);
motor4.setSpeed(255);
motor4.run(BACKWARD);
}
void left()
{
motor1.setSpeed(255);
motor1.run(BACKWARD);
motor2.setSpeed(255);
motor2.run(BACKWARD);
motor3.setSpeed(255);
motor3.run(FORWARD);
motor4.setSpeed(255);
motor4.run(FORWARD);
}
void right()
{
motor1.setSpeed(255);
motor1.run(FORWARD);
motor2.setSpeed(255);
motor2.run(FORWARD);
motor3.setSpeed(255);
motor3.run(BACKWARD);
motor4.setSpeed(255);
motor4.run(BACKWARD);
}
void Stop()
{
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
motor3.setSpeed(0);
motor3.run(RELEASE);
motor4.setSpeed(0);
motor4.run(RELEASE);
}
Those resistor values seem on the high side. I use 1K and 2K, but have no basis to prove that is better though I have no trouble with my modules communicating.
There seem to be bogus H05 modules out there. From where did you purchase your modules? There are version 4-xxxxx... modules out there that seem to be bogus. Do you know the version number (in AT mode query "AT+VERSION?") How to set HC05 to AT mode.
No proof, but I'm with Groundfungus. While you are indeed presenting the right voltage, the current resistance is 135 times that provided by the recommended combination, which is surely asking for trouble.
Do it properly, and then come back.
Getting some result, as you do, rather suggests your code is kosher, and the same applies to HC-05.
Your stopping 4 motors every time you get a command. You could be having problems with emi.
Just work one motor and dont stop on every command. just stop when you have too. Also slow down before stopping will help.
I am. I bought 1k and 2k resistors and I am currently reconfiguring my code. I am asking just in case if all else fails. I really appreciate everyone's help.
1. Connect your BT with UNO as per following diagram (Fig-1).
Figure-1:
2. Upload the following sketch in UNO.
#include <SoftwareSerial.h>
SoftwareSerial SUART(2, 3); // SRX = 2, STX = 3
#define ledpin 13 //built-in L of UNO
void setup()
{
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, LOW); //L is OFF
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
if (SUART.available())
{
char x = SUART.read();
if (x == 'Y')
{
digitalWrite(ledpin, HIGH); //L is ON
}
if (x == 'N')
{
digitalWrite(ledpin, LOW); //L is OFF
}
}
if(Serial.available())
{
char c = Serial.read();
SUART.print(c);
}
}
3. Open BT Terminal in your Phone. 4. Pair your Phone with BT. 5. Open Serial Monitor at Bd = 9600. 6. Send Y from BT of Phone in ASCII Mode. 7. Check that L (onboard LED) is ON. 8. Send N from BT of Phone. 9. Check that L is OFF. 10. Open Serial Monitor. 11. Enter Hello in the InputBox of Serial Monitor and then click on the Send Button. 12. Check that Hello has appeared on the BT Terminal of your Phone. 13. This is the end of functional check of BT, UNO, and Phone.