Using 2 linear actuator with 4 limit switches

Hello guys, I am new to the arduino community. I am working on a project that uses 2 12V linear actuators and 4 limit switches, but I am not sure what I would need and how to connect a 12V AC supply to the actuators and arduino together.

For the process of the project, I want to press a button and the 1st actuator to retract first, when it hits a limit switch, it will stop at the switch and activate the 2nd actuator to extract. When the 2nd actuator hits another limit switch, it will stop and complete the first process. For the second process, I press another button to retract the 2nd actuator, when it hits a switch, it stops and activate the 1st actuator to extract.

I would like 2 limit switches for each actuator to control the position, but only 1 actuator moving at a time. What I have right now are 2 actuators, power supply, limit switch, and I am not sure if I needed a 4-channel relay. Can someone please tell me what would I need for this project?

You should do a flow chart of the logic for your proposed device. When that is complete, you will be better prepared to begin programming.

build your hardware with motors, relays and limit switches and simulate an Arduino with a few buttons and LEDs and check the inputs, outputs and times that should be processed by the Arduino.

Most probably you need a H-bridge to extract and retract an actuator. Such bridges are powered by 12V DC or more. Please give a link to the actuator data sheet.

Please post a link to the 12 volt power supply.

this is the link to the 12V actuator that I have:

This is the power supply:

The actuator claims to have limit switches built in, but these are not accessible from outside. More information is required.

I am planning to put 2 external limit switches for each actuator, and once the actuator hit an external switch, it will stop.

Let me see if I can make it more clear of how I want the process work:

There are 2 linear actuators,4 limit switches, and 2 buttons.
#1 and #2 limit switch are for A actuator, #3 and #4 switches are for B actuator.

Step1: press first button > A actuator will retract and hit switch #1 and stop > once it hits switch #1 it will activate B actuator to extract and hit switch #3 and stop.

Step2: press second button > B actuator will retract and hit switch #4 and stop > once it hits switch #4 it will activate A actuator to extract and hit switch #2 and stop.

I am also getting confuse on how to write the code when pressing a button once that will run Step 1, and press different button that will run Step 2.

See the sticky topics on top of the forum “several things at the same time” etc.

Also look into state machines that can do all that for you.

Think deeper. All the items you mentioned can be considered steps, or ‘states’, in your process:

  1. All at rest, no switches active, waiting for button press to start actuator A retracting
  2. starting A retracting
  3. waiting for A to hit S2.
  4. stopping A, starting B to extend
  5. waiting for B to hit S3
  6. stopping B
  7. waiting for second button press to reverse all the above

This state machine tutorial illustrates some things which will be useful to you to build your sketch. There are others on the site as well. Don’t forget to look at switch/case on the IDE reference page.

1 Like

so I have never used the state machine before, and I trying to program a very simple process:

  1. motor doesnt spin when button is not press at the beginning
  2. press the button once, the motor start spinning

But I don’t know what is wrong or missing with my code.

int pushbutton = 2;
int extract = 7;
int retract = 8;

void setup()
pinMode(pushbutton, INPUT_PULLUP);
pinMode(extract, OUTPUT);
pinMode(retract, OUTPUT);

void loop()
if(pushbutton == HIGH){
state = 0;
if(pushbutton == LOW){
state = 1;
state = default;

switch (state) {
case 0:
digitalWrite(extract, LOW);
digitalWrite(retract, LOW);

case 1:
digitalWrite(extract, HIGH);
digitalWrite(retract, LOW);

digitalWrite(extract, HIGH);
digitalWrite(retract, HIGH);


You need to use digitalRead to find out the state of your pushbutton.

so do I do “if(digitalRead(pushbutton) == HIGH) {”?

That’s it.

it is giving me error saying that ‘state’ is not declare in this scope

To add code please click this link;

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Make loop() contain only the switch. Every state waits for one or more specific conditions that tell when it’s time to do something and advance to another state.

E.g. in idle state (0) read the pushbutton and, if pressed, start expanding and advance to expanding state (1). Having names for the states makes the code better readable.

Make ALL your limit switches Normally Closed connections, the actuator hitting the limit will then make it go open.
This is a form of fail safe if you have broken or intermittent connections form the controller to limit switch.

Tom… :grinning: :+1: :coffee: :australia: