0% found this document useful (0 votes)
94 views43 pages

Arduino Microcontroller I/O Guide

Uploaded by

ANAS MOHD NOOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views43 pages

Arduino Microcontroller I/O Guide

Uploaded by

ANAS MOHD NOOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NMJ21304

-MICROCONTROLLER AND INTERFACES-


BIOMEDICAL ELECTRONIC ENGINEERING PROGRAM
YEAR 2, SEMESTER 2
© Anas Mohd Noor, 2023.

All rights reserved. No part of this document may be reproduced or


transmitted in any forms by any means, electronic, mechanical, or
otherwise, whether now or hereafter devised, including photocopying,
recording, or by any information storage and retrieval system without
express written prior permission from the author
CHAPTER 5: INPUT AND OUTPUT

5.1 Introduction
5.2 Analog Input
5.3 Analog Input with External Reference
5.4 Analog Output
5.5 Digital Input
5.6 Digital Output
5.7 Serial Input
5.8 Serial Output
5.9 PWM Output
5.10 I2C and SPI Output
5.11 Interrupt
5.1 Introduction

 Inputs are used to read data from sensors or other


devices.

 Outputs are used to control actuators or other devices.

 The pins can be configured as inputs, outputs, or a


combination of both.

 These pins are typically configured as digital inputs or


outputs

 Analog input pins can be used to read analog signals,


such as from a potentiometer or a light sensor.

 Analog output pins can be used to output analog


signals, such as PWM signals. These pins are typically
configured as PWM outputs.
5.2 Analog Input

 Analog input on an Arduino board allows the measurement of


voltage levels on a specific pin using built-in analog-to-digital
converter (ADC). Pin Description
Analog Pin 1 Input and to read analog voltage
 To utilize analog input, you need to use the analogRead() function, Analog Pin 2 Input and to read analog voltage
which requires a single argument, the pin number to be read from. Analog Pin 3 Input and to read analog voltage
The function returns a value between 0 and 1023, where 0 Analog Pin 4 Input and to read analog voltage
Analog Pin 5 Input and to read analog voltage
represents 0V and 1023 represents the reference voltage of the
GND Ground pin. Arduino have 3 GND pins
board (commonly 5V). VCC Power source
AREF Analog reference pin
Reset Reset the Arduino board
ICSP In-circuit serial programming header for
external programmer.

10 BIT ADC
BUILT-IN
The resolution of 10 bits means that the ADC can produce a
digital output of 1023 for the maximum input voltage (5V), or
2^10 - 1. This means that the ADC can produce 1024 possible
output values, allowing for a high degree of accuracy in analog
signal measurement.
5.2 Analog Input

Pin Description
Analog Pin 1 Input and to read analog voltage
Analog Pin 2 Input and to read analog voltage
Analog Pin 3 Input and to read analog voltage
Analog Pin 4 Input and to read analog voltage
Analog Pin 5 Input and to read analog voltage
GND Ground pin. Arduino have 3 GND pins
VCC Power source
AREF Analog reference pin
Reset Reset the Arduino board
ICSP In-circuit serial programming header for
external programmer.
5.2 Analog Input (internal ref. value 5.0V)

To use the analog input pins, you need to use the appropriate function in the Arduino programming language. The
analogRead() function is used to read the voltage on an analog pin and return its value as an integer between 0 and 1023.
This integer value is proportional to the voltage applied to the pin, where 0 corresponds to 0 volts and 1023 corresponds
to 5 volts.
5.3 Analog Input With External Reference

To use the analog input pins, you need to use the appropriate function in the Arduino programming language. The
analogRead() function is used to read the voltage on an analog pin and return its value as an integer between 0 and 1023.
This integer value is proportional to the voltage applied to the pin, where 0 corresponds to 0 volts and 1023 corresponds
to 5 volts.
5.4 Analog Output

Arduino boards have the capability to output analog signals. They can output a range of voltages, typically between 0 and 5V,
using the analogWrite() function. The function takes two arguments: the pin number and a value between 0 and 255, where 0
corresponds to 0V and 255 corresponds to 5V

Arduino Pin supported for analog output: 3,5,6,9,10,11 (490 Hz)


490 Hz (pins 5 and 6: 980 Hz)
5.4 Analog Input Syntax

Syntax
analogRead(pin)

Parameters
pin: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6 on MKR boards, A0 to A7 on the
Mini and Nano, A0 to A15 on the Mega).
5.4 Analog Output Syntax

Syntax
analogWrite(pin, value)

Parameters
pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.
5.4 Analog External Reference Syntax
Syntax
analogReference(type)

Parameters
type: which type of reference to use (see list of options in below).

DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
INTERNAL: a built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328P and 2.56 volts on the ATmega32U4
and ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.
5.4 Analog Output Example
5.5 Digital Input / 5.6 Digital Output

