본문 바로가기

사물인터넷

Stepper Motor with Hall Effect Limit & Homing Switches

https://dronebotworkshop.com/stepper-motor-hall-effect/

 

Controlling Stepper Motors with Hall Effect Switches | DroneBot Workshop

Learn how Hall Effect Switches work and how to use them as Limit Switches and Homing sensors for stepper motors. Complete with Arduino sketches.

dronebotworkshop.com

Introduction

Today we will look at implementing both limit switches and homing sensors for a stepper motor.

We have used stepper motors in many of our projects and experiments, and for good reason. Stepper motors allow precise control of their shaft position, using microstepping you can control this to a fraction of a degree.  This is why stepper motors find use in application like printers and DVD drives, they are a great choice anytime a powerful yet precise motor is required.

But, unlike servo motors, stepper motors do not normally have an internal feedback mechanism to determine the current shaft position. So when your stepper-based project is first powered up it really has no idea what position the motor shaft is currently sitting at. 

In some cases, you really don’t care what the current shaft position is, but you do want to know when the stepper (or the mechanical devices being driven by the stepper) has reached a certain point in its travel so you can stop or reverse the motor.

Fortunately, there are a couple of ways to resolve these issues.  We’ll take a look at a couple of methods today.

Limit Switches and Homing Sensors

Limit switches and homing sensors are ways of determining the position of either the stepper motor shaft itself or of the mechanism that the shaft is powering.  

Both schemes use switches of some sort that are activated when the shaft or mechanism reaches a certain position.

Limit Switches are generally used in pairs and are placed at the mechanisms the end of travel. Once the switch is activated the motor will respond, how it responds is entirely up to you, but in most cases, it will either stop or reverse.

A Homing Sensor or switch is placed in a predetermined position to provide a reference point. Once it is activated the motor “knows” where it is. From there you can use the precision of the stepper motor to accurately move into any position you wish.

A common use of the homing sensor that you are probably familiar with is in a printer.  Many printers go through a sequence of operations when they are powered up, and one of them is to move the print carriage to the homing position to get a reference point.

Types of Switches

You can use many kinds of switches to build homing sensors and limit switches. 

  • Conventional (mechanical) switches: These are easy to work with and are available in a wide variety of styles. Microswitches are commonly used in this application. They work well, but as they are mechanical they can wear out. They are also not suitable for some environments where they would be subject to dirt, oil or moisture.
  • Optical Switches: Usually these are either the reflective type or the “optical source-sensor” type with a gap in them.  Either reflecting or breaking an infrared light beam will trigger the switch. They don’t experience wear and tear and can work in many environments, providing their light source is not obscured.
  • Hall Effect switches: These devices respond to magnetic fields, so they can be triggered by a small magnet. They don’t experience wear and tear and are not affected by light or heat (within reason). They can, however, be falsely triggered by stray magnetic fields if not mounted properly.

Today we are going to build our homing sensor and limit switches using Hall Effect switches. If you wish you can use the same code for other types of switches as well.

So let’s see how these devices work.

Hall Effect Switches

As mentioned above, Hall Effect switches respond to magnetism. 

Don’t confuse these with magnetic reed switches. A magnetic reed switch is a mechanical switch that can be activated by a magnet. These are often used in security systems.

A Hall Effect switch, on the other hand, is a semiconductor device that is sensitive to the strength, polarity, and direction of the magnetic field. 

History 

In 1879 Edwin Hall, an American physicist, was exploring the interaction between electric currents and magnetic fields when he observed a voltage being produced when DC current was sent through a conductor in the presence of a magnetic field.

Later the Hall Effect was also observed in semiconductors when the magnetic field was perpendicular to current flow.

Hal Effect sensors and switches are inexpensive and used in a variety of applications. They are available in two configurations:

  • Analog – These can be used to measure the strength of a magnetic field.  While not accurate enough for instrumentation they can be used in experiments and toi create switches at hat trigger at a specific magnetism level.
  • Digital – Theses output a binary on or off when a magnet of the correct polarity is brought near. These are the types of devices we will use today.

For the most part, I’m using the terms “Hal Effect Switch” and “Hall Effect Sensor” to mean pretty well the same thing, although technically the “sensor”: is the analog variety.

Among the digital Hall Effect switches you’ll find a variety of configurations. The two most common are Latching and Non-Latching.

Non-Latching Hall Effect Switches

