01 - Digital - IO - LQ

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

25/03/23

Digital Input/Output

.DIEM
INTERNET OF THINGS

• Most of the pins on the board are General Purpose Input Output
(GPIO) pins

I/O pins on • i.e. they do not have a pre-assigned function, but can be used
for different purposes, if suitably configured
• While a pin usually have different possible functions, you cannot
theBoard have all the functions on all the pins
• You need a map of the pins of your board to know what you
can do with each pin

1
25/03/23

• On the PIN map you can read:

I/O pins on • The number of the pin


• GPIO pins can be used as Digital Input or Digital Output; the
map shows also additional functions:
theboard • ADC = Analog/Digital Converter to convert an analog voltage
to a numeric value

2
25/03/23

DigitalOutputs
• Digital output pins translate a logical value (1 or 0) to one of
two voltage levels: 0V for 0, and 3.3V for 1
• This voltage level can be used as an input for another device

• IMPORTANT! IMPORTANT! IMPORTANT! IMPORTANT!


• You should never, never, never get out of a digital output
pin a (positive or negative) current greater than a few mA
(say 10mA to be safe)
• If you do, you can permanently damage the MCU!
• So you may need to add resistors for limiting the current,
and you have to compute the appropriate resistor value

Light Emitting Diodes(LEDs)


• First thing first, we have to know the “Thing” we want to connect to our
board
• A LED has two terminals, Anode (+) and Cathode (-)
• Current can only flow from the anode to the cathode (from + to -); in the
other direction, the LED will act as an open circuit
• When a current passes through the LED, it emits light, with an intensity
related to the current
• In a circuit scheme, a LED is represented with this symbol:

3
25/03/23

Light Emitting Diodes(LEDs)


• How to find the Anode and Cathode?

Light Emitting Diodes(LEDs)


• When conducting, the voltage across the led is almost independent of the
current. This voltage is called the forward voltage (VF) and it is a characteristic
that you can read on the datasheet of the LED:

4
25/03/23

Light Emitting Diodes(LEDs)


• The current passing through the LED depends on other components of the
circuits, but there is a limit over which the LED may overheat and be
permanently damaged
• For small LEDs, this limit is typically 20mA

How to connect a LED to your board?

Output pin

10

5
25/03/23

How to connect a LED to your board?

Output pin

When the output pin is HIGH, there is no limitation


to the current passing through the LED (and through
the pin)!!!!! NEVER DO THAT!!!!!

11

How to connect a LED to your board?


We need to add a current-limiting resistor

Output pin

12

6
25/03/23

How to connect a LED to your board?


How do we choose the resistance R?
VR
R Kirchhoff and Ohm come to
our rescue!
Output pin

VHIGH VF

Ohm’s law: VR = R*I


Kirchoff’s: VR = VHIGH – VF
I*R=(VHIGH-VF)
R=(VHIGH-VF)/I

13

How to connect a LED to your board?


How do we choose the resistance R?
VR
If:
R VHIGH = 3.3V
VF = 2.2V
Output pin and we want:
I=10mA
VHIGH VF
R=110 Ω

If we don’t have 110Ω resistors, we choose an


approximation (e.g. a 100Ω, if we have it)

14

7
25/03/23

How to connect a LED to your board?

R
By the way, if we do not know VF,
we can connect the LED to a
Voltage source
power source using a “safe”
resistor (e.g. 1kΩ), and measure
VF with our multimeter

15

More current /power


• If you need to turn on and off a device that requires more
current and more power than a digital output pin can
provide, you need to use some other device/circuit:
• A voltage buffer
• A relay

To power these devices you


usually an external power source
(e.g. a battery); drawing to much power from
the +5V or +3.3V pins on the board can
cause problems (e.g. unexpected resets)

16

8
25/03/23

Hands on
session
Handling digital outputs on ESP32

17

• Digital input pins translate a voltage level to