In Arduino microcontroller, a digital input is a way to read the state of a digital signal, either high (5V) or low (0V), from a
sensor or other external device

Digital Input/Output (I/O) : Pin 0-13


5.5 Digital Input / 5.6 Output Syntax

Syntax
digitalWrite(pin, value)

Parameters
pin: the Arduino pin number.
value: HIGH or LOW.
5.5 Digital Input / 5.6 Output Syntax

Syntax
digitalRead(pin)

Parameters
pin: the Arduino pin number you want to read
5.5 Digital Input / 5.6 Output Syntax

Syntax
pinMode(pin, mode)

Parameters
pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete description of the functionality.
5.5 Digital Input / 5.6 Digital Output Example
5.7 Serial Input / 5.8 Serial Output

Serial communication is a way to send and receive data between the microcontroller and a computer or other device. The
Arduino board has a built-in serial communication module, which can be used to establish a serial communication link. The
data can be sent and received through the serial port (RX and TX pins) using the Serial library

Library Functions

[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
5.7 Serial Input / 5.8 Serial Output

if(Serial)
Serial. available()
Serial. availableForWrite()
Serial. begin()
Serial. end()
Serial. find()
Serial. findUntil()
Serial. flush()
Serial. parseFloat()
Serial. parseInt()
Serial. peek()
Serial. print()
Serial. println()
Serial. read()
Serial. readBytes()
Serial. readBytesUntil()
Serial. readString()
Serial. readStringUntil()
Serial. setTimeout()
Serial. write()
Serial. serialEvent()
5.7 Serial Input / 5.8 Serial Output Syntax

Syntax
[Link]()

Parameters
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
5.7 Serial Input / 5.8 Serial Output Syntax

Syntax
[Link](val)
[Link](str)
[Link](buf, len)

Parameters
Serial: serial port object. See the list of available serial ports for
each board on the Serial main page.
val: a value to send as a single byte.
str: a string to send as a series of bytes.
buf: an array to send as a series of bytes.
len: the number of bytes to be sent from the array.
5.7 Serial Input / 5.8 Serial Output
5.9 PWM output

Pulse Width Modulation (PWM) is a technique used to generate an analog output signal using a digital signal. PWM is
achieved by rapidly switching a digital signal on and off, with a varying duty cycle. The duty cycle is defined as the proportion
of time that the signal is high (on) compared to the total period. A duty cycle of 50% means that the signal is high half of the
time and low half of the time.

 PWM can be used to control the brightness of an


LED, the speed of a motor, or the position of a
servo, among other things.
 PWM is also widely used in control systems to
control the power delivered to loads
 Such as motor controllers, power supplies, and
power inverters.
 PWM is used to control the average voltage or
current delivered to the load by varying the duty
cycle of the applied voltage or current
5.9 PWM output (analog write function)

Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various
speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the
next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin.
Syntax
analogWrite(pin, value)

Parameters
pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.

analogWriteResolution() is an extension of the Analog API for the Arduino Due.


analogWriteResolution() sets the resolution of the analogWrite() function. It defaults to 8 bits (values between 0-255) for
backward compatibility with AVR based boards
Syntax
analogWriteResolution(bits)

Parameters
bits: determines the resolution (in bits) of the values used in the analogWrite() function. The value can range from 1 to 32. If
you choose a resolution higher or lower than your board’s hardware capabilities, the value used in analogWrite() will be
either truncated if it’s too high or padded with zeros if it’s too low. See the note below for details.
analogWriteResolution

analogWrite
5.9 PWM output (analogWrite)
5.10 I2C and SPI

The Arduino microcontroller has also I2C and SPI output interfaces, which are used for communication with other devices,
such as sensors and displays. These interfaces are based on the I2C and SPI communication protocols, which are widely used
in embedded systems. The Arduino programming language provides libraries to facilitate communication over these
interfaces

I2C, short for Inter-Integrated Circuit, is a synchronous, serial communication protocol used to connect various electronic
devices together. I2C communication involves two lines: a data line (SDA) and a clock line (SCL). Devices connected to the
same I2C bus share these two lines, and each device is identified by a unique address.

The advantage of using I2C is that you only need two wires to communicate with multiple devices
5.10 I2C and SPI

The advantage of using I2C is that you only need two wires to communicate with multiple devices
5.10 I2C and SPI

SPI, short for Serial Peripheral Interface, is a synchronous, full-duplex communication protocol used to connect various
electronic devices together. It was developed by Motorola in the 1980s.

SPI communication involves four lines: a master-out, slave-in (MOSI) line, a master-in, slave-out (MISO) line, a clock line
(SCK), and a slave select (SS) line. The master device controls the communication and generates the clock signal on the SCK
line. The MOSI line carries data from the master to the slave device, and the MISO line carries data from the slave to the
master. The SS line is used to select the specific slave device with which the master wants to communicate.
5.10 I2C and SPI
5.10 I2C and SPI
This library allows you to communicate with I2C/TWI devices. On the Arduino boards with the R3 layout (1.0 pinout), the
SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. The Arduino Due has two I2C/TWI
interfaces SDA1 and SCL1 are near to the AREF pin and the additional one is on pins 20 and 21.

#include <Wire.h> Syntax


[Link]()
[Link]() [Link](address)
[Link]()
[Link]() Parameters
[Link]() address: the 7-bit slave address (optional); if not specified, join the bus as a
[Link]() controller device.
[Link]()
Syntax
[Link]()
[Link](value)
[Link]() Syntax [Link](string)
[Link]() [Link]() [Link](data, length)
[Link]()
[Link]() Parameters Parameters
[Link]() None. value: a value to send as a single byte.
[Link]()
string: a string to send as a series of bytes.
[Link]()
data: an array of data to send as bytes.
length: the number of bytes to transmit.
5.10 I2C and SPI
I2C
MASTER SLAVE
5.10 I2C and SPI
I2C

MASTER

SLAVE
5.11 Interrupt
The Arduino microcontroller can also generate interrupt signals, which are used to signal an event or trigger some action. This
feature can be useful for time-sensitive tasks, such as reading sensor data or controlling a stepper motor. Chapter 10 will
discuss details of microcontroller interrupts operation.

Main routine

Interrupt routine
5.11 Interrupt

Syntax

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)


