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 and analog data • Digital and analog signals • Microcontroller’s input and output pins • Digital/analog input and output 2
Di Digi gital vs. . Analog og 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 3
Di Digi gital vs. . Analog og Si Sign gnals To be measured, data must be To be tr transformed ed to to physical signals • Digital signals value ◦ Have a limited number of valid values time • Analog signals value ◦ have an infinite number of time values in a range 4
Tr Transducers • A transducer is a device that converts one form of energy/signal to another one form of signal another form of signal Transducer sound wave electrical signal sound wave electrical signal 5
Di Digi gital vs. . Analog og 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 6
Ou Output vs. . Input Pi Pins 3.3V • An output pin attempts to control the voltage of the pin to a output Microcontroller specified level (e.g., 0V or 3.3V) ◦ Allows current to flow out of or in to output the pin Microcontroller • An input pin probes the voltage 3.3V level of the pin without changing its voltage input ◦ Does not allow current to flow out of Microcontroller or in to the pin Caution: two output pins MUST NOT be connected 7
Ex Examples o amples of D f Dig igit ital and A al and Analo nalog I/ I/O Digital Analog • Push switch • Light sensor Input • Proximity sensor • Microphone • Potentiometer • LED (on/off) • LED (dimmable) Output • Buzzer • Motor • Relay • Servo https://www.makerlab-electronics.com 8
Di Digi gital Ou Output Example • LED control (on/off) Pin’s output logic Pin’s voltage 0 0V 1 3.3V Turning on LED by setting output the pin’s logic to 1 Microcontroller 3.3V Turning on LED by setting output the pin’s logic to 0 Microcontroller 9
LED LED C Control l – Sc Schematic ic • Red LED connected to IO2; green LED connected to IO12 KidBright's Schematic provided by INEX 10
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) 11
Digi Di gital Input Example 3.3V • Switch input circuit ◦ A pull-up resistor is required internal to avoid noise being picked up pull-up when the pin is floating resistor digital input Microcontroller Pin’s voltage Pin’s input logic Switch is released 3.3V 1 Switch is pressed 0V 0 12
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 13
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) 14
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) 15
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) 16
Reading Digital and Analog Signals Re 1 3.3V 0V 2-level 0 digital signal digital data 1 3.3V 0 15 0V 16-level analog signal digital data 0 17
An Analog-to to-Di Digi gital Con onverter (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) 18
ESP32’s Analog Inputs ES • Up to 12-bit resolution (0 – 4095) https://randomnerdtutorials.com/esp32-pinout-reference-gpios/ 19
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) 20
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 21
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 22
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) 23
Input Input Vo Voltage Ca Calculation By Ohm’s Law, 3.3V 𝑆 "#$ 𝑊 ! = 3.3× 𝑆 % + 𝑆 "#$ R 1 V A analog input Light-Dependent Resistor (LDR) Microcontroller 24
Ex Example ample • If the previous code displays the value of 1000 from LDR reading, what is the resistance of the LDR? 25
An Analog Output Pins • Certain microcontrollers, including ESP32, provide DAC (Digital-to-Analog Converter) pins to produce multi-level output 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 26
Pu Pulse-Wi Width th Modulati tion (PWM WM) • PWM utilizes duty cycle control to simulate analog output 25% 50% 75% duty cycle duty cycle duty cycle Analog data PWM output https://en.wikipedia.org/wiki/Pulse-width_modulation 27
PW PWM Ou Output Example • Dimmable LED control KidBright's Schematic provided by INEX 28
PW PWM Ou Output: : Cod ode • 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) 29
Ex Exer ercise 3. ise 3.3: 3: Lig Light-Co Controlled LED • Write a MicroPython script to adjust LED brightness based on 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 ) 30
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) 31
Recommend
More recommend