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. 

Stepper Motors and Hall Effect switches

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.

Hall Effect sensors

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. 


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.

Hall Effect non latching 03

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

Hall Effect non latching 01

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

Hall Effect non latching 02

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.

Hall Effect experiment setup

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.

Hall Effect experiment

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!

Simple Arduino Hall Effect hookup

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.


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:

A4988 stepper module

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:

Arduino limit switch hookup

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.

Limit switch experiment 02

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:


 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:


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.

Limit switch experiment 01

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!


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.


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


Stepper Motor with Hall Effect Limit & Homing Switches
Stepper Motor with Hall Effect Limit & Homing Switches
Article Name
Stepper Motor with Hall Effect Limit & Homing Switches
Learn how Hall Effect switches work and how to use them to build Limit Switches and Homing Sensors for a stepper motor.
Publisher Name
Dronebot Workshop
Publisher Logo

If you have a question...

Comments about this article are encouraged and appreciated. However, due to the large volume of comments that I receive, it may not be possible for me to answer you directly here on the website.

You are much more likely to get answers to technical questions by making a post on the DroneBot Workshop Forum. Your post will be seen not only by myself, but by a large group of tech enthusiasts who can quickly answer your question. You may also add code samples, images and videos to your forum posts.

Having said that, please feel free to leave constructive comments here. Your input is always welcome. Please note that all comments may be held for moderation.

Newest Most Voted
Inline Feedbacks
View all comments
Vance Mayfield
1 year ago

Hello Bill, I very much like your teaching style. This information you have presented regarding reversing steppers is very close to what I am looking for to replicate a small project. I am completely new to Arduino programming, but I do intend to use an Arduino microprocessor to run my project. Could you possible show some minor tweaks to your coding? My fear is, I will not apply proper syntax or logic if I try to simple combine the two functionalities on my own. Project Scope: Try to replicate the Magmov generator depicted in the following youtube videos. 1.… Read more »

Kevin Enzian
1 year ago

Recently found your website and enjoying every bit.

How accurate is a hall-effect switch? In the last section of this tutorial, you used a hall-effect for homing. However, in the video the stepper motor stopped short of the sensor itself. Is the sensor dependent upon the intensity of the magnet? Is it as accurate as the accuracy of the steps possible in the motor itself; ie, in this case, down to 1.8deg?

Kevin Enzian
1 year ago

Thanks. Appreciate the quick response.

aanan stha
11 months ago

hi , i got a lots of information on stepper motor from your videos and i want know more on how we can use those hall sensor one to run a motor in clockwise direction and another to stop the motor after certain degree of rotation and again resume to rotate in clockwise.

Henri Portelli
10 months ago

Hi Bill. Thanks for devoting so much time in preparing your tutorials, your method of explaining is pleasantly clear. I want to follow up with this article to set up a turntable for my model railway layout but unfortunately the link to the code seems to have dropped. It would also be of great help if you would post a PDF file of the article, similar to the one of the Stepper Motors article which we can download and have at hand while tinkering.

Larry Marek
10 months ago

Hi! I’m trying to piece together a project and your video here was just what I needed to get me going in the right direction, so thanks! Some questions tho, particularly about the magnet itself, in your experiment you put the magnet on an alligator clip. Fine enough, but any ideas how to get the magnet more permanently attached to the motor? Do you suppose I could glue the magnet to the motor’s shaft? How close to the magnet does the hall effect sensor have to be? And last, (Whew!) maybe a link to those nice little magnets?


8 months ago

Sir can I use proximity sensor for this project and one direction I want to change speed of motor. Iam new for coding help me please

7 months ago
Reply to  Raj

/* Stepper Motor Dual Limit Switch Program Uses Hall Effect sensors as limit switches Uses Gecko G251 Stepper Motor Driver module */ // Define connections – pins #define HALL_SENSOR_A 2 #define HALL_SENSOR_B 3 #define DIR 10 //Gecko #define STEP 11 //Gecko #define SPD A0 //potentiometer // Variables int pd = 500; // Pulse Delay period boolean setdir = LOW; // Set Direction // Interrupt Handlers void limit_a (){ // Reverse direction of motor travel setdir = !setdir; } void limit_b (){ // Reverse direction of motor travel setdir = !setdir; } void setup() { // Setup the stepper controller pins… Read more »

7 months ago

Thanks for the episode – great wealth of information

I’m trying to apply your logic to my project. I have added a POT to control the motor speed, but i can’t work out how to incorporate “debounce” functionality on the proximity switches. I’ve tried to use libraries such as ezBounce and to manually write code to wait (millis). Can you redirect me to something I could incorporate in this sketch with interrupts.

David Marschik
6 months ago

I have a hard time reading the code from the screen, I think your programs as presented are great. I am 83 years old and have a hard time reading the red print. you have made the Arsuino a real thing for me.

Mark Page
6 months ago

Nice video, you always do a great job. Truly appreciate the hard work and clarity!

Malatesh D
2 months ago

What I am requesting in you is Push Button Hall Effect Homeing Switch
Its code needed and along with its diagram
What I am requesting in you is Push Button Hall Effect Homeing Switch
Its code needed and along with its diagram
What I am requesting in you is Push Button Hall Effect Homeing Switch
Its code needed and along with its diagram

Last edited 2 months ago by Malatesh D
2 months ago

Thanks for the great tutorial,
I am brand new to arduino and this had me up an running in no time.

Is there any easy way to add acceleration/deceleration to the start stops? I am driving a linear axis with a leadscrew and the start stop is rather violent if I speed up and considerable amount.

Any tips are greatly appreciated.

David Norton
2 months ago

Hi Bill – I’m building a set of powered exoskeleton arms for my friend who’s arms are paralyzed due to ALS, I’m finding your videos very helpful! After watching this video I’m considering using hall effect sensors with my big stepper motors to home the elbow and shoulder motors as well as set limits. The torque involved is significant and I need everything to be very automated and safe since my friends only has limited use of his hands and his muscles are extremely weak. Do you think Hall Effect sensors are the best sensors to use for this application?

15 days ago

Hello Sir
I need your help in a stepper motor project.
I just need an Arduino code.

Would love your thoughts, please comment.x