SwitchesWithESP32

Page Actions

Lecture 3: Switches

CSC 321: Embedded Sysytem

First Semester 2020/2021

Switches

A Switch is a device which is designed to interrupt the current flow of electrons in a circuit.

Switches are essentially binary devices: they are either completely on (\x93closed\x94) or completely off (\x93open\x94).

A Switch simply can make or break an electrical circuit.

Every electrical and electronics application uses at least one switch to perform ON and OFF operation of the device.

Switches are a part of the control system and without it, control operation cannot be achieved.

When the contacts of a switch are closed, the switch creates a closed path for the current to flow and hence load consumes the power from source.

Types of Switches

\x95 Switches exist in various incarnation.

\x95 A Switch object is used to control a push-button switch.

\x95 There are the simple mechanical switches: on/off or push button switches

\x95 There are also switches than turn on or off with various external conditions:

\x95 Temperature

\x95 infra red radiation (e.g. the Passive Infrared Sensor or PIR sensor)

\x95 Hall switches detecting magnetic field

\x95 microphone switches which turn on when a certain noise level is detected

Types of Switches

Toggle Switches

Toggle Switch

Toggle switches are actuated by a lever angled in one of two or more positions.

The common light switch used in household wiring is an example of a toggle switch.

Pushbutton Switches

Pushbutton Switch

Pushbutton switches are two-position devices actuated with a button that is pressed and released.

Most pushbutton switches have an internal spring mechanism returning the button to its \x93out,\x94

or \x93unpressed,\x94 position, for momentary operation.

Types of Switches

Selector Switches

Selector Switch

Selector switches are actuated with a rotary knob or lever of some sort to select one of two or more positions.

Joystick Switches

Joystick Switch

A joystick switch is actuated by a lever free to move in more than one axis of motion.

One or more of several switch contact mechanisms are actuated depending on

which way the lever is pushed, and sometimes by how far it is pushed.

Pressure Switches

Pressure Switch

Gas or liquid pressure can be used to actuate a switch mechanism if that pressure is applied to a piston,

diaphragm, or bellows, which converts pressure to mechanical force.

Temperature Switches

Temperature Switch

An inexpensive temperature-sensing mechanism is the \x93bimetallic strip:\x94 a thin strip of two metals,

joined back-to-back, each metal having a different rate of thermal expansion.

The Push Button & PIR

pushbutton.png pir.png

Digital Inputs

When you try to use a microcontroller to detect activity in the physical world,

the simplest activity you can perceive is whether an activity is True or False.

  • Is the audience in the room or outside?
  • Are they touching the table?
  • Is the door open or closed?
In these cases, you can use the digital input or the switch to determine the state.

Schematic of a Digital Input to a microcontroller

From the schematic diagram current has two directions it can go to ground:
through the resistor or through the microcontroller.

When the switch is closed, the current will follow the path of least resistance, to the microcontroller pin,
and the microcontroller can then read the voltage. The microcontroller pin will then read as high voltage or HIGH.

When the switch is open, the resistor connects the digital input to ground, so that it reads as zero voltage, or LOW.

\x95 A digital input is a board pin which can read if it sees a high (on) or low (off) logic level connected to it.

\x95 For example if you connect a button to a digital input you can detect when it's pressed and released.

\x95 Also if you connect a PIR(passive infrared sensor) to a digital input, it can detect the infrared (IR) light radiating from objects in its field of view.

\x95 MicroPython makes it just as easy to use a pin digital inputs as it does outputs.

The Push Button

\x95 The push button switch is usually used to turn on and off the control circuit, and it is a kind of control switch appliance that is widely used.

\x95 This is very similar to switching a LED on or off.

\x95 A single GPIO line is used.

\x95 The pin in programmed as a digital input and we can add a pullup or pulldown resistor:

_PB_PIN = 22 
pushButton = Pin(_PB_PIN, Pin.IN, Pin.PULL_UP)

\x95 Once the pushButton pin is defined we can get its current state with:

state = pushButton.value()

Pull-up Resistors

