0% found this document useful (0 votes)
14 views

Internet of Things (Arduino) M2 Notes

The document provides an overview of the Arduino UNO, detailing its features, components, and programming structure. It covers the board's microcontroller, input/output pins, data types, constants, and operators used in Arduino programming. Additionally, it explains the setup and loop functions essential for Arduino sketches.

Uploaded by

Aliza Quraishi
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)
14 views

Internet of Things (Arduino) M2 Notes

The document provides an overview of the Arduino UNO, detailing its features, components, and programming structure. It covers the board's microcontroller, input/output pins, data types, constants, and operators used in Arduino programming. Additionally, it explains the setup and loop functions essential for Arduino sketches.

Uploaded by

Aliza Quraishi
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
You are on page 1/ 20

Internet of Things

Unit-II:
# What is Arduino Uno?
ANS:
The Arduino UNO is a standard board of Arduino. Here UNO means 'one' in
Italian. It was named as UNO to label the first release of Arduino Software. It
was also the first USB board released by Arduino. It is considered as the
powerful board used in various projects. Arduino.cc developed the Arduino
UNO board.
Arduino UNO is based on an ATmega328P microcontroller. It is easy to use
compared to other boards, such as the Arduino Mega board, etc. The board
consists of digital and analog Input/Output pins (I/O), shields, and other
circuits.
The Arduino UNO includes 6 analog pin inputs, 14 digital pins, a USB connector,
a power jack, and an ICSP (In-Circuit Serial Programming) header. It is
programmed based on IDE, which stands for Integrated Development
Environment. It can run on both online and offline platforms.
The IDE is common to all available boards of Arduino.
# Arduino Uno Block Diagram:
o ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel
family. The processor code inside it is of 8-bit. It combines Memory (SRAM,
EEPROM, and Flash), Analog to Digital Converter, SPI serial ports, I/O lines,
registers, timer, external and internal interrupts, and oscillator.
o ICSP pin - The In-Circuit Serial Programming pin allows the user to program
using the firmware of the Arduino board.
o Power LED Indicator- The ON status of LED shows the power is activated.
When the power is OFF, the LED will not light up.
o Digital I/O pins- The digital pins have the value HIGH or LOW. The pins
numbered from D0 to D13 are digital pins.
o TX and RX LED's- The successful flow of data is represented by the lighting of
these LED's.
o AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to
the Arduino UNO board from the external power supply.
o Reset button- It is used to add a Reset button to the connection.
o USB- It allows the board to connect to the computer. It is essential for the
programming of the Arduino UNO board.
o Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which
makes the Arduino UNO a powerful board.
o Voltage Regulator- The voltage regulator converts the input voltage to 5V.
o GND- Ground pins. The ground pin acts as a pin with zero voltage.
o Vin- It is the input voltage.
o Analog Pins- The pins numbered from A0 to A5 are analog pins. The function
of Analog pins is to read the analog sensor used in the connection. It can also
act as GPIO (General Purpose Input Output) pins.
# Sketch Structure of ARDUINO UNO:
Software structure consist of two main functions −

• Setup( ) function
• Loop( ) function

void setup ( ) {}
PURPOSE − The setup() function is called when a sketch starts. Use it to
initialize the variables, pin modes, start using libraries, etc. The setup
function will only run once, after each power up or reset of the Arduino
board.

void loop ( ) {}
PURPOSE − After creating a setup() function, which initializes and sets
the initial values, the loop() function does precisely what its name
suggests, and loops consecutively, allowing your program to change and
respond. Use it to actively control the Arduino board.
# ARDUINO Data Types:
The data types are used to identify the types of data and the associated functions for
handling the data. It is used for declaring functions and variables, which determines the bit
pattern and the storage space.
The data types that we will use in the Arduino are listed below:
o void Data Type
o int Data Type
o Char Data Type
o Float Data Type
o Double Data Type
o Unsigned int Data Type
o short Data Type
o long Data Type
o Unsigned long Data Type
o byte data type
o word data type

void Data Type


The void data type specifies the empty set of values and only used to declare the functions.
It is used as the return type for the functions that do not return any value.

int Data Type