attachInterrupt(interrupt, ISR, mode) (not recommended)
attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD
Boards, Uno WiFi Rev2, Due, and 101.)

Parameters
interrupt: the number of the interrupt. Allowed data types: int.
pin: the Arduino pin number.
ISR: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is
sometimes referred to as an interrupt service routine.

mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:

LOW to trigger the interrupt whenever the pin is low,


CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low.
HIGH to trigger the interrupt whenever the pin is high. (ONLY The Due, Zero and MKR1000 boards)
5.11 Interrupt
Example

The microcontroller run continuous conversion When the pushbutton is pressed, the analog
of analog input at pin A0. LCD display the value in conversion process will be halted, and the interrupt
volts unit and digital value (decimal). service routine (ISR) will serve. ISR routine will toggle a
LED and display “Interrupt detected” on the LCD
display
5.11 Interrupt
Summary
Section 5.1 - Introduction:
This section provides a general overview of Arduino programming, including what it is, what it can do, and how to get
started with programming it. It's an essential section for beginners, as it lays out the basics of using an Arduino board.

Section 5.2 - Analog Input:


Analog input involves reading the voltage on an analog pin and converting it into a digital value. This section covers the
basics of analog input, including how to read analog input values and how to interpret them.

Section 5.3 - Analog Input with External Reference:


Similar to regular analog input, this section covers the use of an external voltage reference instead of the default internal
reference. Using an external reference can improve the accuracy of your analog readings.

Section 5.4 - Analog Output:


Analog output allows you to output a variable voltage from an Arduino pin. This section covers how to set up an analog
output, how to use it to control things like LED brightness or motor speed, and how to read the output voltage.

Section 5.5 - Digital Input:


Digital input involves reading the state of a digital pin, which can be either HIGH or LOW. This section covers how to set up
digital input, how to read digital input values, and how to use it for detecting things like button presses or switches.
Summary

Section 5.6 - Digital Output:


Digital output allows you to set the state of a digital pin, which can be either HIGH or LOW. This section covers how to set
up digital output, how to use it for controlling things like LEDs or relays, and how to read the state of a digital output pin.

Section 5.7 - Serial Input:


Serial input involves receiving data from a serial connection, such as a USB port or Serial Pin. This section covers how to
set up serial input, how to read serial data, and how to use it for communicating with other devices or sensors.

Section 5.8 - Serial Output:


Serial output involves sending data over a serial connection. This section covers how to set up serial output, how to send
data over a serial connection, and how to use it for sending data to other devices or displaying information on a
computer.

Section 5.9 - PWM Output:


PWM output involves using pulse-width modulation to control the output voltage of a pin. This section covers how to set
up PWM output, how to use it to control things like motor speed or LED brightness, and how to read the output voltage.

Section 5.10 - I2C and SPI Output:


I2C and SPI output involve using specialized protocols to communicate with other devices, such as sensors or displays.
This section covers how to set up I2C and SPI output, how to use it to communicate with other devices, and how to read
data from those devices.
Summary

Section 5.11 - Interrupts:


Interrupts allow you to pause the execution of your code and handle an external event, such as a button press or a
change in a sensor reading. This section covers how to set up interrupts, how to handle interrupt events, and how to use
them for creating responsive and interactive programs.
Reference

Official Arduino Reference website that provides documentation for the Arduino programming language and functions. It
includes a comprehensive list of libraries and functions, examples, and tutorials for Arduino programming. The website is a
valuable resource for both beginners and advanced users of the Arduino platform.

[Link]

You might also like