Door Control – Part 3 [BS2]

posted in: Tutorials 0

This article is part 3 of 3 discussing motorized door control. Each part will discuss a specific motor type as well as a specific limit switch type. In the previous articles I focused on using servos for motion and optical limit switches, and DC motors using mechanical limit switches. Finally, part 3 will discuss stepper motors using magnetic switches or hall-effect sensors. You can mix and match motor types with limit switch types as your project / application dictates. I will also discuss a few projects in which these ideas can be implemented.

Opening Doors with Stepper Motors

Stepper motors are pretty common in devices that require precision control and positioning, such as 3D printers and pick-and-place machines. Many years ago they were even used in floppy and hard drives to position the read/write heads. There are two main types of stepper motors; unipolar and bipolar. Unipolar motors are simpler to use and we’ll be using one in our project. Like the DC motor used in the previous article, stepper motors can’t be connected directly to the I/O pins of a microcontroller. With a stepper motor you still need a driver circuit. This driver circuit could be as simple as a several transistors for a unipolar stepper motor. For a bipolar stepper motor you would actually need a push-pull driver circuit since the coils actually change polarity. Interestingly, for either type of stepper motor we can use the same driver IC we used in the previous article, which was a quad half H-bridge [L293D].

Figure 1

I’m not going to get into the details of how an H-bridge works. There are many tutorials on the internet that can be found with a simple search. Suffice it to say that this H-Bridge IC allows us to not only energize the individual phases for the unipolar stepper motor, but also to change the polarity of the phases on a bipolar stepper motor. Figure 1 shows a highly simplified control diagram for our use with the unipolar stepper motor. This circuit will allow us to energize the individual phases of the stepper motor moving it a certain number of degrees per step. In our case 3.6 degrees per step meaning that 100 steps rotates the motor one revolution. This is done with four (4) NPN transistors by grounding each phase one at a time while the COM line is tied to the supply voltage. You could also tie the COM line to ground and sequence each phase to the supply voltage to move the stepper motor, which is what we’ll be doing in this demo.

A unipolar stepper motor works by energizing the four phases in sequence and then repeating that sequence to go in a specific direction. Since each step is a certain number of degrees it is easy to determine how much the motor has turned. Stepping the phases in the reverse order moves the motor the opposite direction with the same number of degrees per step. So repeatability in both directions is one of the positive characteristics to using a stepper motor. Four I/O pins are used to control the stepper motor. Each pin is an output controlling the specific phase of the stepper motor.

Figure 2

For door control, we’re not as concerned with precision in the steps per se, at least in this demo. However if your system involved a gear driven model and you wanted position control, these would be important properties. Figure 2 shows the wiring of the L293D on the breadboard area of the Board of Education for use in this demo. See the full schematic for more details.

Stepper Motor Demo

Figure 3

In the previous articles I showed you the demos I built for the servo controlled door and DC motor controlled door. In this article I have built an entirely new demo (Figure 3) with a Unipolar Stepper Motor in place of the servo / DC motor, and magnetic sensors in place of the optical sensors and custom contact switches on the previous demos. Other than those changes the demo units are virtually the same. Remember, the limit switches are interchangeable. I could have just as easily used optical limit switches here, or even mechanical, however this article demonstrates magnetic limit switches.

Figure 4

You may have noticed the Board of Education is upside down on the demo. That’s because I needed the breadboard area to be close to where the stepper motor wires came through the back due to short length. Figure 4 shows how the wires come through behind the Board of Education.

Figure 5

The stepper motor is mounted in the same position and manner as the servo and the DC motor in the previous demos. Like the previous demos, I chose a mobile power pack. Figure 5 shows the back side where the power supply sits opposite the Board of Education. The stepper motor wires run in between through the hole shown in Figure 4.

This stepper motor is unipolar motor. However, using the wiring configuration on the L293D and the same code you could actually run a bipolar stepper motor. The difference is the bipolar motor would not have a COM wire. But if you connected the four phases in the same manner it should work, making this circuit ideal for either type of stepper motor.

Magnetic Limit Switches

Figure 6

Magnetic limit switches could be any type that is sensitive to magnetic fields. So this could be a hall-effect sensor, reed switch or even the magnetic switches used in alarm systems (which are usually a reed switch). The sensors I used are shown in Figure 6. Essentially it is a normally open reed switch that closes when near a magnetic field. So you can use them in the same manner you would use a normally open push button switch.