The integer data types are the whole numbers like 5, -6, 10, -123, etc. They do not have any
fractional part. The integer data types are represented by int. It is considered as the primary
data type to store the numbers.
The size of int is 2 bytes ( 16 bits).
Minimal range: -32768 to 32767 or - (2^ 15) to ((2 ^ 15) - 1)
In the ATmega and Arduino UNO boards, the int data types store the value of 2 bytes.
On the boards like Arduino Zero and MKR1000 (SAMD boards), and Arduino Due, the int
data type stores the value of 4 bytes or 32 bits. The Minimal range in such case would be -
(2^ 31) to ((2 ^ 31) - 1) or -2,147,483,648 to 2,147,483,647.
The negative numbers are stored in the form of 2's complement, where the sign bit or the
highest bit is flagged as the negative number.
char Data Type
The char datatype can store any number of character set. An identifier declared as the char
becomes a character variable. The literals are written inside a single quote.
The char type is often said to be an integer type. It is because, symbols, letters, etc., are
represented in memory by associated number codes and that are only integers.
The size of character data type is minimum of 8 bits. We can use the byte data type for an
unsigned char data type of 8 bits or 1 byte.
For example, character ' A ' has the ASCII value of 65.
If we specify, ' A ' + 2, it will have the ASCII value of 67.

float Data Type


A number having the fractional part and a decimal part is considered as a floating-point
number. For example, 4.567 is a floating-point number. The number 13 is an integer, while
13.0 is a floating-point number. Due to their greater resolution, fractional numbers are used
to approximate the contiguous and analog values.
Floating point numbers can also be written in the exponent form. The numbers can be as
large as 3.4028235E+38 and as small as -3.4028235E+38. The size of float data types is 4
bytes or 32 bits.
Note: We need to add a decimal point to a number. Otherwise, it will be considered as an
integer. For example, 14.0 is considered as a float number, while 14 is an integer.

double Data Type


The double data type is also used for handling the decimal or floating-point numbers. It
occupies twice as much memory as float. It stores floating point numbers with larger
precision and range. It stands for double precision floating point numbers.
It occupies 4 bytes in ATmega and UNO boards, while 8 bytes on Arduino Due.

unsigned int Data Type


The unsigned int stores the value upto 2 bytes or 16 bits. It stores only positive values. The
range of unsigned int data type is from 0 to 65,535 or 0 to ((2 ^ 16) - 1).
Arduino Due stores the unsigned data value of 4 bytes or 32-bits.
The difference between Unsigned and signed data type is the sign bit. The int type in
Arduino is the signed int. In a 16-bit number, 15 bits are interpreted with the 2's
complement, while the high bit is interpreted as the positive or negative number. If the high
bit is '1', it is considered as a negative number.

short Data Type


The short is an integer data type that stores two bytes or 16-bit of data.
The range of short data types is from -32768 to 32767 or - (2^ 15) to ((2 ^ 15) - 1). The ARM
and ATmega based Arduino's usually stores the data value of 2 bytes.

long Data Type


