Door Control – Part 1 [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 1 of 3 discussing motorized door control. Each part will discuss a specific motor type, as well as a specific limit switch type. In this article I will focus on using servos for motion and optical limit switches. Part 2 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 Servos

In the hobby world, moving linkages and small assemblies can easily be done with standard hobby servos. They’re used in many radio control planes, boats and cars. In fact I have used hobby servos for many years in various projects to move and control mechanical systems in many projects. I have even used them to open and close small doors.

Figure 1

In Figure 1 you can see a project listed on the Savage///Circuits website called, “Build A Better Mousetrap”. This project uses a standard hobby servo to open and close a trap door, allowing you to capture a mouse without injuring it. A BASIC Stamp 2 MCU reads an infrared sensor and controls the servo, which is a standard hobby servo. The nice thing about standard servos is that you can control position based on the pulse value sent to the servo. Standard servos have internal feedback and know their position, but are limited to about 90-180 degrees of motion.

Standard servos can be used to control valves and large mechanical switch assemblies. But if you need to move something with a servo that requires more range than a standard servo, then you might need to use a continuous rotation servo. A continuous rotation servo is modified to allow the servo to continuously spin, but at the cost of no longer having positional feedback, and it lacks the ability to stop at a specific position. In a sense they’re more similar to a geared DC motor, except that they still require pulses to control. They do not, however, require a motor controller, making them easier to use in applications that don’t require a high degree of torque or speed.

Schoolhouse Greenhouse

A middle-school technology team contacted me at Parallax Tech Support one day about a project they were working on. This team of girls was creating a model greenhouse using a BASIC Stamp Microcontroller and a Parallax Continuous Rotation Servo. The group was trying to open and close a door on the greenhouse to regulate air flow but quickly realized they could not get consistent control of the servo. Turning it for the same number of pulses in one direction or the other did not result in the same amount of movement each way.

There was no encoder or active feedback to set a position, nor was it convenient to add an encoder to the system. The solution was to implement a limit switch of sorts, but they weren’t sure how to read the switches and move the servo at the same time. Furthermore, they were limited on options for a limit switch due to space constraints as well as the way the door mechanism operated.

Based on the information provided I created a small demo of the style of door they were using. This is a vertical sliding door and was lifted up by the servo using a piece of fishing line or string wrapped around the servo shaft. To open the door the servo reels in the string, lifting the door. To close the door the servo lets the string back out and the weight of the door allows it to close. You can see the mock-up I created in the Figure 2a (Front) and Figure 2b (Back). This allowed me to test their existing code and devise a solution using parts they already had in the BASIC Stamp kit. Afterward, I design the schematic shown in Figure 2c.

It was clear that given the mechanical aspects of this design that traditional mechanical limit switches would be difficult to implement. I decided to use optical limit switches, since the plastic could be easily drilled for the optical elements and the door itself would make a great optical interrupter.

Refining the Demo

While I was able to describe to the students how they could implement the servo control and limit switches, it occurred to me that with a better demo I could show more people how to go about controlling a door, so I enlisted in a colleague from Parallax, Matt Gilliland to laser cut me something that could serve as a demo for what I was trying to do.

The results are the project shown in Figure 3. This is a full working demo. There is a schematic and example code for controlling the door with one or both push buttons. The bi-color LED is used for status feedback and is optional. I used to have a video of this demo in action. I was able to recover everything except the video.

This project is essentially a demo for showing how to control a vertical sliding door using push buttons and optical limit switches. The demo includes a continuous rotation servo, two push buttons, a bi-color LED, the sliding door mechanism and two optical limit switches, which are really just an IR LED and a Phototransistor aimed at each other with a gap in between. The top also contains a battery pack power supply and a controller. In this case the Parallax Board of Education with a BASIC Stamp 2 Module installed.

How It Works

Typically in a microcontroller application your code will have a main loop. Within this main loop you are waiting for some indication that the door needs to be opened or closed. In our case this is done by monitoring the push buttons within the main loop. Once a button has been pressed we will jump to a subroutine or function to handle the opening or closing of the door. In Figure 4 you can see a close-up of the control board and how the LED, servo, switches and sensors are connected to it.

The servo and phototransistors are connected to the servo headers. The push buttons and LEDs are connected to the breadboard via solid wire. In Figure 4 the 10K resistors at top left are the pull-ups for the phototransistors, while the 10K resistors just to the right are the pull-ups for the active-low push buttons. The 220Ω resistor is used to limit current to the bi-color LED and the two 1K resistors on the bottom right are used to limit current to the IR LEDs used in the limit switches. See the schematic for more details. A full high-resolution copy of the schematic is included in the ZIP file in the resources below.

The push buttons are pretty straight forward. They’re configured as active-low, which means that normally the I/O pin reads high and when you push the button it goes low. This is the more common configuration. The optical limit switches are essentially two beam-break detectors consisting of an IR LED and a phototransistor. A 10K resistor normally pulls the collector of the phototransistor high. However the IR LED saturates the phototransistor causing it to conduct and be assert the collector low. This works in a similar manner to the beam-break detector in most garage door openers, but at a much closer range.

In Figure 5a you can see the IR LED that makes up one half of the upper limit switch (red arrows). The IR LED is soldered to a piece of proto board and mounted to the unit in such a way that when the door is fully open, its light is blocked from passing through to the other side. When the door is not fully open, light passes through. Note that the IR LED is always on when the circuit has power.

In Figure 5b you can see the phototransistor that makes up the other half of this optical limit switch. When the light from the IR LED is blocked from the phototransistor (when the door is fully open), it does not conduct, and is pulled high via a 10K resistor. However when the IR light does pass through, the phototransistor conducts bringing the I/O pin (P15) low. Note that the acrylic used in this demo is semi-translucent, so black ABS was used for the door to fully block the IR light.

By monitoring the switches, we know when the door is to be opened / closed. By monitoring the limit switches we know when the door is fully opened / closed. By changing the states of the I/O pins the status LED is connected to, we can turn the LED Green / Red / Off.

Bill of Materials

(1) Board of Education Full Kit USB
(1) Li-Ion Power Pack Full Kit
(1) Continuous Rotation Servo
(1) Bi-Color T1-3/4 LED
(2) Infrared T1-3/4 LED
(2) 850NM T1-3/4 Phototransistor
(1) 220Ω, 1/4W, 5% Carbon Film Resistor
(2) 1K, 1/4W, 5% Carbon Film Resistor
(4) 10K, 1/4W, 5% Carbon Film Resistor
(2) N.O. Pushbuttons

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 blocked). 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, 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 it hasn’t yet reached its target position.

