Professional Documents
Culture Documents
Abyanuddin Salam
POLMAN Bandung
Pin I/O Mikrokontroler Arduino Uno
I/O DIGITAL MIKROKONTROLER
Microcontroller Ports and Pins
The communication channels
through which information flows C
into or out of the
microcontroller
Ex. PORTB
Pins PB0 – PB7
May not be contiguous
Often bi-directional
Port Pin Data Directionality
• Input
• When you want to take information from the external
world (sensors) into the MCU
• Output
• When you want to change the state of something outside
the MCU (turn a motor on or off, etc.)
• Pins default to input direction on power-up or reset
• Your program can set or change the directionality of a
pin at any time
ATmega328
Block Diagram
Input
Output
M68HC11 microcontroller
Digital I/O
• Semua pin pada Arduino, baik pin digital maupun pin analog,
dapat digunakan sebagai pin digital. Digital berarti sinyal yang
dikirimkan/diterima bernilai 1 atau 0, on atau off, HIGH atau LOW,
ada atau tidak ada sinyal.
• Berbeda dengan sinyal analog yang nilainya bersifat kontinyu,
yakni nilai antara 0 dan 1 dipertimbangkan.
• Pin digital berarti pin dapat menerima/mengirim sinyal digital.
Digital I/O
• Secara umum pin pada Arduino dapat dikonfigurasi ke dalam dua mode, yaitu
mode input dan output.
• Mode input berarti mengeset pin agar dapat digunakan untuk menerima
masukan sinyal.
• Mode output berarti mengeset pin agar dapat mengirimkan sinyal.
Setting the Pin Data Direction
• Arduino
• pinMode(pin_no., dir)
• Ex. Make Arduino pin 3 (PD3) an output
• pinMode(3, OUTPUT);
• pinMode(PIN_D3, OUTPUT); // with me106.h
• Note: one pin at a time
• Suppose you wanted Arduino pins 3, 5, and 7 (PD3, PD5, and PD7)
to be outputs?
• Is there a way to make them all outputs at the same time?
• Yes! Answer coming later…
Pin Voltages
• Microcontrollers are fundamentally digital devices.
For digital IO pins:
• Information is ‘coded’ in two discrete states:
• HIGH or LOW (logic: 1 or 0)
• Voltages
• TTL
• 5 V (for HIGH)
• 0 V (for LOW)
• 3.3 V CMOS
• 3.3 V (for HIGH)
• 0 V (for LOW)
Pin Used as an Output
• Turn on an LED, which is connected
to pin Arduino pin 0 (PD0) (note the ATmega328
resistor!)
• What should the data direction be for Arduino
pin 0
pin 0 (PD0)? (PD0)
• pinMode(____, ____);
• Turn on the LED
• digitalWrite(PIN_LED,HIGH);
• Turn off the LED
• digitalWrite(PIN_LED,LOW);
Pins as Inputs and Pull-up Resistors - 1
• Using a switch as a sensor
• Ex. Seat belt sensor ATmega328
• Detect the switch state
• What should the data direction be for Arduino
Arduino pin 3 (PD3)? pin 3
(PD3)
• pinMode(____, ____);
• What will the voltage be on PD3 when
the switch is closed?
• What will the voltage be on PD3 when
the switch is open?
• Indeterminate! SPST
momentary
Pins as Inputs and Pull-up Resistors - 2
• Switch as a sensor, cont.
• Make the voltage on the pin ATmega328
determinate by turning on the pull-up VTG= +5V
resistor for PD3
• Assuming PD3 is an input: 1
• digitalWrite(PIN_SWITCH,HIGH); PD3
turns on the “pull-up” resistor 0
• pinMode(PIN_SWITCH,INPUT_PULLUP);
• What will the voltage on PD3 be when
the switch is open?
• VTG
• What will the voltage on PD3 be when
the switch is closed?
Pins as Inputs and Pull-up Resistors - 3
• Switch as a sensor, cont.
• To turn off the pull-up resistor ATmega328
• Assuming PD3 is an input: VTG= +5V
digitalWrite(PIN_SWITCH,LOW);
turns the “pull-up” resistor off
1
PD3
0
Pins as Inputs and Pull-up Resistors - 4
• Possibility of ‘weak drive’ when
pull-up resistor is turned on ATmega328
• Pin set as an input with a pull-up VTG= +5V
int vadc;
ADC_Init(); //inisialisasi ADC dengan nilai default
vadc=ADC_Read(0); //konversi digital kanal 0
Pulse Width Modulation (PWM)
• Untuk mengkonversi besaran digital menjadi analog diperlukan
suatu converter yang disebut DAC (Digital to Analog Converter).
• Selain menggunakan DAC, terdapat cara lain yaitu dengan
menggunakan PWM.
Pulse Width Modulation (PWM)
• Pulse Width Modulation (PWM) secara umum adalah sebuah cara
memanipulasi lebar sinyal yang dinyatakan dengan pulsa dalam
suatu perioda, untuk mendapatkan tegangan rata-rata yang
berbeda.
• Besarnya tegangan sinyal analog hasil rekonstruksi sebanding
dengan siklus kerja (duty cycle)
• Duty cycle merupakan prosentase periode sinyal high dan periode
sinyal low, prosentase duty cycle akan bebanding lurus dengan
tegangan rata-rata yang dihasilkan. Berikut ilustrasi sinyal PWM.
Pulse Width Modulation (PWM)
Pulse Width Modulation (PWM)
• Sinyal PWM pada umumnya memiliki amplitude dan frekuensi
dasar yang tetap, namun memiliki lebar pulsa yang bervariasi.
Lebar pulsa PWM berbanding lurus dengan amplitude sinyal asli
yang belum termodulasi. Artinya, sinyal PWM memiliki frekuensi
gelombang yang tetap namun duty cycle bervariasi antara 0%
hingga 100%.
• Ton adalah waktu dimana tegangan keluaran berada pada posisi
tinggi (high atau 1) dan, Toff adalah waktu dimana tegangan
keluaran berada pada posisi rendah (atau 0). Ttotal adalah waktu
satu siklus atau penjumlahan antara Ton dengan Toff , biasa
dikenal dengan istilah periode satu gelombang.
• Ttotal = Ton + Toff
Pulse Width Modulation (PWM)
• Siklus
kerja atau duty cycle sebuah gelombang di definisikan
sebagai: