Di Digi gital and Analog og Si Sign gnals 01219335 Data - - PowerPoint PPT Presentation

di digi gital and analog og si sign gnals
SMART_READER_LITE
LIVE PREVIEW

Di Digi gital and Analog og Si Sign gnals 01219335 Data - - PowerPoint PPT Presentation

Di Digi gital and Analog og Si Sign gnals 01219335 Data Acquisition and Integration Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Ka Kasetsart Unive versity Revised 2020-08-26 Ou Outline Digital


slide-1
SLIDE 1

Di Digi gital and Analog

  • g Si

Sign gnals

Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Ka Kasetsart Unive versity

01219335 Data Acquisition and Integration

Revised 2020-08-26

slide-2
SLIDE 2

2

Ou Outline

  • Digital and analog data
  • Digital and analog signals
  • Microcontroller’s input and output pins
  • Digital/analog input and output
slide-3
SLIDE 3

3

Di Digi gital vs. . Analog

  • g Da

Data

  • Digital data
  • Data take on discrete values
  • E.g., number of people,
  • switch position
  • Analog data
  • Data take on continuous values
  • E.g., human voice, temperature reading

Cliparts are taken from http://openclipart.org and http://clipart-library.com

slide-4
SLIDE 4

4

Di Digi gital vs. . Analog

  • g Si

Sign gnals

  • Digital signals
  • Have a limited number of

valid values

  • Analog signals
  • have an infinite number of

values in a range

value time value time

To To be measured, data must be be tr transformed ed to to physical signals

slide-5
SLIDE 5

5

Tr Transducers

  • A transducer is a device that converts one form of

energy/signal to another

Transducer

  • ne form of signal

another form of signal

sound wave electrical signal electrical signal sound wave

slide-6
SLIDE 6

6

Di Digi gital vs. . Analog

  • g Input

Input/Out utput put Pi Pins

  • Digital input pin
  • Detects whether or not there is a presence of voltage greater than

a threshold on the pin

  • Digital output pin
  • Produces 2-level signal, a supply level or 0V, on the pin
  • Analog input pin
  • Detects multiple levels (many more than two) of voltage on the pin
  • Analog output pin
  • Produces multiple levels of signal on the pin
slide-7
SLIDE 7

7

Ou Output vs. . Input Pi Pins

  • An output pin attempts to control

the voltage of the pin to a specified level (e.g., 0V or 3.3V)

  • Allows current to flow out of or in to

the pin

  • An input pin probes the voltage

level of the pin without changing its voltage

  • Does not allow current to flow out of
  • r in to the pin

Microcontroller 3.3V

  • utput

Microcontroller

  • utput

Microcontroller 3.3V

input

Caution: two output pins MUST NOT be connected

slide-8
SLIDE 8

8

Ex Examples o amples of D f Dig igit ital and A al and Analo nalog I/ I/O

Digital Analog Input

  • Push switch
  • Proximity sensor
  • Light sensor
  • Microphone
  • Potentiometer

Output

  • LED (on/off)
  • Buzzer
  • Relay
  • LED (dimmable)
  • Motor
  • Servo

https://www.makerlab-electronics.com

slide-9
SLIDE 9

9

Di Digi gital Ou Output Example

  • LED control (on/off)

Microcontroller 3.3V

  • utput

Microcontroller

  • utput

Turning on LED by setting the pin’s logic to 1 Turning on LED by setting the pin’s logic to 0 Pin’s output logic Pin’s voltage 0V 1 3.3V

slide-10
SLIDE 10

10

LED LED C Control l – Sc Schematic ic

  • Red LED connected to IO2; green LED connected to IO12

KidBright's Schematic provided by INEX

slide-11
SLIDE 11

11

LED LED C Control: P l: Python C Code

from machine import Pin # make pin 2 and 12 OUTPUT, # refer to them as 'led_red' and 'led_green' led_red = Pin(2, Pin.OUT) led_green = Pin(12, Pin.OUT) # set pin 2’s voltage to 0V (Red LED on) led_red.value(0) # set pin 12’s voltage to 0V (Green LED on) led_green.value(0)

slide-12
SLIDE 12

12

Di Digi gital Input Example

  • Switch input circuit
  • A pull-up resistor is required

to avoid noise being picked up when the pin is floating

Microcontroller

digital input

Pin’s voltage Pin’s input logic Switch is released 3.3V 1 Switch is pressed 0V

3.3V internal pull-up resistor

slide-13
SLIDE 13

13

Sw Switch Status – Sc Schematic ic

  • Two push-buttons are connected

to IO14 and IO16 pins

  • When a button is pushed, its status

reading is 0

  • When a button is released, its

status reading is 1

KidBright's Schematic provided by INEX

slide-14
SLIDE 14

14

Sw Switch Status: Python Code

from machine import Pin from time import sleep # make pin 16 an INPUT pin with pull-up enabled; # refer to it as 'sw1' sw1 = Pin(16, Pin.IN, Pin.PULL_UP) while True: # read and print out the switch value print("S1 value =", sw1.value()) # delay a little to slow down screen output sleep(0.1)

slide-15
SLIDE 15

15

