Introduction: Hall Effect Sensors 2: Synchronizing Motion

About: Progressive Automations is your primary source for electric linear actuators, motion control systems and automation accessories. For over a decade, we have supplied various industries with top quality automati…

In this Instructable we will be expanding on what we learned last week in the Hall effect introduction Instructable. We will use the Hall effects on two actuators to synchronize the motion of the actuators. We will track both counts, and if the counts get too far apart, one actuator will slow down to equalize the positions. This is Part 2 of a 3 part series. Last week we covered the basics of interrupts and hall effect sensors and next week will cover more advanced PID control.

Part 1:https://www.instructables.com/id/Hall-Effect-Sensors-1-Position-Control/

Part 3:https://www.instructables.com/id/Hall-Effect-Sensors-3-PID-Control/

For this Instructable, you will need:

-Arduino Due (The Due is needed for extra interrupt pins)

-2 or more Actuators with Hall Effect Sensors

- MegaMoto motor control shield

- Relay Board (4 or 8)

Let's get started!

Step 1: Hardware Setup

The first thing that's different from Part 1 is that we are using an Arduino Due. The Due has two processors on it, with one that is dedicated to interrupts. This allows us to attach interrupts to any pin as long as we initialize it correctly in the code. The Due is also needed if you are synchronizing more than two motors. The encoder counts are too fast, so if you have more than two motors supplying pulses, you will need the Due's faster processor to not miss counts.

The second difference is that we're using a bigger power supply. Check the power consumption of all the motors you are controlling and ensure that the PSU can provide enough current. There must also be a much larger start-up current to accommodate multiple motors.

We need to wire the Hall effect sensors the same way we did in Part 1 - we're only going to use one Hall effect signal per actuator.

Wire them as follows:

- Red wire to 5V

- Black wire to GND

- Yellow wire to Arduino interrupt pin

- Actuator red wire to MOTA of the MegaMoto

- Actuator black wire to MOTB of the MegaMoto

Check the beginning of the code in the next step, and ensure that the jumpers on the MegaMoto are set to the correct pins and that the hall effect sensors are set to the correct pins. Ensure that "hall0" and "hall1" correspond to the correct MegaMotos (PWMA0 and PWMA1, respectively).

Once the motors are wired correctly to the boards, wire as follows:

- Connect 12V to BAT+

- Connect GND to BAT-

- Connect 12V to Vin on the Due

- Wire two buttons between pins 7 and 8, connecting them to GND

Step 2: Programming 2 Actuators

Now that everything is wired, we can upload the code. The code will read the switches and extend or retract the actuators. While the actuators are moving they will maintain equal position. If you put a load on one, the other will slow down to match it. See the snippet of code below, ensure that the jumpers on the MegaMoto are set to the correct pins and that the hall effect sensors are attached to the correct pins.

#define amp0 A5
#define PWMA0 6
#define PWMB0 5
#define enable0 13   //pins for first MegaMoto

#define amp1 A4
#define PWMA1 9
#define PWMB1 10
#define enable1 12   //pins for second MegaMoto

#define hall0 52
#define hall1 53   //interrupt pins for hall effect sensors

#define switch0 4   //Up button
#define switch1 7   //Down button

Attached is the full code, continue to the next steps to see more details of this code.

Step 3: Moving the Actuators

There are two functions that move the actuator. One is for going forwards, the other for reverse. They are essentially the same, they just differ by which variables are changed and which speeds are adjusted. See the first function below. There are a few Serial.prints() included that can have the comments removed for debugging purposes.

void goForwards()
{
  if (prev[0] == 1 && sw[0] == 0) firstRun = true;
  else firstRun = false;//when the switch changes, use firstRun delay
  forwards = true;
  backwards = false;//set direction
  fullyRetracted = false;//once moved forwards, no longer retracted
  //Serial.println("Moving forwards");
  spd[0] = FWDspd;
  spd[1] = 0;
  spd[2] = FWDspd;
  spd[3] = 0; // set base speeds
  FWDsync();//update speeds based on hall effect counts
  getFeedback();//check current draw

  digitalWrite(enable0, HIGH);
  digitalWrite(enable1, HIGH);//enable both boards
  //Serial.print("Dont Extend"), Serial.println(dontExtend);
  if (dontExtend == true)
  {
    digitalWrite(enable0, LOW);
    digitalWrite(enable1, LOW);//disable both boards if not allowed to extend
  }
  delay(10);//slight delay

  //Serial.print(" Speeds "), Serial.print(spd[0]), Serial.print(", "), Serial.print(spd[1]);
  //Serial.print(", "),       Serial.print(spd[2]), Serial.print(", "), Serial.println(spd[3]);

  analogWrite(PWMA0, spd[0]);
  analogWrite(PWMB0, spd[1]);
  analogWrite(PWMA1, spd[2]);
  analogWrite(PWMB1, spd[3]);//set speeds

  if (firstRun == true) delay(timedelay[0]);
  else delay(timedelay[1]); //small delay to get to speed for current readings

  dontRetract = false;//once you have moved forwards, you can move back again.
  firstRun = false;
}//end goForwards