Figure 7

In the schematic, each switch connects to ground and to the signal line, which is pulled to VDD via a 10K resistor. So the program sees a high on the line when that sensor is not active, and a low when it is. When the magnet is near the switch, it is closed. Figure 7 shows the upper limit switch having been triggered by the door moving up, stopping the door at that point.

Figure 8

Figure 8 shows the lower limit switch having been triggered by the door moving down, stopping the door at that point. I used tape to affix the magnetic switches and the magnet since it allowed me to make adjustments much easier. Once you have the ideal position, you can always drill out for the holes.

The magnet is also taped to the bottom right edge of the door so it could be adjusted. This setup works quite nicely, however in a more professional application I would embed the magnet in the door. I would probably use two magnets as well, one for each direction limit which would provide more control over the door travel. The switches would be mounted so they could be adjusted. You might have noticed the mounting holes allow for some adjustment in one direction. So, if I turned the switches 90 degrees, I could easily adjust the limit for up / down position.

Bill of Materials

  • (1) Board of Education Full Kit USB (#28803)
  • (1) Li-Ion Power Pack Full Kit (#28989)
  • (1) Unipolar Stepper Motor
  • (1) Bi-Color T1-3/4 LED (#350-00005)
  • (1) 220Ω, 1/4W, 5% Carbon Film Resistor (#150-02210)
  • (4) 10K, 1/4W, 5% Carbon Film Resistor (#150-01030)
  • (4) N.O. Pushbuttons/Switches/Contacts
  • (1) L293D or other H-Bridge Driver IC
  • (1) Magnet

Source Code

The example code is for the BASIC Stamp 2 and can easily be ported to any other microcontroller. It defines all the I/O definitions first, as well as the constants that define the active / inactive state of the switches / sensors (yes / no). During initialization, the code checks to see if the door is partially open. It does this by checking to see if both limit switch sensors read inactive (not closed). If this condition is met, the door close routine is called. Otherwise the code enters the main loop, while monitoring the push buttons.

The open / close routines are very simple. Upon entry into the routine the limit switch is checked. If it is active (in this case, closed) the routine exits. This is always the first thing done in the routine in case the door is already at its limit when the routine is called. We don’t want to move the door before verifying if it has reached its target position. Since we’re using a stepper motor to move the door it must be controlled by a driver, in this case the L293D. This driver IC will receive 4-bit phase steps on the four inputs to control the motor direction. Opening or closing the door simply involves outputting the phase steps in one a forward or reverse fashion. The loop continues until the door reaches the limit switch and then exits, setting the status LED to the corresponding color for that routine. Green if the door is open. Red if it is closed. The stepper motor remains energized in the phase it was last in. This prevents the door from moving when it should not.


Stepper motors can require higher voltage and current to operate than a Servo or DC motor. Most stepper motors run at 12 or 24 volts. The stepper motor used in this demo is rated at 24V, but is being run at just under 12V, reducing its effective torque. That does not affect this demo though, since the door is so light. Stepper motors also require four I/O pins to operate directly, whereas the servo required one and the DC motor required two.

Magnetic switches are sometimes a little finicky on where they engage. So while the stepper motor can provide a degree of precision, the magnetic switches aren’t quite so precise in most applications. There’s also the possibility that a stray magnetic field could trip one or both switches resulting in a false reading.

Final Thoughts

While stepper motors are very common in precision mechanisms, they are not usually used in a door system. On the other hand if you were trying to build a vent or baffle system and needed precise positioning, the stepper motor could help you achieve that. Keep in mind that, at startup, the stepper motor position is unknown. Until the stepper motor moves to a known position, it could be anywhere. This is why old-school disk drives would step the head the maximum number of positions it would take to get the head to the home position and then use that position as a reference for how many steps it moved.

Using two buttons in the demo was done for clarity, completeness and simplicity. The code and hardware could easily be designed to work with just one button, or even none, but rather receive the command from some other part of your code or a flag variable.


Door Control – Part 1 [BS2]

Door Control – Part 2 [BS2]

Discuss this tutorial on Savage///Chats

This project was published in the November 2016 issue of Servo Magazine

Door Control – Part 3 by Chris Savage is licensed under CC BY 4.0


Leave a Reply