Servo controlled via HC-05 doesn't move

I'm trying to send data over bluetooth to a HC-05 so that a servo can rotate, but it refuses to move.

I uploaded this sketch:
#include<SoftwareSerial.h>
#include<Servo.h>
Servo x;
int bttx=10; //tx of bluetooth module is connected to pin 9 of arduino
int btrx=11; //rx of bluetooth module is connected to pin 10 of arduino
SoftwareSerial bluetooth(btrx,bttx);
void setup()
{
x.attach(7); // servo is connected to pin 11 of arduino
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
if(bluetooth.available()>0) //if bluetooth module is transmitting data
{
int pos=bluetooth.read(); // store the data in pos variable
Serial.println(pos);
x.write(pos); //move servo head to the given position
}
}

In addition, I wired a separate power supply to see if that was the problem, but it wasn't. I also tried adding a 1k resistor from Rx into pin 11 on my Arduino, but nothing seems to work. Here is the feedback I receive when sending signals and it makes no sense to me:

A couple of changes to your code and it works for me. Change the bluetooth.read to bluetooth.parseInt so you receive the number properly and set the sender to not send any line ending. With read the number would come in 1, 2, or 3 bytes with line endings added. You were only reading 1 byte at a time.

Also your assignment of Software serial pins is confusing and the comment did not match the code.

Code posted properly in code tags and formatted with the IDE autoformat tool.

#include<SoftwareSerial.h>
#include<Servo.h>

Servo x;
const byte servoPin = 7;

int bttx = 10; //TX of bluetooth module is connected to pin 11 of arduino
int btrx = 11; //RX of bluetooth module is connected to pin 10 of arduino

SoftwareSerial bluetooth(btrx, bttx);  // (11,10)

void setup()
{
   x.attach(servoPin); // servo is connected to pin 7 of arduino  *** fixed comment
   Serial.begin(9600);
   bluetooth.begin(9600);
}

void loop()
{
   if (bluetooth.available() > 0) //if bluetooth module is transmitting data
   {
      int pos = bluetooth.parseInt(); // store the data in pos variable ******* change to parseInt
      Serial.println(pos);
      x.write(pos); //move servo head to the given position
   }
}

That seemed to do the trick, now it's actually responding to my commands. Once I get my servo to not return back home after moving, this project will come to a close. Thanks!

That is why you need to disable sending line endings like line feed or carriage return. Like the serial Bluetooth terminal app that I use has a Settings tab. In that tab and in the Send tab set Newline to None.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.