I have just got a new pair of HC-05 Bluetooth modules. I am working on a simple project. I'm trying to move two servo motors using two potentiometers. I followed a simple tutorial to move one servo, and it worked successfully.

How to make the same thing with 2 potentiometers and two servos?


I have successfully managed to write down something that get to read values from two potentiometers, but I'am still facing problems with the slave part. Can't figure out the problem.


int potValuePins[] = { A0, A1 };
void setup() {
  Serial.begin(9600); // Default communication rate of the Bluetooth module
   for (auto pin : potValuePins) {
     pinMode(pin, INPUT);

void loop() {
 for (auto pin : potValuePins) {
   Serial.write(pin); // Send the pin number
   Serial.write(analogRead(pin)); // Send the pin value with a trailing 
 newline as a delimiter
   int value=analogRead(pin);
   Serial.print(" Pin is: ");
   Serial.print("Value is: ");


#include <Servo.h>
Servo myServo1;
Servo myServo2;

void setup() {

void loop() {
if (Serial.available()) {
 char input[3];
 Serial.readBytes(input, 2);
 int pin = input[0];
 int angle = input[1];
 Serial.print("the pin:");
 Serial.print("the angle:");
 Serial.println( angle);

 switch (pin) {
  case A0:
  case A1:
    Serial.println("Unknown value");

Looking at the example you linked to, the "communication" between the two ends is just writing a (byte) value between 0 and 255, and reading that on the other end.

One simple (out of many possibilities) to extend this to handle a set of two is just to send 2 bytes in sequence, with the first byte being an identifier of which servo you are denoting.

Where the example master code shows:

// Reading the potentiometer
potValue = analogRead(A0);
int potValueMapped = map(potValue, 0, 1023, 0, 255);
Serial.write(potValueMapped); // Sends potValue to servo motor

You instead write:

// Reading the potentiometer 1
pot1Value = analogRead(A1);
int pot1ValueMapped = map(pot1Value, 0, 1023, 0, 255);
Serial.write(pot1ValueMapped); // Sends pot1Value to servo motor

// Reading the potentiometer 2
pot2Value = analogRead(A0);
int pot2ValueMapped = map(pot2Value, 0, 1023, 0, 255);
Serial.write(pot2ValueMapped); // Sends pot2Value to servo motor

And on the slave side, where the example has:

if (Serial.available() > 0) { // Checks whether data is comming from the serial port
  state = Serial.read(); // Reads the data from the serial port
// Controlling the servo motor

You have to separate out the two possible choices:

if (Serial.available() > 0) {
  servo_num = Serial.read();
if (Serial.available() > 0) {
  state = Serial.read();

switch (servo_num) {
  case 1:
  case 2:

Note that you'll also have to declare the servo_num variable, and instantiate two of the Servo objects. The code above is just a guide.

The simple expansion above is not robust, as it's possible that the 2nd byte is not received by the time the 2nd Serial.read() is called, and then that byte would be caught next time around the loop() by the first Serial.read() call and you'd be out of sync. But hopefully, this shows the general concept of how to start developing a protocol that is more complex than just single values.

Edit: What's probably happening is the system is getting out of sync. As I noted, it's not a robust protocol, but I think you may now understand the idea.

You can re-map the servo values to go from 2-255, rather than 0-255, and set the servo_num to either 0 or 1. In the slave, if you get a number <2, then it's a servo selector, otherwise it's a servo value. but then you lose the ability to set the servo to 0 or 1.

Also, you may want to space out the master sending the values, as you are likely sending data faster than the servo could respond. You could put a delay(100) or something in the loop() function, or change your code to use a millis()-based state machine (see the "BlinkWithoutDelay" Arduino example).

  • I have set the " pot2Value = analogRead(A0); " to A1, the pin in which I have inserted my second potentiometer. I have done what you told me too, the servos are not moving the way they are suppose to. both of them are moving at the same time and not following the potentiometers. moving constantly.
    – Ash Bougui
    Dec 1 '17 at 20:53
  • I left the rest of the coding as an exercise to the reader :-) How did you instantiate both Servo objects? Dec 1 '17 at 21:03
  • Servo myServo1; Servo myServo2; and I attached each one of them to the pin I'am using .
    – Ash Bougui
    Dec 1 '17 at 21:05
  • Edit/append the updated code you wrote to the question, or join the Pin 13 chat room and we can discuss it Dec 1 '17 at 21:09
  • I need to have 20 reputation in order to join the chat room, which I don t have them "yet" so I updated the post.
    – Ash Bougui
    Dec 1 '17 at 21:27

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.