one of two logical values: 0V-0.8V to 0, and
2.2V-5V to 1 (in-between voltage levels are
ambiguous and should be avoided)
• This voltage level can come from the output
Digital Inputs of another digital device, or from some
sensor or circuit generating the appropriate
voltage as its output
• The current passing through a digital input
pin is practically 0 (it is on the order of nano
Amperes)

18

9
25/03/23

Connecting a push-button asa digital input


• A push-button acts as a switch that closes the circuit between its terminals
when it is pushed, and opens the circuits when released
• actually this is the behavior of a Normally-Open (NO) push-button (the most-
common type); you may also find Normally-Closed (NC) buttons where the logic is
reversed

19

Connecting a push-button as a digital input

… is it right?

20

10
25/03/23

Connecting a push-button asa digital input

This circuit won’t work:


• when the push-button is pressed,
the input pin will actually read a
HIGH voltage level
• BUT when the button is released,
the input pin is connected to an
open circuit: so the voltage level
will be unpredictable

21

Connecting a push-button as a digital input

Solution: we add a resistor


connecting the input pin to
ground.

22

11
25/03/23

Connecting a push-button asa digital input

Solution: we add a resistor


connecting the input pin to
ground.
• When the button is open, the
resistor pulls the input pin
voltage to ground (0V)
• So it is called a pull-down resistor

23

Connecting a push-button asa digital input


Alternative scheme
• Now, when the button is pressed,
the input pin receives a LOW
voltage
• When the button is released, the
resistor pulls the input pin
voltage to the HIGH level
• It is a pull-up resistor
• Note that the logic value of
the input is now reversed!
(LOW when pressed, HIGH
when released)

24

12
25/03/23

Connecting a push-button asa digital input

What value should we use for the pull-up or pull-down resistors?


• A small resistor means a larger current is drawn from Vcc, and a larger
power is wasted heating the resistor, when the button is closed
• A large resistor means a longer transient when the input voltage has to
pass from the button-closed state to the other state
• Typically, a 20kΩ resistor is a good compromise

25

Built-in pull-up andpull-down

Since the need to use pull-up or pull-down resistors is very common, the MCU
has internal, built-in resistors that you can activate when you set the working
mode of the pin

internal

internal

26

13
25/03/23

The user button


on theboard
• The devkit-c has a user button
connected to the digital pin D0
• User button is configured as
PULLUP

27

A physicalpush-button
• The most commonly available push-buttons have actually 4
pins
• A is always connected to D, and B is always connected to C
• When the button is pressed, all four pins are connected together

28

14
25/03/23

Hands on
session
Handling digital inputs on ESP32

29

Introducing interrupts
ESP32 provides up to 32 interrupt slots for each core. Each
interrupt has a certain priority level and can be classified into
two types.

• Hardware interrupts – These occur in response to an


external event. For example, a GPIO interrupt (when a key is
pressed) or a touch interrupt (when touch is detected).

• Software Interrupt – These occur in response to a software


instruction. For example, a simple timer interrupt or a
watchdog timer interrupt (when the timer times out).

30

15
25/03/23

Introducing • When an interrupt happens, the processor stops the


execution of the main program to execute a task, and then
interrupts gets back to the main program.

31

ESP32 GPIO Interrupt


• In ESP32 we can define an interrupt service routine function that will be called when the
GPIO pin changes its logic level.
• You can use all GPIOs as interrupts, except GPIO 6 to GPIO 11.
• The interrupt handling function should be as simple as possible, so the processor gets
back to the execution of the main program quickly.
• In MicroPython, the interrupt handling function should accept a parameter of type Pin.
This parameter is returned to the callback function and it refers to the GPIO that caused
the interrupt.

32

16
25/03/23

Hands on
session

Interrupt on button press

33

Most push buttons bounce: when you push


and release them, the mechanical parts
oscillate and they produce several rising
and falling edges.
Therefore, we should eliminate events
Bouncing that are too close (less than 200ms is OK
with most cheap push buttons).
buttons We can address this wrong behavior in our
code.
Look at the documentation of the time (or
utime) module to find the relevant
functions.

34

17

You might also like