The synchronizing magic happens in the FWDsync() subroutine. We'll cover that in the next step. Attached is the full code.

Step 4: Synchronizing Counts

This is the subroutine that synchronizes the counts. It takes the difference between the counts, and depending on how big it is, it will slow down the other actuator by a certain amount to compensate. See the comments in the code for better explanations of each line. There are some Serial.prints() included that can have the comments removed for debugging purposes.

void FWDsync() {
  int diff = count[0] - count[1];//calculate difference
  diff = abs(diff);//remove negative
  //Serial.print(" Diff - "), Serial.print(diff);
  if (diff >= 10)
  {
    if (count[0] > count [1]) spd[0] = FWDspd - 10; //if actuator 0 is faster, slow it down
    else spd[2] = FWDspd - 10; //else, slow actuator 1
    if (diff >= 20)
    {
      if (count[0] > count [1]) spd[0] = FWDspd - 20; //if actuator 0 is faster, slow it down

      else spd[2] = FWDspd - 20; //else, slow actuator 1

      if (diff >= 30)
      {
        if (count[0] > count [1]) spd[0] = FWDspd - 30; //if actuator 0 is faster, slow it down

        else spd[2] = FWDspd - 30; //else, slow actuator 1

        if (diff >= 40)
        {
          if (count[0] > count [1]) spd[0] = 0; //if too out of sync, stop actuator
          else spd[2] = 0; //if too out of sync, stop actuator
        }//end if diff>40
      }//end if diff>30
    }//end if diff>20
  }//end if diff>10

  else spd[0] = spd[2] = maxSpeed;  //if not out of sync, apply full speed
  //Serial.print(" ACT1 counts - "), Serial.print(count[0]);
  //Serial.print(" ACT2 counts - "), Serial.print(count[1]);
}//end FWDSync

Step 5: Programming Multiple Actuators

This second code is a modification of the first. It changes all the speed settings and current readings to use for loops and arrays so that it can monitor multiple actuators. Since we are using multiple actuators, we are going to split the H bridges of the MegaMotos into half bridges, and use relays to apply the direction. See this Instructable (Step 4) for more details of wiring the MegaMoto with relays. You will also need to distribute power to all of the hall effect sensors. Use a small breadboard so you have enough places to connect all the 5V and GND wires.

Once the relays are attached, ensure that the MegaMoto jumpers are set correctly. We are using the current sensors split apart to measure each half of the H bridge. See the code below. Amp0 will monitor the current of the actuator attached to PWMA0. Amp1 will monitor PWMB0, amp2 monitors PWMA1, and amp3 monitors PWMB1. Ensure that the correct hall effect sensors are connected to the correct pins as well.

#define amp0 A5
#define amp1 A2
#define PWMA0 6
#define PWMB0 5
#define enable0 13   //pins for first MegaMoto

#define amp2 A4
#define amp3 A1
#define PWMA1 9
#define PWMB1 10
#define enable1 12   //pins for second MegaMoto

#define hall0 50
#define hall1 51
#define hall2 52
#define hall3 53  //interrupt pins for hall effect sensors

#define forwards0 14
#define forwards1 15
#define forwards2 16
#define forwards3 17

#define backwards0 18
#define backwards1 19
#define backwards2 20
#define backwards3 21 //relay pins

#define switch0 4   //Up button
#define switch1 7   //Down button

Step 6: Conclusion

In this Instructable we learned how you can synchronize the motion of multiple motors with Hall effect sensors. By counting the output pulses of the sensors, you can ensure that all the motors travel at the same speeds. The current is also monitored, to ensure that the actuators don't get overloaded. Next week Part 3 will be launched, teaching how to use PID control to give more precise control of the actuators by using the counts from the hall effect sensors.

If you'd like to take a look at our selection of linear actuators, motions control systems, and microcontrollers, please visit us at progressiveautomations.com for all your actuator needs! We can even build a custom actuator or control system for you based on your own custom specifications with the help of our highly trained staff of engineers. You can learn more about the custom order process right here.

Follow, Like, and Subscribe!

Facebook - Twitter - Instagram - LinkedIn - YouTube