Since we’re using a servo to move the door, it must receive a pulse every 20 ms. This pulse needs to be the right width to move the servo in the direction required to open / close the door as needed and will usually be 1 ms or 2 ms. 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.


Servos and optical limits switches both come with their own caveats. Servos tend to twitch when powered up and so there is often some small amount of movement. Normally this wouldn’t be such a concern, however if your door is already at its limit and a power-on twitch tries to move it past that you can put stress on the mechanical parts. Fortunately hobby servos don’t tend to have a lot of torque.

Optical limit switches seem like a fancy way of solving an issue without any moving parts to wear out, but I did run into something to be aware of. While filming the video of the door on the demo opening and closing an interesting thing happened. The door tried to open past the limit switch and fractured the laser-cut frame before the fishing line broke relieving stress on the door. What happened was that the bright lights I use when recording video washed out the phototransistors on the limit switches causing them to think the beam had never been broken.

One thing to try is to tweak the phototransistor circuit so that more light is required and to shield the LED and phototransistor from ambient light so that only a direct beam from the LED would saturate the phototransistor. In most projects this would work. But when using translucent materials this may not work as well.

For added safety you can also change the open / closed state of things by making it so that the phototransistor is blocked when the door is in transition and saturated at the limit points. That way if one or both of the sensor gets washed out for some reason the door can be set not to move. This also includes the possibility of using a single optical limit switch for both positions in some applications.

Final Thoughts

If you need more torque or speed, a servo may not be your best option. Likewise, if light is an issue an optical limit switch may not be the best solution for you. Using two buttons was for both for completeness and simplicity. The code and hardware could easily be designed to work with just one button or 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 two parts.


Door Control – Part 2 [BS2]

Door Control – Part 3 [BS2]

Discuss this tutorial on Savage///Chats

This project was published in the September 2016 issue of Servo Magazine.

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


Leave a Reply