LECTURE - 5 (IOTPP) Student Copy Final
LECTURE - 5 (IOTPP) Student Copy Final
(CSE 4110)
(LECTURE – 5)
What is a Switch?
A switch is a device which is used to make or break an electric circuit automatically or manually.
Switches are a part of the control system and without it, control operation cannot be achieved. A switch can
perform two functions, namely fully ON (by closing its contacts) or fully OFF (by opening its contacts).
Working of an Electrical Switch
The controlling operation of a switch can be defined by its “Pole” and “Throw“. A Pole represents the
number of operations controlled by a single switch. The Throw indicates the number of contacts in a switch.
For example, the NO (Normally Open) and NC (Normally Closed) are Single Throw which is used to control
the circuit by making/breaking contacts of the switch.
The intermediate and changeover switches are Double Throw which is used to control the two way operation
of the circuit by closing/opening contacts of the switches which is used to divert the flow of current from
one circuit to another.
A Single-Pole-Single Throw (SPST) switch is known for controlling a single circuit (e.g. ON/OFF) per
operation. This way, the number of poles and throws are used to represent the controlling process of a
switch.
Types of Switches
Mechanical (activated physically, by moving, pressing,
to control a circuit)
Electro-Mechanical
Latched Switches, maintain the contact until it is forced to the other position.
SPST, also known as 1P1T DPST also known as 2P1T SPDT also known as 1P2T 3PST also known as 3P1T
Single pole, single throw Double pole, single throw Single pole, double throw Three pole, single throw
Mechanical Switch
Toggle Switch
Toggle switches are latched type of switches which are actuated by
a lever angled in one or more directions.
This switch is stable in state and remains in that state unless or
until the lever is pushed in another direction.
Most of all household applications (such as lighting control
switches) have toggle switch and it can fall into any category as
mentioned above e.g. SPST, DPDT, DPST, DPDT etc.
They are available for high current applications up to 30+ amperes
and can be used for small current switching operations.
Slide Switch
A slide switch uses a slider as actuator which slides back and
forth to make and break the contacts.
But when a GPIO Pin of the Raspberry Pi is declared as Input, it must be ‘tied’ to High or Low or else it
is called as a Floating Input Pin. A Floating Input is a pin which is defined as input and left as it is.
Any Digital Input Pin is very sensitive and catches even the slightest changes and will pick up the
In order to avoid this, a Digital Input Pin must be tied to VCC or GND with the help of Pull-up or Pull-
down resistors.
Pull-Up and Pull-Down Resistor
Pull-up Resistors If we connect the push button switch directly to Raspberry Pi
to get digital input, It means switch one pin is connected to
Ground or 5v Vcc and another pin connected to Raspberry Pi
digital pin. In this case, the Raspberry Pi is read unstable input
from the push button.
In this case, the push button output is Low(0) when the button
is open, and the output of the push button is High(1) when the
button is pressed.
2. Chattering Phenomenon
It should be considered in only some application that needs to detect exactly number of the pressing.
Symptom: Button is pressed once, but Raspberry Pi code detects several times.
Cause : Due to mechanical and physical issues, the state of button (or switch) is quickly toggled
between LOW and HIGH several times.
Solution: Debounce
Trouble-shooting Pull-Up and Pull-Down Resistor
What is Switch Bouncing?
When we press a pushbutton or toggle switch or a
micro switch, two metal parts come into contact to short
the supply. But they don’t connect instantly but the
metal parts connect and disconnect several times
before the actual stable connection is made. The same
thing happens while releasing the button. This results
the false triggering or multiple triggering like the button
is pressed multiple times. Its like falling a bouncing ball
from a height and it keeps bouncing on the surface,
until it comes at rest.
Switch bouncing is the non-ideal behavior of any switch which generates multiple transitions of a single
input. Switch bouncing is not a major problem when we deal with the power circuits, but it cause problems
while we are dealing with the logic or digital circuits. Hence, to remove the bouncing from the
circuit Switch Debouncing Circuit is used.
Trouble-shooting Pull-Up and Pull-Down Resistor
Switch De-Bouncing
There are three commonly used methods to prevent the circuit
from switch bouncing.
Hardware Debouncing (In the hardware debouncing technique
we use an S-R flip flop to prevent the circuit from switch
bounces. This is the best debouncing method among all.)
RC Debouncing (The R-C is defined by its name only, the
circuit used a RC network for the protection from switch
bounce. The capacitor in the circuit filter the instant changes in
the switching signal. When the switch is in open state the
voltage across the capacitor remain zero. Initially, when the
switch is open the capacitor charge through the R1 and R2
resistor.)
Switch Debouncing IC (Some of the debouncing ICs are
MAX6816, MC14490, and LS118.)
Electrical & Electronic Switches
Electrical and mostly electronic switches are solid-state devices based on semiconductor materials
with fast response, accurate operation and small in size as compared to mechanical and
electromechanical switches. The solid state switches are based on the basic components such as
Electronic switches have no physical contacts or moving parts and can be automatically operated by
electric signals or programmed circuits like microcontroller or microprocessor. They are precise in
operation for stability and reliability of the system without noise of switching operation.
They are used in many modern applications such as variable frequency drive (VFD) drives for motors,
HVAC & in industrial, automation, automotive, aerospace, robotic and many more commercial
applications.
Electrical & Electronic Switches
Diode as a Switch
Forward Bias: If positive signal applied to the Anode terminal and cathode is negative, the diode is
Reverse Bias: If the negative signal applied to the Anode and cathode is positive, the diode is in
resistor, the main controller may receive a ‘noisy’ signal which can trigger even when you’re not
resistor, the main controller may receive a ‘noisy’ (floating) signal which can trigger even when you’re
mass storage volume. You can then just drag and drop a UF2 file onto the board. Sometimes it is not
convenient to keep unplugging the micro USB cable every time you want to upload a UF2 onto the Pico.
Transistor is a semiconductor device that controls a large current through a small current. Its function is
to amplify weak signals into larger amplitude signals, and can also be used as a non-contact switch.
Some components use high-current (such as Buzzer). If the power is directly supplied from the GPIO of
the microcontroller, the power may be insufficient or the microcontroller may be damaged. Then, the
transistor has played a “dam” role here. Transistor receives the weak electrical signal from the GPIO pin
to control the turn-on and turn-off of the large current (from VCC to GND). In this way, high-current
components can be driven and the microcontroller can be protected.
When a High level signal goes through an NPN transistor, it is energized. But a PNP one needs a Low
level signal to manage it. Both types of transistor are frequently used for contactless switches.
The base is the gate controller device for the larger electrical supply.
In the NPN transistor, the collector is the larger electrical supply and the emitter is the outlet for that
supply, the PNP transistor is just the opposite.
Controlling an LED and a transistor using a Push Button
When we press the button, Pico will send a high-level signal to the transistor (i.e., LED or BUZZER will be
ON) ; when we release it, it will send a low-level signal (i.e., LED or BUZZER will be OFF).
Application of Transistor:
1) as an Amplifier
2) as a switch
A basic three-light traffic light system
Traffic lights are used to direct traffic operation and are generally composed of red, green, and yellow lights.
doesn’t interrupt the traffic lights. To make that work, you’ll need a new library: _thread.
Thread 1
Multithreading Concept
(Task – 1)
Thread 2
(Task – 2)
Busy
MOM
Thread 3
(Task – 3)
Multiple action without threading
done in : 4.064005613327026
Hah... I am done with all my work
now! Thread 2
(Task – 2)
doesn’t interrupt the traffic lights. To make that work, you’ll need a new library: _thread.
The traffic light will switch in the order of red for 5 seconds,
yellow for 2 seconds, green for 5 seconds, and yellow for 2
seconds. If we (pedestrians) press the button, the red LED will be
extended to 15 seconds, which will give us more time to cross
the road.
A single-player game
Objective:
When the program starts, the LED will turn off within 5 to 10 seconds. You need to press the button as fast
as possible, and the program will tell you what your reaction time is.
Interrupt Requests, or IRQs
EXAMPLE:
You are reading a book page by page, as if a program is executing a thread. At this time, someone came
to you to ask a question and interrupted your reading. Then the person is executing the interrupt request:
asking you to stop what you are doing, answer his questions, and then let you return to reading the book
after the end.
MicroPython interrupt request also works in the same way, it allows certain operations to interrupt the
main program. Here, a callback function (button_press) is first defined, which is
called an interrupt handler. It will be executed when an interrupt
request is triggered. Then, set up an interrupt request in the main
program, it contains two parts: trigger and handler.
In this program, the trigger is IRQ_RISING, which means that the
value of the pin rises from low level to high level (That is, pressing
the button).
handler is the callback function button_press we defined before.
In this example, you will find a
statement button.irq(handler=None) in the callback function, which is
equivalent to canceling the interrupt.
Interrupt Requests, or IRQs
The job of the interrupt handler is to service the device and stop it from interrupting. Once the handler
returns, the CPU resumes what it was doing before the interrupt occurred.
The urandom library is loaded here. Use the urandom.uniform(5,10) function to generate a random
number, the ‘uniform’ part referring to a uniform distribution between those two numbers.
The utime.ticks_ms() function will output the number of milliseconds that have passed since
the utime library started counting and store it in the variable timer_light_off.
utime.ticks_diff() is used to output the time difference between two time nodes. The two time nodes in
this function are utime.ticks_ms(), the current program time (press the button) and the reference time
(light off) stored in the variable timer_light_off.
These two functions are usually used together to calculate the execution time of the program. Here we
use it to calculate the time from when the light turns off to when the button is pressed.
Playing with your friends will be more fun, why not add buttons and see who can press the buttons the
fastest?
A two-player game
Simple Reaction Timing Game (Two Player)
A two-player game
Objective:
Pushbutton at GP1 is named Faster and pressing this button flashes the LED faster. Similarly,
pushbutton at GP2 is named Slower, and pressing this button flashes the LED slower.
7 LEDs are positioned in the form of a circle. Each LED is assumed to represent a day of the week.
Pressing a button generates a random number between 1 and 7 and lights up only one of the LEDs. The
day name corresponding to this LED is assumed to be your lucky day of the week.
Lucky Day of the Week
Lucky Day of the Week
Lucky Day of the Week