them easy to connect. Each board has ground, power, and
the signal pin, as well as a 10K pull-up resistor.
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. The position of the whisker in relation
to the three-pin header it shorts against can be easily
adjusted. As a note, I borrowed this idea from the BOE-Bot
whiskers which use a similar system to detect when a
whisker is pressed.
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.
The example code is for the BASIC Stamp 2 and can
easily be ported to any other microcontroller. It specifies 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 pushbuttons.
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 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 and red if it is closed.
As discussed, 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 can be
compensated for in the limit switches by
setting them to allow for some overshoot, and
also by using an H-bridge that helps 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
42 SERVO 10.2016
Figure 5. Close-up of lower limit switch.
Figure 6. Complete door assembly and limit switches.
Savage///Circuits You Tube