Non-Latching, or “normal,” Hall Effect Switches react to the presence of a magnetic field of the correct polarity.  They work as follows 

  • When a magnet is brought near the switch it is activated. It will remain activated as long as the magnet is nearby.

  • If the magnet is removed the switch is deactivated.
  • If the magnet is returned, but in the opposite polarity, the switch remains deactivated.

  • However, if you apply the magnet in the opposite polarity on the other side of the Hall Effect switch it is activated.

These are the most common types of Hall Effect switches and are available as individual semiconductors and on modules with indicator LEDs.

They are also available in a variety of configurations, some that can withstand extreme environments. They are used in a variety of industrial control and alarm applications. 

Latching Hall Effect Switches

Latching Hall Effect Switches hold their value after being activated. They are not as common as their non-latching cousins, a common use for them is in alarm systems.

They can be reset by removing the DC current, or by applying a magnetic field of the opposite polarity.

  • When a magnet is brought near the switch it is activated, just as with the non-latching switch.
  • If the magnet is moved away the switch is still activated.
  • If the magnet is returned it does not change the switch status. In other words, this is not a “toggle”.
  • If the magnet is returned in the opposite polarity the switch is unlatched. It can also be unlatched by removing its current.

Testing Hall Effect Switches and Sensors

Hall Effect switches sensors are very easy to work with.  They are available in modules, which are the easiest variety to use, and as discrete semiconductors.

If you use the semiconductors you’ll need to use a pull-up resistor with them when interfacing to a microcontroller or other logic. Of course, if you’re using an Arduino you may use the built-in pull-up resistors, which are called in your Setup routine.

I did a few experiments with just a 5-volt power supply. I used both a module with a non-latching (regular) Hall Effect switch and a discrete semiconductor, the US1881 Latching Hall Effect sensor.

As the module has a built-in LED I just used that to rest the switch. Bringing a magnet of the correct polarity worked as one would expect, the LED was activated.  Reversing the magnetic polarity had no effect on the switch.

To test the Latching switch I used my Logic Probe to display the output levels. I added a 10K resistor as a pull-up.

The switch sent the output HIGH when a magnet was brought near. The output stayed HIGH when the magnet was removed.

Reversing the polarity of the magnet sent the output LOW again.

Hall Effect Switches with Arduino

You can interface a Hall Effect switch with a microcontroller, like an Arduino, to build sophisticated control systems.

You can also interface a Hall Effect switch to an Arduino to create something incredibly simple. Which is exactly what we will be doing for our first experiment.

A Hall Effect switch is essentially just another type of switch or sensor that outputs a logic level that changes in response to an action. A fancy way of saying that it goes HIGH or LOW when you bring a magnet near it.

Simple Arduino Hookup

For this experiment, you’ll need an Arduino Uno (or actually any type of Arduino) and a Hall Effect Switch module.

Optionally, you can add an LED and a 220-ohm dropping resistor if you wish. Or you can just use the Arduino’s built-in in LED and save yourself some wiring.

And, of course, you’ll need a magnet to test your Hall Effect switch!

The hookup is very simple, the output of the Hall Effect sensor is sent to pin 3 of the Arduino. The sensor is powered from the 5-volt and ground connections on the Arduino.  

If you wired up the LED it is connected through the dropping resistor to pin 13 of the Arduino. The cathode of the LED is grounded.

Once you get this all connected it’s time to load some code.

Arduino Simple Hall Effect Sketch

If you think that the wiring for this experiment is easy, wait until you see the sketch!

This could very well be the simplest Arduino sketch you will ever see.  Its entire purpose is to read the Hall Effect switch module state and indicate it on the LED.

Hall Effect Demo

