User Tools

Site Tools

Switch Input

Let’s say you are developing some kind of machine controlled by a Cubloc.
The first thing you need is a user interface. Our task today is to build a
machine that will receive input from a switch and process it to perform a
task.

We will make a “start” and “stop” button that will turn a lamp on and off.

As you can see above, ports P0 and P4 will be connected to a pull-down
resistor (resistor attached to ground). The CB220 will read these switches
as logic-low or “off” when the switch is not pressed. To find out if these
switches are pressed or unpressed, we can use Cubloc BASIC command In.

When the switch is pressed, a “bouncing” effect occurs from the switch’s
mechanical spring.

The above picture shows how bouncing can confuse Cubloc controller with
alternating high and low signal levels. To get rid of this bouncing effect, a
capacitor and resistor can be added to filter it out.

A simpler method is to use the KeyInH command instead of In which will
remove the bouncing effect through software.

The 2nd parameter of KeyInH(0, 20) a delay before reading the input so
the signal bounce has time to settle. This delay is called the debouncing
time. The software will wait for 20ms before reading the input.

In industrial environments, there can be a lot of electromagnetic noise
which can affect switch signals. To prevent this noise from affecting the
circuit, a circuit diagram similar to one shown below can be used. Using a
photocoupler, the user is able to raise the voltage and minimize the effect
the noise has on the switch input.