\x95 Pull-up resistors are very common when using microcontrollers (MCUs) or any digital logic device.`

\x95 The value of the pull-up resistor needs to be chosen to satisfy two conditions:

\x95 When the button is pressed, the input pin is pulled low. The value of resistor R1 controls how much current you want to flow from

VCC, through the button, and then to ground.

\x95 When the button is not pressed, the input pin is pulled high. The value of the pull-up resistor controls the voltage on the input pin.

pullup.png

Interrupts

\x95 Interrupts are useful for making things happen automatically in microcontroller programs

\x95 With interrupts you don\x92t need to constantly check the current pin value.

\x95 When a change is detected, an event is triggered (a function is called).

\x95 When an interrupt happens, the processor stops the execution of the main program to execute a task,

and then gets back to the main program

\x95 This is especially useful to trigger an action whenever motion is detected or

whenever a pushbutton is pressed without the need for constantly checking its state.

Interrupt Method

The irq() method accepts the following arguments:
  • trigger: this defines the trigger mode. There are 3 different conditions:
  • Pin.IRQ_FALLING: to trigger the interrupt whenever the pin goes from HIGH to LOW;
  • Pin.IRQ_RISING: to trigger the interrupt whenever the pin goes from LOW to HIGH.
  • 3 or Pin.IRQ_FALLING | Pin.IRQ_RISING: to trigger the interrupt in both edges (this means, when any change is detected)
  • handler: this is a function that will be called when an interrupt is detected, in this case the handle_interrupt() function.
pushButton.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)

Slide 10 of 15

The PIR Sensor

\x95 PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range.

\x95 They are small, inexpensive, low-power, easy to use and don't wear out.

\x95 They are often referred to as PIR, "Passive Infrared", "Pyroelectric", or "IR motion" sensors.

\x95 PIR is an electronic sensor which detects the changes in the infrared light across certain

distance and gives out an electrical signal at its output in response to a detected IR signal.

\x95 It can detect any infrared emitting object(Objects that generate heat) such as human beings or

animals if it is the range of the sensor, or moves away from the range, or moves within the range of the sensor.

\x95 The radiation of the infrared object should be strongest at a wavelength of 9.4μm.

\x95 Infrared in this range will not pass through many types of material that pass visible light such as ordinary window glass and plastic.

\x95 However it will pass through, with some attenuation, material that is opaque to visible light such as germanium and silicon.

pir.png pir_back.png

Slide 11 of 15

Slide 12 of 15

PIR Sensor Features

  • Wide range on input voltage varying from 4.V to 12V (+5V recommended)
  • Output voltage is High/Low (3.3V TTL)
  • Can distinguish between object movement and human movement
  • Has to operating modes - Repeatable(H) and Non- Repeatable(H)
  • Cover distance of about 120\xB0 and 7 meters
  • Low power consumption of 65mA
  • Operating temperature from -20\xB0 to +80\xB0 Celsius

Repeatable(H) mode

In Repeatable(H) mode the output pin Dout will go high (3.3V) when a person is detected within range

It goes low after a particular time (time is set by \x93Off time control\x94 potentiometer).

In this mode the output pin will go high irrespective of whether the person is still present inside the range or has left the area.

The sensitivity can be set using the \x93sensitivity control\x94 potentiometer

Non- Repeatable(L) mode

In \x93I\x94 mode the output pin Dout will go high (3.3V) when a person is detected within range

It will stay high as long as he/she stays within the limit of the Sensors range.

Once the person has left the area the pin will go low after the particular time which can be set using the potentiometer.

The sensitivity can be set using the \x93sensitivity control\x94 potentiometer

Slide 13 of 15

PIR Sensitive Crystal

PIR-Sensor-Image.jpg

There are two important materials present in the sensor:
  • The pyroelectric crystal which can detect the heat signatures from a living organism (humans/animals)
  • The Fresnel lenses which can widen the range of the sensor.
\x95 The dark portion of the metal is where the IR sensitive crystal is housed,

\x95 The sensitive crystal can detect the level of infrared in the surroundings.

\x95 It actually houses two pyroelectic sensors for detecting moving objects.

\x95 If one of the sensitive crystals detects change in infrared (increment or decrement) than the other sensitive crystal,

the output gets triggered.

Slide 14 of 15

PIR to MCU

\x95 A single GPIO line is used.

\x95 The pin in programmed as a digital input and we can add a pullup or pulldown resistor:

_IR_PIN = 16
pir_sensor = Pin(_PB_PIN, Pin.IN, Pin.PULL_UP)

\x95 Once the pushButton pin is defined we can get its current state with:

state = pir_sensor.value()


Slide 15 of 15