/*
  Hall Effect Switch Demo
  hall-effect-demo.ino
  Uses Hall Effect sensor to activate LED when magnetic field detected
 
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Define connections
#define HALL_SENSOR      3
#define LED      13
 
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(HALL_SENSOR, INPUT);
  digitalWrite(LED, LOW);
 
}
 
void loop()
{
  // Read sensor and display status on LED
  digitalWrite(LED, !digitalRead(HALL_SENSOR));
}

 

The sketch starts by defining constants to represent the Hall Effect switch input and LED output pins.

In the setup, the Hall Effect switch connection is set as an input while the LED is set up as an output. A LOW value is written to the LED, to turn it off.

We then get to the loop.

All that goes on in the loop is that the LED is set to the opposite state of the Hall Effect sensor. Why the opposite?

This is done because the Hall Effect sensor is active-low, it’s output goes slow when it is activated.  As this is the opposite of what we want the LED to do we simply invert its value with an exclamation mark.

So when the Hall Effect sensor is activated it goes LOW, sending the LED HIGH, which turns it on. Deactivate the Hall Effect switch and the LED goes off.

Give the sketch a try, it should work exactly as you expect it to. 

Of course it’s a fairly useless sketch, you could accomplish the same thing with a transistor and eliminate the Arduino completely,  But serves to illustrate how you can trigger an Arduino with a Hall Effect switch.

You could expand upon the sketch and use something more exciting than an LED as an output if you wish.

Controlling a Stepper Motor – Limit Switches

So now that we know how Hall Effect switches work let’s put them to use with our stepper motor.

The first thing we will do is use Hall Effect switches as Limit Switches.  This is a very common application, as described earlier.

In my test setup I’m going to limit the motor shaft itself, but in “real life” you’ll probably be installing the limit switches on the mechanism that is powered by the stepper motor, and not the motor itself.

Limit Switch Demo Hookup

To perform this demonstration you’ll need an Arduino, two Hall Effect Switch modules, a bipolar stepper motor, and an A4988 stepper motor driver module.

You’ll also need a power supply suitable for your stepper motor, I used my 12-volt bench supply.

One thing that I didn’t show in the schematic, but is very commonly used with the A4988, is a 100uf electrolytic capacitor. You will use this as a decoupling capacitor, placed across the 12-volt power supply leads. Be sure to observe polarity.

The pinouts of the A4988 module are shown here:

Note that the Microstepping leads (MS1-MS3) will allow you to step the motor down to 1/128 step. You can experiment with this if you like. In our experiment we won’t be microstepping. 

You will need to know the pinouts of your bipolar stepper motor so you can match it with the A4988 module outputs. If you aren’t sure you can use a multimeter to measure coil resistance, polarity in this case will be “ hit and miss”.   The Internet is a great source for motor data sheets.

Here is how we will hook everything else up:

The Hall Effect switches are connected to pins 2 and 3 on the Arduino Uno. You’ll need to keep them on these pins as we’ll be using interrupts in our sketch. The DIR and STEP inputs to the A4988 are connected to Arduino pins 10 and 11, you can switch these to any other two pins if you prefer.

On the A4988 connect the RESET and SLEEP pins together. Then connect the two power supplies, the stepper motor coils and the connections to the Arduino.  You might want to turn off the 12-volt power supply until everything is ready and the sketch is loaded.

You’ll need to arrange your two sensors so they are at the desired end of travel for your motor or mechanism.  In my demo, I just placed them on each side of the motor rotation.

The object you are trying to sense will need a magnet on it. Make sure to try it out with a Hall Effect switch and make any adjustments you need to.  Double-check to make sure you have the polarity correct!

Once you have the circuit wired up and the mechanical arrangement with the magnet and Hall Effect switches perfected it’s time to look at the sketch.

Arduino Limit Switch Sketch

The sketch we will be using to control our stepper motor with two limit switches is as follows:

Stepper Motor Limit Switch Demo

/*
  Stepper Motor Limit Switch Demo
  stepper-limit-demo.ino
  Uses Hall Effect sensors as limit switches
  Uses A4988 Stepper Motor Driver module
  
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Define connections
#define HALL_SENSOR_A      2
#define HALL_SENSOR_B      3
#define DIR      10
#define STEP      11
 
// Variables
int pd = 4000;       // Pulse Delay period
boolean setdir = LOW; // Set Direction
 
// Interrupt Handlers
 
void limit_a (){
 
  // Reverse motor
  setdir = !setdir;
  
}
 
void limit_b (){
 
  // Reverse motor
  setdir = !setdir;
  
}
 
 
void setup() {
  
  // Setup the stepper controller pins as Outputs
  pinMode(DIR,OUTPUT); 
  pinMode(STEP,OUTPUT);
  
  // Setup the Hall Effect switches as Inputs
  pinMode(HALL_SENSOR_A, INPUT);
  pinMode(HALL_SENSOR_B, INPUT);
  
  // Attach interrupt pins to handlers
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_A), limit_a, FALLING);
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_B), limit_b, FALLING);
   
}
 
void loop() {
  
    digitalWrite(DIR,setdir);
    digitalWrite(STEP,HIGH);
    delayMicroseconds(pd);
    digitalWrite(STEP,LOW);
    delayMicroseconds(pd);
 
}

 

 The sketch starts by defining the pins we will be using to connect our Hall Effect switches and the A4988 stepper motor driver module.   

We also create a couple of variables. The “pd” variable is the pulse duration or delay period, this determines how quickly we step our motor.  If you increase the value the motors will spin slower. Note that this value is the period in microseconds, not milliseconds.

The “setdir” variable is a boolean that holds the current motor direction.

Next, we define a couple of interrupt handlers, one for each of the switches. This is where we decide what to do when a switch is activated. In this simple example I’m just reversing the direction of the motor by flipping the value of the setdir variable, but you could do other things here as well if you like, 

You are also not restrained to doing the same thing for each limit switch, you could configure one interrupt handler to reverse the motor and the other to stop it if you wish.

Now into the Setup.

We set up our Hall Effect switch connections to be inputs and the connections to the A4988 as outputs. Then we attach the interrupt handler we just wrote to the two interrupt pins. Note that we activate the interrupts on the FALLING edge of the input pulse, as the switch will go from HIGH to LOW when it is activated.

Finally, in the loop, we drive the stepper motor by setting its direction and then manually creating a step pulse.

It’s a pretty basic sketch, load it up to your Arduino, power up the motor and watch it in action. Providing that you have the arrangement with the magnet setup properly you should observe the motor traveling back and forth between the two Hall Effect switches.

Controlling a Stepper Motor – Homing Sensor

Homing sensors and limit switches are really just two variations on the same theme.  The motor is moved to a reference point, and after that you can calculate its position step-by-step.

Homing is usually done when your project is first powered up or reset, so we will take care of it in the Setup routine in our sketch. That way once you have reached the Loop you can be sure that the motor is in the desired position.

The hardware setup for this experiment is identical to the Limit Switch demo, except you only need one Hall Effect switch.  You can disconnect the Hall Effect switch from pin 2 of the Arduino, or you can just leave it alone and ignore it, the sketch will only be looking at the switch on pin 3. 

Homing Sensor Arduino Sketch

Here is our homing sensor sketch:

Stepper Motor Homing Demo

/*
  Stepper Motor Homing Switch Demo
  stepper-homing-demo.ino
  Uses Hall Effect sensor as homing switch
  Uses A4988 Stepper Motor Driver module
 
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Define connections
#define HALL_SENSOR      3
#define DIR      10
#define STEP      11
 
// Direction Variable
boolean setdir = LOW;
 
void homefunction() {
  // Set motor speed pulse duration
  int pd = 4000;
 
  // Move motor until home position reached
  while (digitalRead(HALL_SENSOR) == 1) {
 
    digitalWrite(DIR, setdir);
    digitalWrite(STEP, HIGH);
    delayMicroseconds(pd);
    digitalWrite(STEP, LOW);
    delayMicroseconds(pd);
  }
 
}
 
void setup() {
 
  // Setup the stepper controller pins as Outputs
  pinMode(DIR, OUTPUT);
  pinMode(STEP, OUTPUT);
 
  // Setup the Hall Effect switch as an Input
  pinMode(HALL_SENSOR, INPUT);
 
  // Home the motor
  homefunction();
 
}
 
void loop() {
 
  // Loop, put code here
 
}

 

 

The sketch for homing the motor is pretty simple. As with the previous sketch we start by defining the connections to the Hall Effect switch and the A4988 motor driver.  We also define a variable to represent the motor direction.

Next, we create a function to home the motor.  This function sets the motor speed and then uses a while loop to move the motor until the homing switch is activated. Once the switch is activated the while loop is broken and the motor stops.

In the setup we setup the switch connection as an input and the A4988 connections as outputs.  Then we call the homing function to home the motor.

Once the motor has homed we jump into the loop. There is no code there, this is where you would place all of your code for your project.

Give the sketch a try. You should observe the motor moving into the home position and stopping when the sketch is run.

Your stepper has found its way home!

Conclusion

Hall Effect switches and sensors are reliable devices that can be used in a number of applications.   By coupling them with a stepper motor we can build devices that can move precisely under our command.

The sketches shown here may be simple but they serve to illustrate an important aspect of working with stepper motors.  Give them a try, and see how you would incorporate Hall Effect sensors into your next stepper motor design.

Resources

Code for this article – All of the Arduino sketches used in this article in a ZIP file.

Hall Effect – Wikipedia article detailing the Hall Effect