Using linear actuator relative encoder with interrupts to map position

Hey guys.

I`m having some problems while using a linear actuator if you could help me out I would very much appreciate it.

Hardware setup: Arduino Uno + IBT2 H-Bridge + Linear actuator with relative position encoder. Linear actuator is controlled with 2 PWM signals.

I am trying to map the actuator’s position so I can accurately know and control, at any point, its position. For this I am using an interrupt attached to Digital pin 3 where the feedback from the servo is connected. So every rising edge of the position signal I am increasing my counter and want to serial print this counter.

My first problem is that instead of getting an increased counter every time I get a signal I get a reading every now and then (the position frequency seems to be around 20-25 kHz) and I don`t get a proper value. My logic is that i fully extend the actuator - count - wait - reset count - retract actuator - wait.
Please point out my flaws in logic. Thank you.

int x = 0;
int pwm1 = 5; //out
int pwm2 = 6; //in
int pwmin = 0, pwmout = 0;
volatile int pulse_count;
int act_pos = 3;

void setup()
{
  Serial.begin(57600);
  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  pinMode(act_pos, INPUT);
  attachInterrupt(digitalPinToInterrupt(act_pos), ircounter, RISING);
}

void ircounter()
{
  pulse_count++;
}

void loop()
{
  if(x == 0)
  {
    act_init();
    x++;
  }
  act_in();
  Serial.print("Count: ");
  Serial.println(pulse_count);

  act_off();
  pulse_count = 0;
  
  act_out();
  Serial.print("Count: ");
  Serial.println(pulse_count);
  pulse_count = 0;
}

A pic with my serial monitor is attached.

Where is the rest of the code?

Please post a link to the product page or data sheet for the actuator/encoder.

Hey jremington. Thanks for the reply.

Actuator Datasheet:

I have the 12V version, with reed feedback, 3 wires.

Rest of code:

void act_in()
{
  analogWrite(pwm1, 0);
  analogWrite(pwm2, 255);
  delay(10000);
}

void act_out()
{
  analogWrite(pwm1, 255);
  analogWrite(pwm2, 0);
  delay(10000);
}

void act_off()
{
  analogWrite(pwm1, 0);
  analogWrite(pwm2, 0);
}

void act_init()
{
  analogWrite(pwm2, 255);
  analogWrite(pwm2, 0);
  delay(10000);
}

Quick note - the reason for the 10000 delay is that it takes around 6-8 seconds for full actuator extension / retraction. The extra time is because sometimes after half extension the actuator considerably slows down for unknown reasons. So I full extend / retract, count and reset each time after. Since my interrupt is outside the motor control function I would expect to get continous readings on the serial monitor not a reading every 7-8 seconds.

The computer can't do anything while it is in delay() (except to process an interrupt). So, you do not expect it to print continuously. Also, you can't print from an interrupt.

Do you have the four or three wire version? How is the reed feedback connection wired?

You may have a switch bounce problem.

I see your point. However - my actuator works to go in or out around 6-7 seconds and during this whole time I am receiving pulses which SHOULD be printed on the serial monitor. It`s not a problem of what happens during my delay but all the time outside it.

I have the 3 wire version - GND, VCC and REED feedback wire. The reed feedback wire goes into a terminal with 3 connectors - 1 wire from this goes to my scope and 1 goes to the arduino directly. The voltage signal levels is between 0 - 11V and I know that Arduino can handle up to 12V.

so reed wire - split into - scope + arduino digital pin 3.

The voltage signal levels is between 0 - 11V and I know that Arduino can handle up to 12V.

No, the input pins will be destroyed if > 5V is applied. I'm surprised it is still working. Perhaps this is because you did not connect the grounds.

I don't understand from the data sheet how the wiring of the three-pin actuator works (page 16), as the power pins can have either polarity. This configuration is not a good choice for Arduino. The four wire connection would be much better.

during this whole time I am receiving pulses which SHOULD be printed on the serial monitorYour input interrupt just counts pulses. Printing happens elsewhere and cannot work in an interrupt, or during delay().

Well the Arduino is still working, surprisingly according to you. I will put a resistor to lower the voltage to 5V. And all my setup has the same ground - arduino ground + actuator ground + power supply ground.

You said something earlier that it might be a contact bounce issue but I dont get this. The signals from the actuator arent a magnet passing a sensor? How can it be a contact bounce issue?

Use a voltage divider, not a resistor, and make certain that negative voltages are not applied to the input pin.

A reed switch is a switch, and bounces.

Reed switches are very bouncy in fact, but very rapid - even 2ms of debounce time may be enough.

Avoid calling delay, there's no reason to call delay. Just check the progress using the count.