0

I wanted to control my servo with a Bluetooth module.

I gave certain values for rotating it. When I press those values the servo rotates, but then retains its original position and the PC also makes the sound of the Arduino board disconnecting because I powered the board with my PC (tried with powerbank also). Even my Bluetooth disconnects.

This is my code:

#include <Servo.h>

Servo myservo;
int pos = 0;
char data = 0; 
           
void setup() {
  myservo.attach(9);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) { 
    data = Serial.read();             
    if (data == 'F') {        
      // for (pos = 0; pos <= 180; pos += 1) 
      myservo.write(180);
      delay(1);
    }
  }
  else if (data == 'B') {     
    // for (pos = 180; pos >= 0; pos -= 1) 
    myservo.write(0); 
    delay(1);
  }
}

Connections:

Servo:

red wire -- 5 V (not working at 3.3 V)
orange wire -- pin9
brown wire -- gnd

Bluetooth (hc05):

5 V -- 5 V
gnd -- gnd
rx -- tx
tx -- rx
6
  • I fixed your indentation. You are missing 2 closing curly brackets at the end. Also your for loop for the character F is not doing anything except waiting (you are not writing to the servo there). If your USB connection fails when the servo moves that can indicate a power issue. What type of servo do you have exactly? Have you tried powering the servo seperately from a more capable power source?
    – chrisl
    Jun 24 at 10:13
  • There's no servo positioning line for when 'F' is pressed (myservo.write), I'm guessing that's an accidental omission in the code you posted. Your description of the symptoms indicates the arduino board is resetting, presumably a power issue. A full circuit diagram would help diagnose your issue.
    – Bra1n
    Jun 24 at 10:14
  • @chrisl i powerd it with a charger but still dosent work. Jun 24 at 10:28
  • How exactly did you power it with a charger? Through the USB port of the Arduino? Depending on the power draw of the servo you might overload the diode in there. Please try powering the servo directly from the charger (not through the Arduino). And tell us what kind of servo you have
    – chrisl
    Jun 24 at 10:44
  • 1
    your code should never contain }}, unless you are an expert programmer ... always position } at the correct indentation level for each block that they delimit ... prepare yourself for a world of hurt if you do not learn to format your code properly
    – jsotola
    Jun 24 at 17:42

1 Answer 1

0

I tried changing the USB B cable with a better quality one and it worked.

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.