Download PDF Parts List View on YouTube Download Code


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.


Parts List

Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.




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
Notify of

Inline Feedbacks
View all comments
Vance Mayfield
4 years 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
4 years 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
4 years ago

Thanks. Appreciate the quick response.

aanan stha
4 years 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
4 years 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
4 years 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?


4 years 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

4 years 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 »

4 years 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
4 years 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
4 years ago

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

Malatesh D
3 years 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 3 years ago by Malatesh D
3 years 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
3 years 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?

3 years ago

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

Gerth Moberg
3 years ago

Why so difficult to find downloading instructions and the scetch file on this web page?

How do I make the code so I can push a button and
3 years ago

How do I make the code so I can push a button and stepper motor drives to Halifax sensor. But when I push a different button stepper motor will run past that Halifax sensor and go to a separate one without stopping at the first one need help if someone could help that would be awesome

Louis Huemiller
3 years ago

Wondering if instead of using a limit switch or homing sensor, could a mechanical stop be relied on to get to a known position. As an example, let’s say by design the maximum number of steps is 10,000. On power-up, could the system make 10,001 steps in one direction, knowing at some point it would hit a mechanical stop and then skip the rest of the steps. This in part that when a stepper motor needs to produce more force than what is available, it usually skips a step instead of physically breaking. This assumes that the mechanical stop is… Read more »

Greg Mansker
3 years ago
  • Bill, As always great video. I purchased Hiletgo NKJ-5002c, (picked that one because of mounting). With the help of the forum I’m making progress with my project. I’m concerned with the response distance when checking by hand on the bench. I looking for fairly accurate alignment with the homing, and have some questions.
  • In the code you mention “FALLING” ?
  • After homing can you can keep going same direction while the switch still sensing
  • If I need more accurate homing or fine adjustment, can I add some steps after the sensor is detected?


Enrique Iglesias
3 years ago

Hello Bill. I found this website looking for a solution to a problem hall effect, with arduino and cncshield. Specifically, for a small homemade cnc, I have assembled an arduino uno with a cnc shield on top, but instead of using a mechanical limit switch and homing, I want to use Ky-024 digital output hall effect sensors (same as Ky003 but regulable. The problem is that I don’t know where to plug in those sensors, since the cnc shiel covers the arduino, and I don’t know if the limit switch ports of the cnc shield are capable of receiving only… Read more »

2 years ago

Hello Bill, I tried this sketch and works well, but i don’t know how to add a pause when the magnet reaches the limit switch. If I can get help?

2 years ago


Last edited 2 years ago by ZAHID
Peter Elsler
2 years ago

Hello Bill, I want to use a Homing Sensor to be used together with an Incremental Rotary Incoder. I am in doubt, whether to use a Hall effect sensor or a Light bridge sensor to define the 0 point. I need to approach the zero point from both sides, therefore I have some concerns with the Hall effect sensor, coming HIGH a little bit before and after the real zero point. I would prefer the Hall effect sensor because their is no interference related to dust, but I need define the zero point with the maximum precision. What do you… Read more »

Alan Burchi
2 years ago

My name is Alan and I work with RC modeling, I was wondering is the setup (wiring) similar with a Conventional (mechanical) limit switch? I plan on putting limit switches at two ends of an elevator (aircraft carrier elevator) to stop it at the flight deck and at the hanger deck with a pause at each deck. Plan on using an Adafruit motor shield & Elegoo UNO R3. I’m new to this, but want to some realism to my modeling. I have watched most of your videos and have learned much thank you for your time in making these videos.

2 years ago

Hello Bill. I am from Indonesia. can you help me to do my experiment if i use a proximity sensor for circular motion back and forth. with a slight modification of your project about
Controlling a Stepper Motor – Homing Sensor

Vishal Jain (from India)
1 year ago

I am struggling with homing position of Stepper Motor. My code is below to modify. Please help me out. Your reply is highly appreciated. Thanks a lot. int MoveSpeed = 400; //step in Microseconds (100 seems to give faster speed) // Pin connections:  # define EN_Sig 6 //Green ENA+ stepper motor enable , active low    # define DIR_Sig 2 // Blue DIR+ axis stepper motor direction control # define PUL_Sig 3 //White PUL+ axis stepper motor step control      // Other end of the input buttons should be grounded # define UP_Sig A3 // Connect to up signal button # define DN_Sig… Read more »

Jim (SafetyWrench)
1 year ago

Hello Bill, I’ve been watching your instructional and and information videos for the last couple of years. Thank you for the good service you do for the Maker community. I’ve used information from your lessons in several of my projects. I am a fan. 🙂 I have a couple of suggestions for your consideration: 1) I was wondering if you have a cross reference catalog or excel spreadsheet (kind of a card index) of your learning sessions in order to make it possible to do a deep search for specific tutorial information. For example: after learning about Hall Effect sensors… Read more »

Rob Huyben
1 year ago

Hello Bill, for an Art project I want to add a code to your limit switch demo, so that it pauses 2 minutes and than rotates back to the other switch and pauses again 2 min. and back, etc. in an infinite loop, and that every time again when the power is connected.
Could you help me out with that?
I use a Nema 23 motor and a DM542EU driver ( with only DIR and STEP connected ) the wiring is according your schematic.