Door Control – Part 2 [BS2]

posted in: Tutorials 0

Have you ever wanted to build a project that involved opening a door or access panel under motor power? This project discusses several ways to accomplish such a goal, including different types of motors that can be used, as well as different types of limit switches.

This article is part 2 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 article I focused on using servos for motion and optical limit switches. In this article I will discuss DC motors using mechanical limit switches, and finally, part 3 will discuss stepper motors using magnetic switches and 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 DC Motors

DC motors are pretty common in a lot of motor drive units such as those used in robotics, linear actuators, belt-drive systems, etc. Unlike a servo, a DC motor can’t be connected directly to the I/O pins of a microcontroller. The servo has its own internal drivers and logic to control the servo based on signals from the microcontroller (or R/C receiver), however, with a DC motor you still need a driver circuit. This driver circuit could be as simple as a transistor to turn the motor on / off. However if you need directional control of the motor then you would use a circuit called an H-Bridge.

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 an H-Bridge allows us to not only turn the motor on and off, but to reverse the polarity, changing its direction. While the servo was able to change direction with a single I/O pin based on the pulse width, the H-Bridge has two inputs.

Figure 1

Figure 1 shows a highly simplified H-Bridge diagram. The M1 and M2 inputs control the polarity on either side of the motor. Of course, there is a little bit more to it than this, but I’m going to use an L293D IC, which is a quad half h-bridge. By wiring everything up properly, this IC can be used to control two DC motors. For purposes of this project I am only going to use one half of the IC since I am only controlling one motor.

When you connect a DC motor to a power supply it spins in one direction. To change directions you must reverse the connections to the power supply. The H-Bridge does this for us using the two inputs to set the polarity for each terminal. So if you make one terminal high and the other low the motor spins in one direction. Reverse those signals and the motor spins in the other direction. Make them both low and the motors stops, since both terminals are effectively at ground level.

Figure 2

Figure 2 shows the wiring of the L293D on the breadboard area of the Board of Education. See the schematic for more details. A high-resolution schematic and source code are contained in the ZIP file under resources.

DC Motor Demo

In the previous article (Door Control – Part 1) I showed you the demo I built for the servo controlled door. In this article I have built an entirely new demo with a DC Gear Motor in place of the servo, and custom contact switches in place of the optical sensors on the previous demo. Other than those changes and the addition of a driver IC (L293D), 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 magnetic / hall-effect, however this article demonstrates mechanical limit switches.

Figure 3

Also note that this motor (Figure 3) is totally overkill for this demo. This motor has the strength to tear the demo apart if there were a problem with the motor shutting off while opening the door. A much smaller motor could have been used for this demo but I wanted to use parts readily available and easy to mount and this motor fit the bill. It’s also geared down which helps the door move slower, however that also increases the torque of the output shaft significantly.

Possible Issues

The DC motor will tend to keep going for a bit when power is removed. This means that when the door reaches the point where the limit switch is it will want to travel a bit further. For this reason I am using limit switches which allow some leeway. The door is also very light, so the limit switches had to be made to work with a very light touch. In a larger application / project this probably wouldn’t be an issue, but it is something to keep in mind. Some H-Bridge configurations allow something called, “braking” which can help stop the motor faster. This is a function of the H-Bridge if supported.

One more thing to consider is that in this specific demo I am using a power source and motor that can supply / use more current than the L293D can switch, so if there were an issue with the motor stalling or the door binding the IC might be damaged. Always use a driver capable of handling the maximum stall current your motor could draw. With everything operating smoothly the L293D will be running within its limits, however you should never assume that will be the case in a real-world application.

Mechanical Limit Switches

Mechanical limit switches could be any electro-mechanical device that makes or breaks contacts when moved. This function could be performed by push button switches, whiskers, conductive tape, metal strips, etc. I have even seen a wooden clothes pin with metal foil used as a switch!

Figure 4

In this case we’re going to use a whisker system since we need the limit switches to allow for some play past their normal limit. Figure 4 shows the upper limit switch. Notice that the whisker is flexible and can bend some while maintaining contact. The limit switch boards have a 3-pin header to make them easy to connect. Each board has ground, power and the signal pin, as well as a 10K pull-up resistor.

Figure 5

Figure 5 shows the lower limit switch. Again it is set up to allow for some flex, however since this demo uses such a light door with the downward force being only the weight of the door, and the control (fishing) line essentially going slack when fully down, overshoot on the door closing will not be an issue in this demo. The position of the whisker in relation to the 3-pin header it shorts against can be easily adjusted. As a note, I borrowed this idea from the Parallax BoE-Bot whiskers, which use a similar system to detect when a whisker is pressed.

Figure 6

Figure 6 shows the entire door assembly and limit switches. You can see I added a long screw in the door itself to provide a way for the limit switches to detect the door moving up and down within the demo.

Bill of Materials

(1) Board of Education Full Kit USB
(1) Li-Ion Power Pack Full Kit
(1) DC Gear Motor
(1) Bi-Color T1-3/4 LED
(1) 220Ω, 1/4W, 5% Carbon Film Resistor
(4) 10K, 1/4W, 5% Carbon Film Resistor
(4) N.O. Pushbuttons / Switches / Contacts
(1) L293D or other H-Bridge driver IC

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 DC motor to move the door it must be controlled by a driver, in this case the L293D. This driver IC will receive simple logic levels on the two inputs to control the motor direction. Opening or closing the door simply involves making one or the other motor control pins high and making them both low to turn it off. The loop continues until the door reaches the limit switch and then exits, turning off the motor and setting the status LED to the corresponding color for that routine. Green if the door is open. Red if it is closed.


As mentioned earlier in this article, DC motors can overshoot their target when opening a door. This is because when you turn off the motor, most will continue spinning for a bit. This is especially true of geared motors. You can compensate for this in the limit switches, by setting them to allow some overshoot, and also by using an H-bridge that can brake the motor, stopping it sooner. Gearing the motor lower may also help. Some systems include a tension spring so that at each limit the drive system can absorb the overshoot.

Mechanical switches are more likely to fail over time. Contacts can go bad. Wear and tear on the switch can break it. In an end-use application it is recommended that you also include some sort of protection against a bad mechanical limit switch. This is often done by measuring the current draw of the motor, and if it exceeds a certain value, shutting the motor down. Sometimes a position encoder is used in conjunction with the limit switch as well. With mechanical limit switches it is often possible to determine the most likely failure mode (open / closed) and design around it, such that the most likely failure mode is the inactive position, resulting in the door not moving during a failure.

Final Thoughts

While DC motors are very common in door mechanisms, they are usually incorporated into a linear actuator. A linear actuator contains a DC motor on a worm gear, which moves a shaft in / out. Most linear actuators include built-in limit switches, and some even have encoders to provide positional feedback. They will also usually switch their own internal power so you don’t need to monitor the switch and control the power.

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. I’ll be discussing other options for limit switches in the next part.


Door Control – Part 1 [BS2]

Door Control – Part 3 [BS2]

Discuss this tutorial on Savage///Chats

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

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


Leave a Reply