Servo control with one button

Hi everyone, I'm looking for someone to put together a project for me. I want to move 10 servos simultaneously with a single push button (maximum 180°). The button should indicate the servo endpoints: -90° in green and +90° in red. This will be used as a drawer lock in a camper van. Of course, I'll pay for it ;-) Thanks in advance! Best regards, Joerg

What's the budget?

Do you already have the hardware for this project ?

I assume that you will be doing the installation of the hardware. How big is the campervan ? This is important because the cable runs could potentially be quite long

Moving them sequentially, at a very small interval, will lessen the current capacity of the power supply. Reconsider "simultaneously."

[edit] just going to sneak this little code dump in here

onebuttontenservos.ino
#include <Servo.h>
const char servos = 10;  // number of servos
Servo servo[servos];     // create servo instances

unsigned char buttonpin = 12, ledpin = 14;  // pins
bool lockstate = 0, actuate = 0;            // lock state and flag to actuate locks
int lockangle = 180, openangle = 0;         // angle value for locked/opened
int timebetweenservos = 250;                // time given to 180 degree servo move

bool currentButtonState, lastButtonRead;  // button reading
unsigned long buttonTimer, buttonTimeout = 50;

void setup() {
  pinMode(buttonpin, INPUT_PULLUP);
  pinMode(ledpin + 0, OUTPUT);           // red
  pinMode(ledpin + 1, OUTPUT);           // grn

  digitalWrite(ledpin + 0, lockstate);   // red
  digitalWrite(ledpin + 1, !lockstate);  // grn

  for (byte i = 0; i < servos; i++) {
    servo[i].attach(i + 2);     // servo[0] = pin 2, servo[9] = pin 11
    servo[i].write(openangle);  // servo home
    delay(timebetweenservos);   // pause between servo move
  }
}

void loop() {
  readButton();
  if (actuate)  // if button was pressed
    actuateservos();
}

void actuateservos() {     // if locked then open.  if opened then lock.
  actuate = 0;             // reset flag
  lockstate = !lockstate;  // change state
  for (byte i = 0; i < servos; i++) {
    servo[i].write(lockstate * lockangle);  // state 0 = 0 degreesm state 1 = 180 degrees
    delay(timebetweenservos);               // pause between servo move
  }
  digitalWrite(ledpin + 0, lockstate);
  digitalWrite(ledpin + 1, !lockstate);
}