The long data types are considered as the extended size variables, which store 4 bytes (32 -
bits). The size ranges from -2,147,483,648 to 2,147,483,647.
While using integer numbers, at least one of the numbers should be followed by L, which
forces the number to be a long data type.
# Constants in ARDUINO?
The constants in Arduino are defined as the predefined expressions. It makes the code easy
to read.
The constants in Arduino are defined as:
Logical level Constants
The logical level constants are true or false.
The value of true and false are defined as 1 and 0. Any non-zero integer is determined as
true in terms of Boolean language. The true and false constants are type in lowercase rather
than uppercase (such as HIGH, LOW, etc.).
Pin level Constants
The digital pins can take two value HIGH or LOW.
In Arduino, the pin is configured as INPUT or OUTPUT using the pinMode() function. The pin
is further made HIGH or LOW using the digitalWrite() function.
LED_BUILTIN Constant
The Arduino boards have built-in LED connected in series with the resistor. The particular
pin number is defined with the constant name called LED_BUILTIN.
Most Arduino boards have the LED_BUILTIN connected to Pin number 13.
Constant Keyword
The name const represents the constant keyword. It modifies the behavior of the variables
in our program. It further makes the variable as 'read-only'.
The variable will remain the same as other variables, but its value cannot be changed.
It means we cannot modify the constant. For example,
const int a =2;
#define
The #define in Arduino is used to give a name to the constant value. It does not take any
memory space on the chip.
At the compile time, the compiler will replace the predefined value in the program to the
constants with the defined value.
The syntax is:
1. #define a=5;
# Arduino Operators
The operators are widely used in Arduino programming from basics to
advanced levels. It plays a crucial role in every programming concept like C,
C++, Java, etc.
The operators are used to solve logical and mathematical problems. For
example, to calculate the temperature given by the sensor based on some
analog voltage.
The types of Operators classified in Arduino are:
1. Arithmetic Operators
2. Compound Operators
3. Boolean Operators
4. Comparison Operators
5. Bitwise Operators
Arithmetic Operators
There are six basic operators responsible for performing mathematical operations in
Arduino, which are listed below:
o Assignment Operator ( = )
The Assignment operator in Arduino is used to set the variable's value. It is quite different
from the equal symbol (=) normally used in mathematics.
o Addition ( + )
The addition operator is used for the addition of two numbers. For example, P + Q.
o Subtraction ( - )
Subtraction is used to subtract one value from the another. For example, P - Q.
o Multiplication ( * )
The multiplication is used to multiply two numbers. For example, P * Q.
o Division ( / )
The division is used to determine the result of one number divided with another. For
example, P/Q.
o Modulus ( % )
The Modulus operator is used to calculate the remainder after the division of one number
by another number.

Compound Operators
The compound operators perform two or more calculations at once.
The result of the right operand is assigned to the left operand, as already discussed above.
The same condition will apply to all the compound operators, which are listed below:
Let's consider a variable b.
o b++
Here, b = b + 1. It is called the increment operator.
o b+=
For example, b + = 4. It means, b = b+ 4.
o b--
Here, b = b - 1. It is called as the decrement operator.
o b-=
For example, b - = 3. It means, b = b - 3.
o b*=
For example, b * = 6. It means, b = b * 6.
ADVERTISEMENT
o b/=
For example, b / = 5. It means, b = b / 5.
o b%=
For example, b % = 2. It means, b = b % 2.

Boolean Operators
The Boolean Operators are NOT ( ! ), Logical AND ( & & ), and Logical OR ( | | ).
Let's discuss the above operators in detail.
o Logical AND ( & & )
The result of the condition is true if both the operands in the condition are true.
Consider the below example:
1. if ( a = = b & & b = = c )
Above statement is true if both conditions are true. If any of the conditions is false, the
statement will be false.
o Logical OR ( | | )
The result of the condition is true, if either of the variables in the condition is true.
Consider the below example.
1. if ( a > 0 | | b > 0 )
The above statement is true, if either of the above condition ( a> 0 or b > 0 ) is true.
o NOT ( ! )
It is used to reverse the logical state of the operand.
For example, a ! = 2.
The NOT operator returns the value 1 or TRUE when the specified operand is FALSE. It also
reverses the value of the specified expression.

Comparison Operators
The comparison operators are used to compare the value of one variable with the other.
The comparison operators are listed below:
o less than ( < )
The less than operator checks that the value of the left operand is less than the right
operand. The statement is true if the condition is satisfied.
o greater than ( > )
The less than operator checks that the value of the left side of a statement is greater than
the right side. The statement is true if the condition is satisfied.
For example, a > b.
If a is greater than b, the condition is true, else false.
o equal to ( = = )
It checks the value of two operands. If the values are equal, the condition is satisfied.
For example, a = = b.
The above statement is used to check if the value of a is equal to b or not.
o not equal to ( ! = )
It checks the value of two specified variables. If the values are not equal, the condition will
be correct and satisfied.
For example, a ! = b.
o less than or equal to ( < = )
The less or equal than operator checks that the value of left side of a statement is less or
equal to the value on right side. The statement is true if either of the condition is satisfied.
For example, a < = b
It checks the value of a is less or equal than b.
o greater than or equal to ( > = )
The greater or equal than operator checks that the value of the left side of a statement is
greater or equal to the value on the right side of that statement. The statement is true if the
condition is satisfied.
For example, a > = b
It checks the value of a is greater or equal than b. If either of the condition satisfies, the
statement is true.

Bitwise Operators
The Bitwise operators operate at the binary level. These operators are quite easy to use.
There are various bitwise operators. Some of the popular operators are listed below:
o bitwise NOT ( ~ )
The bitwise NOT operator acts as a complement for reversing the bits.
For example, if b = 1, the NOT operator will make the value of b = 0.
o bitwise XOR ( ^ )
The output is 0 if both the inputs are same, and it is 1 if the two input bits are different.
For example,
1. 1 0 0 1 // input 1 or operand 1
2. 0 1 0 1 // input 2
3. 1 1 0 0 // Output
o bitwise OR ( | )
The output is 0 if both of the inputs in the OR operation are 0. Otherwise, the output is 1.
The two input patterns are of 4 bits.
For example,
1. 1 1 0 0 // input 1 or operand 1
2. 0 0 0 1 // input 2
3. 1 1 0 1 // Output ( resultant - OR)
o bitwise AND ( & )
The output is 1 if both the inputs in the AND operation are 1. Otherwise, the output is 0. The
two input patterns are of 4 bits.
For example,
1. 1 1 0 0 // input 1 or operand 1
2. 0 1 0 1 // input 2
3. 0 1 0 0 // Output ( resultant - AND)
o bitwise left shift ( < < )
The left operator is shifted by the number of bits defined by the right operator.
o bitwise right shift ( > > )
The right operator is shifted by the number of bits defined by the left operator
# Functions in Arduino
Functions are blocks of code that perform specific tasks. They help in
organizing the code, making it more readable, reusable, and easier to debug. In
Arduino, there are two primary types of functions: User-defined functions and
Predefined (built-in) functions.

a. User-Defined Functions
These are functions that you create to perform specific tasks in your code. They typically
follow this structure:

Code:
returnType functionName(parameters) {
// Code to be executed
return value; // optional, only if returnType is not void
}

b. Predefined Functions
Arduino provides some predefined functions that are commonly used in sketches:

setup(): This function runs once when the sketch starts. It's used to initialize variables, pin
modes, start using libraries, etc.
loop(): This function runs continuously after setup(). It’s where the main code of the
program runs.
digitalWrite(pin, value): Writes a HIGH or LOW value to a digital pin.
digitalRead(pin): Reads the value from a specified digital pin, either HIGH or LOW.
analogWrite(pin, value): Writes an analog value (PWM wave) to a pin.
analogRead(pin): Reads the value from a specified analog pin.
# Library Functions in Arduino
Arduino libraries are collections of code that make it easy to connect to a
sensor, display, module, etc. These libraries provide pre-written functions that
simplify tasks.

a. Using Libraries
You can include libraries in your sketch using the #include directive. Once included, you can
use the functions provided by the library.

Example:

Code:
#include <Servo.h> // Include the Servo library

Servo myServo; // Create a Servo object

void setup()

myServo.attach(9); // Attach the Servo to pin 9

void loop()

myServo.write(90); // Move the Servo to 90 degrees

delay(1000);

myServo.write(0); // Move the Servo back to 0 degrees

delay(1000);

}
b. Common Libraries and Their Functions
Servo Library:
Used for controlling servo motors.
attach(pin): Attaches the Servo variable to a pin.
write(angle): Sets the servo position to a specified angle.
Wire Library:
Used for I2C communication.
begin(): Initiates the Wire library.
requestFrom(address, quantity): Requests data from a slave device.
LiquidCrystal Library:
Used for controlling LCD displays.
begin(cols, rows): Initializes the LCD with the number of columns and rows.
print(text): Prints text on the LCD.
# Library functions:
I/O Functions:
1. digitalRead(pin): Reads the value from a specified digital pin, either HIGH or LOW.
2. digitalWrite(pin, value): Writes a HIGH or LOW value to a digital pin.
3. pinMode(pin, mode): Configures the specified pin to behave either as an input or an
output.
4. analogRead(pin): Reads the value from the specified analog pin. The returned value
will be between 0 and 1023.
5. analogWrite(pin, value): Writes an analog value (PWM wave) to a pin. Value can
range from 0 to 255.
6. analogReference(type): Configures the reference voltage used for analog input (i.e.,
the value used as the top of the input range).
Char Functions:
1. isAlpha(character): Returns true if the character is an alphabetic letter (a-z or A-Z).
2. isAlphaNumeric(character): Returns true if the character is an alphabetic letter or a
digit (a-z, A-Z, 0-9).
3. isDigit(character): Returns true if the character is a digit (0-9).
4. isHexadecimalDigit(character): Returns true if the character is a valid hexadecimal
digit (0-9, A-F, a-f).
5. isSpace(character): Returns true if the character is a space character (spaces, tabs,
line breaks, etc.).
6. isWhitespace(character): Returns true if the character is a whitespace character.
7. isUpperCase(character): Returns true if the character is an uppercase letter (A-Z).
8. isLowerCase(character): Returns true if the character is a lowercase letter (a-z).
Math Functions:
1. abs(x): Returns the absolute value of x.
2. constrain(x, a, b): Constrains x to lie between a and b.
3. max(a, b): Returns the larger of a or b.
4. min(a, b): Returns the smaller of a or b.
5. pow(base, exponent): Returns the value of base raised to the power of exponent.
6. sqrt(x): Returns the square root of x.
# Serial Communication Functions:
Serial:
• Represents the hardware serial port on the Arduino board. It's used to communicate
with the computer or other serial devices.
Serial.begin(baudrate):
• Initializes the serial communication at the specified baud rate (e.g., 9600). This
function needs to be called before any other serial functions.
Serial.end():
• Ends the serial communication. It disables the serial communication and frees the
serial port.
Serial.available():
• Returns the number of bytes (characters) available to read from the serial port. It
can be used to check if there is any data available before attempting to read it.
Serial.print(data):
• Prints data to the serial port as human-readable ASCII text. It can handle different
data types like integers, floats, strings, etc. You can also specify the format (e.g.,
decimal, hexadecimal, binary) if needed.
Serial.println(data):
• Similar to Serial.print(), but it adds a newline character after printing the data,
moving the cursor to the next line.
Serial.write(data):
• Writes binary data to the serial port. The data can be a byte, a string, or an array of
bytes. It sends the exact bytes, so it’s used for sending raw data.
Serial.read():
• Reads the first byte of incoming serial data. If no data is available, it returns -1. It’s
typically used in conjunction with Serial.available() to ensure there's data to read.
Serial.readBytes(buffer, length):
• Reads a specified number of bytes from the serial port into a buffer. It returns the
number of bytes read and stores them in the provided buffer.
Serial.readString():
• Reads incoming serial data as a string until a newline character is detected. It’s useful
for reading text-based data sent over serial.
Blinking an LED
Blinking an LED is an introductory Arduino project in which we control an LED using
Arduino. LED blinking refers to the process of continuously turning an LED (Light Emitting
Diode) and off in a repetitive pattern. It is a simple and common demonstration in
electronics and microcontroller-based projects.
Working Procedure
setup() and loop() are two fundamental Arduino functions for controlling the behavior of
your board. The Arduino framework automatically calls these functions, which form the
foundation of any Arduino program.
The setup() function is only called once when the Arduino board boots up or is reset. Its
goal is to set pin modes, initialize variables, and execute any other necessary setup tasks
before the main loop begins. This function can be used to configure settings that should
only be changed once over the board’s lifespan.
The loop() function is the heart of an Arduino program. After the setup() function is
executed, the loop() function starts running repeatedly until the Arduino is powered off or
reset. It contains the main code that performs the desired tasks, controls the board, user
input. Whatever is included in the loop() function will be executed in a continuous loop,
allowing the Arduino to perform its intended functions continuously.
In the code, we have declared two integers, LEDpin and delayT. LEDpin represents the pin
number of the Arduino where LEDs need to be connected, and delayT is an integer variable
for the delay() function. The delay() function accepts values in milliseconds.

Code:
int LEDpin = 13;
int delayT = 1000;
void setup() {
// put your setup code here, to run once:
pinMode(LEDpin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LEDpin, HIGH);
delay(delayT);
digitalWrite(LEDpin, LOW);
delay(delayT);
}
Circuit diagram:

You might also like