Homing Actuator via Limit Switch/ Hall Effect Sensors (Details included)

-Arduino Uno
-2 Channel 5V 10A Relay Module
-TiMotion TA2 Series Actuator (24V DC, 2.5A) with 2 internal hall effect sensors and Limit switch cut current for fully extended and retracted.
-Two Pushbuttons

What I’ve done so far:
I’ve used a basic setup and code to be able to extend and retract the actuator when one of two buttons is pressed. Using the internal hall effect sensors of the actuator I’m able to get a value for the position of the actuator.


#include <Encoder.h>
const int relay1 = 6;
const int relay2 = 7;
const int sw1Pin = 12;
const int sw2Pin = 13;

Encoder myEnc(2, 3); 
long oldPosition  = -999;

void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(sw1Pin, INPUT_PULLUP);
  pinMode(sw2Pin, INPUT_PULLUP);

void extendActuator() {
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, LOW);

void retractActuator() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, HIGH);

void stopActuator() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);

void loop() {

  int sw1State = digitalRead(sw1Pin); //Read the status of Switch1
  int sw2State = digitalRead(sw2Pin); //Read the status of Switch2

  if (sw1State == LOW) {        //If switch1 is pressed
  } else {
     if (sw2State == LOW) {     //If switch2 is pressed
     } else {                   //If no switch is pressed.
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;

What I want to do, but don’t know how to do it.
Although there is no issue with the values I’m getting, I’d like to home the actuator to a fully retracted position so I can achieve an absolute value for the position of the Actuator before I go into the main loop. When homed the position would actually start from the base of the actuator ( = 0 actual home).

I’ve done some research and prototyping to do with homing, but I can’t get the actuator to home fully. The closest I have got is retracting the actuator for one revolution of the actuator using attachInterrupt. In my head, I need to be using the limit switches on board and interrupts together to get it to retract/ home fully.

I’m open to any suggestion. Thank you in advance.

There are several versions of that actuator, post the exact, full part number. http://www.linearmotion.it/wp-content/uploads/2014/02/TA2-DataSheet.pdf

If your limit switches just cut the current to the motor, then to home your actuator: 1. turn on the motor in the direction you want to go. 2. every X milliseconds, read your encoder position 3. if it hasn't changed since the last time, the motor got cut which means you are home

If you really want to start each time from exactly the same location, then you must use this procedure to find that location.

  1. test to see if the limit switch is on. If not, step one single step towards that switch and repeat the loop of testing and stepping.
  2. when you have detected the switch is on, then do the following:
  3. test to see if the switch is off. If not off, take one step away from the switch and repeat the test loop.
  4. When you discover the switch is off, then you are at the exact beginning position.

The reason for the above is the initial loop may actually go beyond the location where the switch if located. This is usually the case when micro-switches are used as limit switches. They are never exactly symmetrical for off and on.

Good luck. Paul

That would probably work to find the limit for the actuator. My logic was if you are using the linear motion for something like a CNC machine where you need a zero beginning point to count steps from and to.

What is the project you are using the actuator for?


bajja: The project is very vague. So a user will be able to drive the actuator forward using two switches. Extend/Retract switch and the actuator will stop/lock when no switch is active. (What I've done so far). For further implementation, this one actuator could be paired with another actuator for a synchronous or asynchronous movement. Hence why position control is needed. So to keep the actuators as equal as possible, I thought it would be best to introduce a homing loop so both actuators could both start at a fully retracted position rather than being in random locations. From there the positions would be recorded and then further integrated to act with one another.

Yes, it is wary vague. If at any time, the actuator has to move some heavy object, your code will detect that as a limit switch. Paul