void readButton() {
  bool currentButtonRead = digitalRead(buttonpin);  // read button pin

  if (currentButtonRead != lastButtonRead) {  // if button pin changes...
    buttonTimer = millis();                   // ...start a timer
    lastButtonRead = currentButtonRead;       // ... and store current state
  }

  if ((millis() - buttonTimer) > buttonTimeout) {               // if button change was longer than debounce timeout
    if (lastButtonRead == LOW && currentButtonState == HIGH) {  // ...button WAS PRESSED but state IS RELEASED
      //==================================================
      actuate = 1;  // set flag to actuate locks open or closed
      //==================================================
    }
    currentButtonState = currentButtonRead;  // update button state
  }
}
diagram.json for wokwi.com simulator
{
  "version": 1,
  "author": "xfpd",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 81.6, "left": -0.5, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo1", "top": -261.2, "left": 201.6, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 142.8,
      "left": -65.8,
      "rotate": 270,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 176.75,
      "left": -19.2,
      "attrs": { "value": "330" }
    },
    { "type": "wokwi-servo", "id": "servo2", "top": -213.2, "left": 201.6, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo3", "top": -165.2, "left": 201.6, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo4", "top": -117.2, "left": 201.6, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo5", "top": -69.2, "left": 201.6, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": 54.2,
      "left": 201.6,
      "attrs": { "color": "green", "xray": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 162,
      "left": -65.8,
      "rotate": 270,
      "attrs": { "color": "limegreen", "flip": "1" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -248.84, "left": 9.6, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": 153.6, "left": 172.2, "attrs": {} },
    {
      "type": "wokwi-servo",
      "id": "servo6",
      "top": -50.8,
      "left": -170,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-servo",
      "id": "servo7",
      "top": -99.6,
      "left": -167.2,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-servo",
      "id": "servo8",
      "top": -146.8,
      "left": -170,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-servo",
      "id": "servo9",
      "top": -194.8,
      "left": -170,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-servo",
      "id": "servo10",
      "top": -242.8,
      "left": -170,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 157.55,
      "left": -19.2,
      "attrs": { "value": "330" }
    }
  ],
  "connections": [
    [ "nano:2", "servo5:PWM", "green", [ "v0" ] ],
    [ "nano:3", "servo4:PWM", "green", [ "v0" ] ],
    [ "nano:4", "servo3:PWM", "green", [ "v0" ] ],
    [ "nano:5", "servo2:PWM", "green", [ "v0" ] ],
    [ "nano:6", "servo1:PWM", "green", [ "v0" ] ],
    [ "vcc1:VCC", "servo1:V+", "red", [ "v0" ] ],
    [ "nano:7", "servo10:PWM", "green", [ "v0" ] ],
    [ "nano:8", "servo9:PWM", "green", [ "v0" ] ],
    [ "nano:9", "servo8:PWM", "green", [ "v0" ] ],
    [ "nano:10", "servo7:PWM", "green", [ "v0" ] ],
    [ "nano:11", "servo6:PWM", "green", [ "v0" ] ],
    [ "led1:C", "r2:1", "green", [ "h0" ] ],
    [ "led2:C", "r1:1", "green", [ "h0" ] ],
    [ "led1:A", "nano:A0", "green", [ "h57.6" ] ],
    [ "led2:A", "nano:A1", "green", [ "h67.2" ] ],
    [ "nano:GND.1", "r2:2", "black", [ "v0" ] ],
    [ "nano:GND.1", "r1:2", "black", [ "v0" ] ],
    [ "btn1:1.l", "nano:12", "gold", [ "h0" ] ],
    [ "gnd1:GND", "btn1:2.l", "black", [ "v0" ] ],
    [ "gnd1:GND", "nano:GND.3", "black", [ "v0" ] ],
    [ "gnd1:GND", "servo5:GND", "black", [ "v0" ] ],
    [ "servo5:GND", "servo4:GND", "black", [ "h-19.2", "v-48" ] ],
    [ "servo4:GND", "servo3:GND", "black", [ "h-19.2", "v-48" ] ],
    [ "servo3:GND", "servo2:GND", "black", [ "h-19.2", "v-48" ] ],
    [ "servo2:GND", "servo1:GND", "black", [ "h-19.2", "v-48" ] ],
    [ "servo1:V+", "servo2:V+", "red", [ "h-9.6", "v19.3" ] ],
    [ "servo2:V+", "servo3:V+", "red", [ "h-9.6", "v48.1" ] ],
    [ "servo4:V+", "servo3:V+", "red", [ "h-9.6", "v-47.9" ] ],
    [ "servo4:V+", "servo5:V+", "red", [ "h-9.6", "v48.1" ] ],
    [ "vcc1:VCC", "servo10:V+", "red", [ "v0" ] ],
    [ "servo10:V+", "servo9:V+", "red", [ "h19.2", "v48" ] ],
    [ "servo9:V+", "servo8:V+", "red", [ "h19.2", "v48" ] ],
    [ "servo8:V+", "servo7:V+", "red", [ "h19.2", "v47.2" ] ],
    [ "servo7:V+", "servo6:V+", "red", [ "h16.4", "v48.8" ] ],
    [ "gnd1:GND", "servo6:GND", "black", [ "v0" ] ],
    [ "servo6:GND", "servo7:GND", "black", [ "h9.6", "v-48" ] ],
    [ "servo7:GND", "servo8:GND", "black", [ "h6.8", "v-47.2" ] ],
    [ "servo8:GND", "servo9:GND", "black", [ "h9.6", "v-48" ] ],
    [ "servo9:GND", "servo10:GND", "black", [ "h9.6", "v-48" ] ]
  ],
  "dependencies": {}
}

What will be the servo positions if power is ever removed? What will be the servo positions when power is first applied?

Former 42ft 8 ton RV owner here. Most will not understand the phrase 'rolling earthquake'.
A push button can easily be accidently pushed. I suggest you use a hooded ON/OFF/ON switch.

I realize the pictured switch is only ON/OFF, but the project requires ON/OFF/ON which is -90 / OFF / +90.

What about an actual key for safety concerns? Or even a key fob, tap once to unlock and tap again to lock. Perhaps add an indicator led too.

I would add a test for if the drawer is fully closed before latching.

I stupidly assumed an LED was included. I don't like the Key Fob idea, if it fails to work for some reason, it could present issues. A key isn't a terrible idea, but if it were my RV, I would mount the main power hooded switch inside the main control panel (if it has a door) or failing that as far from the push button as possible.
NOTE to non-RVers. I am assuming the OP's rig is a slide-out, so there will likely be only a small area of the RV reachable when buttoned up and any and all components need to be handy. Another option is a master switch in the basement.
The OP needs to tell us the parameters.

Most likely a simple mechanical locker solves the problem more reliable than any arduino+servo combo could do for a reaonable price :)

Most likely, this isn't about security per se; the OP is probably trying to prevent the drawers from opening every time he hits a pothole in the road. Better confirm with the OP as to the actual need.
To me, so-called child locks, simple little plastic tabs that engage with a plastic ear on the drawer, are just as effective; just don't expect them to actually slow down a child much, as they're purely for frustrating adults.

Or use that switch and a pushbutton. I have a garden tractor key replacement partially wired. Flip up the hood to energize/lift magneto ground, push button to start.

Sorry if I wasn't clear, the switch is only meant to energize the button. Re-reading the OP, it sounds like a simple ON/OFF switch is plenty. We used to call them SPST, is that still correct? It should be obvious, but that switched wire is the hot wire. In many recreational vehicles, the frame is often common ground but I advise against it for control circuits at least.

Hi @joergx!

Controlling ten servos is easily achievable. Here is an example where two LEDs (red and green) indicate the "closed" and "open" states, and the corresponding LED flashes during the transition (red while closing, green while opening).

/*
  Forum: https://forum.arduino.cc/t/servo-control-with-one-button/1447740
  Wokwi: https://wokwi.com/projects/466365025604980737

  2026/06/09
  ec2021

*/

#include <Servo.h>


constexpr byte pwmPins[] = { 5, 6, 9, 10, 11 };
constexpr int noOfPins = sizeof(pwmPins) / sizeof(pwmPins[0]);
Servo servos[noOfPins];

constexpr byte btPin{ 2 };
constexpr byte redPin{ 4 };
constexpr byte greenPin{ 3 };
constexpr unsigned long duration{ 3000 };
constexpr byte delta{ 10 };
constexpr byte openPosition{ 180 };
constexpr byte closePosition{ 0 };

enum class LOCK { IDLE,
                  CLOSING,
                  OPENING,
                  PREPARE
                };

LOCK state = LOCK::IDLE;
boolean locked = true;
byte lockPosition = 0;
unsigned long lastStep = 0;

void setup() {
  Serial.begin(115200);
  pinMode(btPin, INPUT_PULLUP);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  for (int i = 0; i < noOfPins; i++) {
    servos[i].attach(pwmPins[i]);
  }
}

void loop() {
  drawerLock();
}

void drawerLock() {
  switch (state) {
    case LOCK::IDLE:
      if (digitalRead(btPin) == LOW) {
        state = locked ? LOCK::OPENING : LOCK::CLOSING;
        delay(100);  //Very simple debouncing
        digitalWrite(redPin, LOW);
        digitalWrite(greenPin, LOW);
      }
      break;
    case LOCK::CLOSING:
      doBlink(redPin);
      if (millis() - lastStep > 100) {
        lastStep = millis();
        if (lockPosition < delta) {
          lockPosition = closePosition;
        } else {
          lockPosition -= delta;
        }
        setServoTo(lockPosition);
      }
      if (lockPosition == closePosition) {
        locked = true;
        state = LOCK::PREPARE;
      }
      break;
    case LOCK::OPENING:
      doBlink(greenPin);
      if (millis() - lastStep > 100) {
        lastStep = millis();
        if (lockPosition + delta >= openPosition) {
          lockPosition = openPosition;
        } else {
          lockPosition += delta;
        }
        setServoTo(lockPosition);
      }
      if (lockPosition == openPosition) {
        locked = false;
        state = LOCK::PREPARE;
      }
      break;
    case LOCK::PREPARE:
      if (locked) {
        setLedsOnOff(redPin, greenPin);
      } else {
        setLedsOnOff(greenPin, redPin);
      }
      state = LOCK::IDLE;
      break;
  }
}


void setServoTo(byte value) {
  for (int i = 0; i < noOfPins; i++) {
    servos[i].write(value);
  }
}

void doBlink(byte pin) {
  static unsigned long lastBlink = 0;
  if (millis() - lastBlink > 500) {
    lastBlink = millis();
    byte newState = !digitalRead(pin);
    digitalWrite(pin, newState);
  }
}

void setLedsOnOff(byte a, byte b) {
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
}

siehe https://wokwi.com/projects/466365025604980737

Additionally, it would be worth checking whether the drawers are fully pushed in so that the closing mechanism operates correctly without any mechanical issues.

The sketch controls two servos per pin to limit the current drawn on each pin. This probably isn't even necessary, given that a pin can supply up to 20 mA and servos typically draw very little current (less than 1 mA).

Whether implementing this electrically—without an emergency release—and using a single button is actually a good solution is another matter entirely...

Regards,
ec2021

BTW: In any case make sure to supply the servos directly with sufficient energy, not from the Arduino board!

If OP doesn't mind them all opening or closing at the same time, driving all the servos from one pin might simplify wiring. Just need to address the current demand.

I couldn't find reliable information about the use of several servos with one single pin.

it's surely possible to connect more than two servos to one pin but I would not exclude that more than four of them might have an influence on the signal.

It depends on the electrical input behavior of the specific servo type (high or low impedance, capacitance).

The cost of using several pins is low, just some extra wiring. The change in software is simple, only change the pins in the pwmPins array.

For sure an appropriate power supply will be crucial to avoid problems.