Internet of Things (Arduino) M2 Notes
Internet of Things (Arduino) M2 Notes
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
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
void setup()
void loop()
delay(1000);
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: