I am using HC-05 Bluetooth module to control two servos. I use Bluetooth terminal in my android phone to control them.

Problem is that myServo1 is moving like I want but I can't control myServo2. Code is reading the variable a but it can't read variable b.

#include <SoftwareSerial.h>
#include <Servo.h>
Servo myServo1;
Servo myServo2;
int servo;
SoftwareSerial BT(2, 3); 
void setup()  
servo = 90;

char a; 
char b;
void loop() 
 if (BT.available())
b = (BT.read());
if (a=='1')

if (a=='2')

  if (b=='3')

  if (b=='4')


Where is the problem?

  • your sketch has no debugging code .... why are you not printing the values of a and b to the serial monitor? – jsotola Mar 29 '20 at 2:55

When using


that's doing the same as

if(BT.available() > 0)

But then you are reading 2 bytes, though you cannot be sure, that there are 2 bytes to read. The data is transmitted serially, so even when you send it on your phone in one bunch, it does not get transmitted as bunch.

Try to use

if(BT.available() >= 2)

Also makes sure, that nothing else gets send over serial besides the numbers (often there is a line ending send implicitly depending on the used app).

  • If I use the " if(BT.available() >= 2) " it will mix the whole system – engineerstudent Mar 28 '20 at 18:37
  • What do you mean by that? @engineerstudent – chrisl Mar 28 '20 at 19:08
  • It does not work properly. If I put "1", "2", "3" or "4" it turns one servo randomly. In some cases it doesn't work at all – engineerstudent Mar 28 '20 at 19:12
  • Then you also have another problem. Have you confirmed, that the comminication with the HC-05 works correctly? You can do this by using a testsketch, where you forward the received data from bluetooth to the serial monitor. If everything is transmitted correctly, we can rule out that part – chrisl Mar 28 '20 at 19:16
  • I don't think that there is problem in the connection because if I use "BT.available()>0" and I type "1" or "2" then it work properly and turns the myServo1 between 180 and -180. – engineerstudent Mar 28 '20 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.