Ex Exer ercise 3. ise 3.1: 1: S Swit itch-Co Controlled LED

  • Write a MicroPython script to control the LED by pressing

and releasing switch

  • S1 is pressed: red LED is turned on
  • S1 is released: red LED is turned off

(Image by Hebi B. from Pixabay)

slide-16
SLIDE 16

16

Ex Exer ercise 3. ise 3.2: 2: S Swit itch-To Toggled LED

  • Write a MicroPython script to toggle the red LED state by

pressing and releasing switch

  • Initially, the red LED is off
  • S1 is pressed: LED is turned on
  • S1 is released and then pressed again: LED is turned off

(Image by Francis Ray from Pixabay)

slide-17
SLIDE 17

17

Re Reading Digital and Analog Signals

2-level digital data 16-level digital data

1 15

analog signal

0V 3.3V

digital signal

0V 3.3V 1

slide-18
SLIDE 18

18

An Analog-to to-Di Digi gital Con

  • nverter (ADC

DC)

  • Converts continuous values of data to a finite number of

discrete values

  • The higher the bit resolution, the more accurate the

conversion

3-bit resolution (8 levels) 4-bit resolution (16 levels) 8-bit resolution (256 levels)

slide-19
SLIDE 19

19

ES ESP32’s Analog Inputs

  • Up to 12-bit resolution (0 – 4095)

https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

slide-20
SLIDE 20

20

AD ADC Reading Code

  • Create an ADC input object using ADC class in the machine

module

  • Input pin must support analog reading (see previous page)
  • Use ADC.read() method to read input levels (0 – 4095)

from machine import Pin, ADC from time import sleep ain = ADC(Pin(34)) while True: print(ain.read()) sleep(0.1)

slide-21
SLIDE 21

21

ES ESP32’s ADC Sensitivity

Attenuation Measurement range 0dB (default) 0V – 1.1V 2.5dB 0V – 1.5V 6dB 0V – 2.2V 11dB 0V – 3.9V Use ADC.atten() method to control the attenuation

slide-22
SLIDE 22

22

Ki KidBri dBright's s Light Senso ensor

  • Consists of a Light-

Dependent Resistor (LDR) and a fixed-value resistor

  • Attached to SENSOR_VP

pin (i.e., IO36)

KidBright's Schematic provided by INEX

slide-23
SLIDE 23

23

Lig Light Se Sensin ing: P : Python C Code

  • Configure Pin 36 (SENSOR_VP) as ADC input

from machine import Pin, ADC from time import sleep ldr = ADC(Pin(36)) while True: print(ldr.read()) sleep(0.1)

slide-24
SLIDE 24

24

Microcontroller

Input Input Vo Voltage Ca Calculation

analog input

Light-Dependent Resistor (LDR)

3.3V

R1 VA

𝑊

! = 3.3×

𝑆"#$ 𝑆% + 𝑆"#$

By Ohm’s Law,

slide-25
SLIDE 25

25

Ex Example ample

  • If the previous code displays the value of 1000 from LDR

reading, what is the resistance of the LDR?

slide-26
SLIDE 26

26

An Analog Output Pins

  • Certain microcontrollers, including ESP32, provide DAC

(Digital-to-Analog Converter) pins to produce multi-level

  • utput signals
  • Most low-end microcontrollers do not provide DAC pins
  • Pulse-width modulation (PWM) is used to produce “analog” signals
  • Servos expect PWM input to control speed and rotation

DAC pin PWM pin

slide-27
SLIDE 27

27

Pu Pulse-Wi Width th Modulati tion (PWM WM)

  • PWM utilizes duty cycle control to simulate analog output

Analog data PWM output 25% duty cycle 50% duty cycle 75% duty cycle

https://en.wikipedia.org/wiki/Pulse-width_modulation

slide-28
SLIDE 28

28

PW PWM Ou Output Example

  • Dimmable LED control

KidBright's Schematic provided by INEX

slide-29
SLIDE 29

29

PW PWM Ou Output: : Cod

  • de
  • Create a PWM output on Pin 2 (connected to the red LED)

with frequency of 5000 Hz

  • Gradually change duty cycle from 0 to 1023 and vice versa

from machine import Pin, PWM from time import sleep led_red = PWM(Pin(2),freq=5000) while True: for d in range(0,1024): led_red.duty(d) sleep(0.001) for d in range(1023,-1,-1): led_red.duty(d) sleep(0.001)

slide-30
SLIDE 30

30

Ex Exer ercise 3. ise 3.3: 3: Lig Light-Co Controlled LED

  • Write a MicroPython script to adjust LED brightness based
  • n the surrounding light
  • LED brightness increases when the surrounding gets bright
  • LED brightness decreases when the surrounding gets dark

(Images from http://clipart-library.com )

slide-31
SLIDE 31

31

Co Conclusi sion

  • Measurement quantities must be converted into voltage

signals using transducers to be read by microcontroller

  • Microcontroller can be configured to read input signals or

produce output signals

  • Measurement data may be digital (discrete) or analog

(continuous)

  • Analog signals must be converted into multi-level digital

values using Analog-to-Digital Converter (ADC)

  • Analog output can be simulated on a digital pin using

Pulse-Width Modulation (PWM)