Unit Iii Iot and Arduino Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 119

UNIT III IOT AND ARDUINO PROGRAMMING

Introduction to the Concept of IoT Devices – IoT Devices Versus Computers – IoT
Configurations – Basic Components – Introduction to Arduino Types of Arduino –
Arduino Toolchain – Arduino Programming Structure – Sketches – Pins –
Input/Output From Pins Using Sketches – Introduction to Arduino Shields –
Integration of Sensors and Actuators with Arduino.

Introduction to Internet of Things (IoT)

IoT stands for Internet of Things. It refers to the interconnectedness of physical


devices, such as appliances and vehicles, that are embedded with software,
sensors, and connectivity which enables these objects to connect and exchange
data. This technology allows for the collection and sharing of data from a vast
network of devices, creating opportunities for more efficient and automated
systems.

Internet of Things (IoT) is the networking of physical objects that contain


electronics embedded within their architecture in order to communicate and sense
interactions amongst each other or with respect to the external environment. In the
upcoming years, IoT-based technology will offer advanced levels of services and
practically change the way people lead their daily lives. Advancements in
medicine, power, gene therapies, agriculture, smart cities, and smart homes
are just a few of the categorical examples where IoT is strongly established.

IOT is a system of interrelated things, computing devices, mechanical and


digital machines, objects, animals, or people that are provided with unique
identifiers. And the ability to transfer the data over a network requiring human-to-
human or human-to-computer interaction.

History of IOT

Here you will get to know about how IOT is involved and also from the
explanation of each will let you know how IOT plays a role in this innovations !

 1982 – Vending machine: The first glimpse of IoT emerged as a vending


machine at Carnegie Mellon University was connected to the internet to
report its inventory and status, paving the way for remote monitoring.
 1990 – Toaster: Early IoT innovation saw a toaster connected to the
internet, allowing users to control it remotely, foreshadowing the
convenience of smart home devices.

 1999 – IoT Coined (Kevin Ashton): Kevin Ashton coined the term
“Internet of Things” to describe the interconnected network of devices
communicating and sharing data, laying the foundation for a new era of
connectivity.

 2000 – LG Smart Fridge: The LG Smart Fridge marked a breakthrough,


enabling users to check and manage refrigerator contents remotely,
showcasing the potential of IoT in daily life.

 2004 – Smart Watch: The advent of smartwatches introduced IoT to the


wearable tech realm, offering fitness tracking and notifications on-the-go.

 2007 – Smart iPhone: Apple’s iPhone became a game-changer, integrating


IoT capabilities with apps that connected users to a myriad of services and
devices, transforming smartphones into hubs.

 2009 – Car Testing: IoT entered the automotive industry, enhancing


vehicles with sensors for real-time diagnostics, performance monitoring, and
remote testing.

 2011 – Smart TV: The introduction of Smart TVs brought IoT to the living
room, enabling internet connectivity for streaming, app usage, and
interactive content.

 2013 – Google Lens: Google Lens showcased IoT’s potential in image


recognition, allowing smartphones to provide information about objects in
the physical world.

 2014 – Echo: Amazon’s Echo, equipped with the virtual assistant Alexa,
demonstrated the power of voice-activated IoT, making smart homes more
intuitive and responsive.

 2015 – Tesla Autopilot: Tesla’s Autopilot system exemplified IoT in


automobiles, introducing semi-autonomous driving capabilities through
interconnected sensors and software.

Four Key Components of IOT


 Device or sensor

 Connectivity

 Data processing

 Interface

IoT is network of interconnected computing devices which are embedded in


everyday objects, enabling them to send and receive data.

Over 9 billion ‘Things’ (physical objects) are currently connected to the


Internet, as of now. In the near future, this number is expected to rise to a
whopping 20 billion.

Main Components Used in IoT

 Low-power embedded systems: Less battery consumption, high


performance are the inverse factors that play a significant role during the
design of electronic systems.

 Sensors: Sensors are the major part of any IoT application. It is a physical
device that measures and detects certain physical quantities and
converts it into signal which can be provided as an input to processing
or control unit for analysis purpose.

Different types of Sensors

 Temperature Sensors

 Image Sensors

 Gyro Sensors-for detecting object orientation, angular stability

 Obstacle Sensors

 RF Sensor

 IR Sensor
 MQ-02/05 Gas Sensor

 LDR Sensor

 Ultrasonic Distance Sensor

 Control Units: It is a unit of small computer on a single integrated circuit


containing microprocessor or processing core, memory and
programmable input/output devices/peripherals. It is responsible for
major processing work of IoT devices and all logical operations are carried
out here.

 Cloud computing: Data collected through IoT devices is massive, and


this data has to be stored on a reliable storage server. This is where cloud
computing comes into play. The data is processed and learned, giving more
room for us to discover where things like electrical faults/errors are within
the system.

 Availability of big data: We know that IoT relies heavily on sensors,


especially in real-time. As these electronic devices spread throughout every
field, their usage is going to trigger a massive flux of big data.

 Networking connection: In order to communicate, internet connectivity


is a must, where each physical object is represented by an IP address.
However, there are only a limited number of addresses available according
to the IP naming. Due to the growing number of devices, this naming system
will not be feasible anymore. Therefore, researchers are looking for another
alternative naming system to represent each physical object.

Ways of Building IOT

There are two ways of building IoT:

 Form a separate internet work including only physical objects.

 Make the Internet ever more expansive, but this requires hard-core
technologies such as rigorous cloud computing and rapid big data storage
(expensive).

In the near future, IoT will become broader and more complex in terms of scope. It
will change the world in terms of “anytime, anyplace, anything in connectivity.”
IoT Enablers

 RFIDs: uses radio waves in order to electronically track the tags attached to
each physical object.

 Sensors: devices that are able to detect changes in an environment (ex:


motion detectors).

 Nanotechnology: as the name suggests, these are tiny devices with


dimensions usually less than a hundred nanometers.

 Smart networks: (ex: mesh topology).

Working with IoT Devices

 Collect and Transmit Data : For this purpose sensors are widely used
they are used as per requirements in different application areas.

 Actuate device based on triggers produced by sensors or processing


devices: If certain conditions are satisfied or according to user’s
requirements if certain trigger is activated then which action to perform that
is shown by Actuator devices.

 Receive Information: From network devices, users or devices can take


certain information also for their analysis and processing purposes.

 Communication Assistance: Communication assistance is the phenomenon


of communication between 2 networks or communication between 2 or more
IoT devices of same or different networks. This can be achieved by different
: MQTT, Constrained Application Protocol, ZigBee, FTP, HTTP etc.
Working of IoTac

Characteristics of IoT

 Massively scalable and efficient

 IP-based addressing will no longer be suitable in the upcoming future.

 An abundance of physical objects is present that do not use IP, so IoT is


made possible.
 Devices typically consume less power. When not in use, they should be
automatically programmed to sleep.

 A device that is connected to another device right now may not be connected
in another instant of time.

 Intermittent connectivity – IoT devices aren’t always connected. In order to


save bandwidth and battery consumption, devices will be powered off
periodically when not in use. Otherwise, connections might turn unreliable
and thus prove to be inefficient.

Desired Quality of any IoT Application

Interconnectivity

It is the basic first requirement in any IoT infrastructure. Connectivity should be


guaranteed from any devices on any network then only devices in a network can
communicate with each other.

Heterogeneity

There can be diversity in IoT enabled devices like different hardware and software
configuration or different network topologies or connections, but they should
connect and interact with each other despite so much heterogeneity.

Dynamic in Nature

IoT devices should dynamically adapt themselves to the changing surroundings


like different situations and different prefaces.

Self-adapting and self configuring technology

For example, surveillance camera. It should be flexible to work in different


weather conditions and different light situations (morning, afternoon, or night).

Intelligence

Just data collection is not enough in IoT, extraction of knowledge from the
generated data is very important. For example, sensors generate data, but that data
will only be useful if it is interpreted properly. So intelligence is one of the key
characteristics in IoT. Because data interpretation is the major part in any IoT
application because without data processing we can’t make any insights from data.
Hence, big data is also one of the most enabling technologies in IoT field.

Scalability

The number of elements (devices) connected to IoT zones is increasing day by day.
Therefore, an IoT setup should be capable of handling the expansion. It can be
either expand capability in terms of processing power, storage, etc. as vertical
scaling or horizontal scaling by multiplying with easy cloning.

Identity

Each IoT device has a unique identity (e.g., an IP address). This identity is helpful
in communication, tracking and to know status of the things. If there is no
identification then it will directly affect security and safety of any system because
without discrimination we can’t identify with whom one network is connected or
with whom we have to communicate. So there should be clear and appropriate
discrimination technology available between IoT networks and devices.

Safety

Sensitive personal details of a user might be compromised when the devices are
connected to the Internet. So data security is a major challenge. This could cause a
loss to the user. Equipment in the huge IoT network may also be at risk. Therefore,
equipment safety is also critical.

Architecture

It should be hybrid, supporting different manufacturer’s products to function in the


IoT network.

As a quick note, IoT incorporates trillions of sensors, billions of smart systems,


and millions of applications.

Application Domains

IoT is currently found in four different popular domains:

1) Manufacturing/Industrial business - 40.2%


2) Healthcare - 30.3%
3) Security - 7.7%
4) Retail - 8.3%

Modern Applications

 Smart Grids and energy saving

 Smart cities

 Smart homes/Home automation

 Healthcare

 Earthquake detection

 Radiation detection/hazardous gas detection

 Smartphone detection

 Water flow monitoring

 Traffic monitoring

 Wearables

 Smart door lock protection system

 Robots and Drones

 Healthcare and Hospitals, Telemedicine applications

 Security

 Biochip Transponders (For animals in farms)

 Heart monitoring implants (Example Pacemaker, ECG real time tracking)

 Agriculture

 Industry

Advantages of IoT
 Improved efficiency and automation of tasks.

 Increased convenience and accessibility of information.

 Better monitoring and control of devices and systems.

 Greater ability to gather and analyze data.

 Improved decision-making.

 Cost savings.

Disadvantages of IoT

 Security concerns and potential for hacking or data breaches.

 Privacy issues related to the collection and use of personal data.

 Dependence on technology and potential for system failures.

 Limited standardization and interoperability among devices.

 Complexity and increased maintenance requirements.

 High initial investment costs.

 Limited battery life on some devices.

 Concerns about job displacement due to automation.

 Limited regulation and legal framework for IoT, which can lead to confusion
and uncertainty.

IoT Configuration
 Configuration management is essential to keep accurate network
configuration records and to help organizations avoid potential
challenges or problems within their networks. IT teams can't manage a
network if they lose track of connected devices, device configurations and
an accurate picture of the device connections.
 Self Configuring: IOT devices have self configuring capability, allowing
a large number of devices to work together to provide certain
functionality. These devices have the ability configure themselves setup
networking, and fetch latest software upgrades with minimal manual or user
interaction.
Importance of self configuring in IoT:
 IoT devices are able to upgrade their software in accordance with
requirements with a
minimum of user participation. Additionally, they can set up the network,
allowing for the addition of new devices to an already-existing network.
Uses of Configuration:
 In computing, configuration files (commonly known simply as config files)
are files used to configure the parameters and initial settings for some
computer programs. They are used for user applications, server processes
and operating system settings.
 Internet of things device management (IoT device management) involves
remotely
registering, configuring, provisioning, maintaining and monitoring
connected devices from a centralized platform that IT administrators can
access with an internet connection from anywhere on any device.
 IoT networks can be categorized into four main types: LAN/PAN, LPWAN,
and Mesh
protocols. The classification is based on factors like network coverage and
available
bandwidth. The majority of IoT networks operate wirelessly.
INSTALL AND CONFIGURE IOT:
1. Add your wireless device to AWS IoT Core for LoRaWAN.
2. Add profiles to AWS IoT Core for LoRaWAN.
3. Add destinations to AWS IoT Core for LoRaWAN.
4. Create rules to process LoRaWAN device messages.
5. Connect your LoRaWAN device and verify its connection status.
IoT devices be configured to work in a network.
Top Cybersecurity Configuration Tips for IoT Devices
1. Change Default Passwords and Usernames.
2. Regularly Update Firmware and Software.
3. Implement Network Segmentation.
4. Disable Unnecessary Features and Services.
5. Use Encryption for Data Transmission.
6. Employ Multi-Factor Authentication (MFA)
IoT Devices Versus Computers

Difference between IoT devices and Computers:


Computers
IOT Devices

IoT devices are special-purpose devices. Computers are general-purpose devices.

IoT devices can do only a particular task


Computers can do so many tasks.
for which it is designed.

The hardware and software built-in in the


The hardware and software built-in in
computers are streamlined to do many
the IoT devices are streamlined for that
tasks(such as calculation, gaming, music
particular task.
player, etc. )

IoT devices can be cheaper and faster at


a particular task than computers, as IoT A computer can be expensive and slower at
devices are made to do that particular a particular task than an IoT device.
task.

Examples: Music Player- iPod, Alexa,


Examples: Desktop computers, Laptops, etc.
smart cars, etc.

IOT CONFIGURATIONS

 Configuring an Internet of Things (IoT) system involves setting up and managing


various components to ensure proper communication, data collection, and control.

 Here's a general outline of the steps involved in configuring an IoT system:

1. Define Use Case and Requirements:

Clearly define the purpose of your IoT system, the data you want to collect,
the devices involved, and the desired outcomes. This will guide the entire
configuration process.

2. Select IoT Platform:

Choose an appropriate IoT platform that suits your needs. Popular platforms
include AWS IoT, Microsoft Azure IoT, Google Cloud IoT, and more. These platforms
provide tools and services to manage devices, data, and analytics.
3. Device Selection and Configuration:

Choose the IoT devices that will collect and transmit data. These devices
could be sensors, actuators, gateways, or other embedded systems. Configure the
devices to connect to your chosen IoT platform. This involves setting up device
identities, credentials, and communication protocols (MQTT, CoAP, HTTP, etc.).

4. Security Considerations:

Implement security measures to protect your IoT system from unauthorized


access and data breaches. This includes using secure communication protocols,
device authentication, data encryption, and regular security updates.

5. Data Ingestion and Storage:

Configure your IoT platform to receive, process, and store the data generated
by your devices. Set up data ingestion pipelines and storage solutions, such as
databases, data lakes, or cloud storage.

6. Data Processing and Analytics:

Define data processing pipelines to transform and analyze the incoming data.
This could involve real-time analytics, machine learning models, and pattern
recognition to extract meaningful insights from the data.

7. Remote Monitoring and Control:

If your IoT system involves remote control of devices, configure the


necessary interfaces to enable remote monitoring and control. This might include
building a user interface or integrating with existing dashboards.

8. Scalability and Performance:

Ensure that your IoT system is designed to handle the expected load.
Configure scaling options to accommodate an increasing number of devices and data
volume.

9. Alerts and Notifications:

Set up alerts and notifications to be informed about critical events or


anomalies in your IoT system. This could involve sending emails, text messages, or
triggering automated actions.

10.Data Visualization:
Configure data visualization tools to display the insights and information extracted
from your IoT data. Dashboards and visual representations help you understand the
system's performance and make informed decisions.

11.Testing and Quality Assurance:

Thoroughly test the entire IoT system before deployment. This includes
testing device connectivity, data flow, security measures, and the overall
functionality of the system.

12.Deployment and Monitoring:

Deploy the configured IoT system in the target environment. Monitor its
performance, stability, and security on an ongoing basis. Implement procedures for
updates, patches, and maintenance.

Basic Components

Major Components of IOT:

These are explained as following below.

 Things or Device
 These are fitted with sensors and actuators.
 Sensors collect data from the environment and give to gateway where as
actuators performs the action (as directed after processing of data).

 Gateway

 The sensors give data to Gateway and here some kind of pre-processing of
data is even done.
 It also acts as a level of security for the network and for the transmitted data.

 Cloud

 The data after being collected is uploaded to cloud.

 Cloud in simple terms is basically a set of servers connected to internet 24*7.


 Analytics

 The data after being received in the cloud processing is done. Various
algorithms are applied here for proper analysis of data (techniques like
Machine Learning etc are even applied).

 User Interface

 User end application where user can monitor or control the data.

INTRODUCTION TO ARDUINO
 Arduino is an open-source electronics platform based on easy-to-use hardware
and software.
 Arduino boards are able to read inputs - light on a sensor, a finger on a button,
or a Twitter message - and turn it into an output - activating a motor, turning
on an LED, publishing something online.
 You can tell your board what to do by sending a set of instructions to the
microcontroller on the board.
 To do so you use the Arduino programming language (based on Wiring), and the
Arduino Software (IDE) , based on Processing.
 Over the years Arduino has been the brain of thousands of projects, from everyday
objects to complex scientific instruments.
 A worldwide community of makers - students, hobbyists, artists, programmers,
and professionals - has gathered around this open-source platform, their
contributions have added up to an incredible amount of accessible knowledge that
can be of great help to novices and experts alike.
 Arduino was born at the Ivrea Interaction Design Institute as an easy tool for
fast prototyping, aimed at students without a background in electronics and
programming.
 As soon as it reached a wider community, the Arduino board started changing to
adapt to new needs and challenges, differentiating its offer from simple 8-bit
boards to products for IoT applications, wearable, 3D printing, and embedded
environments.

Why Arduino?
 Due to its simple and accessible user experience, Arduino has been used in
thousands of different projects and applications.
 The Arduino software is easy-to-use for beginners, yet flexible enough for
advanced users.
 It runs on Mac, Windows, and Linux.
 Teachers and students use it to build low cost scientific instruments, to prove
chemistry and physics principles, or to get started with programming and robotics.
 Designers and architects build interactive prototypes, musicians and artists
use it for installations and to experiment with new musical instruments.
 Makers, of course, use it to build many of the projects exhibited at the Maker Faire,
for example. Arduino is a key tool to learn new things. Anyone - children,
hobbyists, artists, programmers - can start tinkering just following the step by step
instructions of a kit, or sharing ideas online with other members of the Arduino
community.
 There are many other microcontrollers and microcontroller platforms available
for physical computing.
 Parallax Basic Stamp, Netmedia's BX-24, Phidgets, MIT's Handyboard, and
many others offer similar functionality.
 All of these tools take the messy details of microcontroller programming and wrap
it up in an easy-to-use package.
 Arduino also simplifies the process of working with microcontrollers, but it offers
some advantage for teachers, students, and interested amateurs over other
systems:

1. Inexpensive - Arduino boards are relatively inexpensive compared to


other microcontroller platforms. The least expensive version of the
Arduino module can be assembled by hand, and even the pre-assembled
Arduino modules cost less than \$50
2. Cross-platform - The Arduino Software (IDE) runs on Windows,
Macintosh OSX, and Linux operating systems. Most microcontroller
systems are limited to Windows.
3. Simple, clear programming environment - The Arduino Software (IDE) is
easy-to-use for beginners, yet flexible enough for advanced users to take
advantage of as well. For teachers, it's conveniently based on the Processing
programming environment, so students learning to program in that
environment will be familiar with how the Arduino IDE works.
4. Open source and extensible software - The Arduino software is
published as open-source tools, available for extension by experienced
programmers. The language can be expanded through C++ libraries, and
people wanting to understand the technical details can make the leap from
Arduino to the AVR C programming language on which it's based.
Similarly, you can add AVR-C code directly into your Arduino programs if
you want to.
5. Open source and extensible hardware - The plans of the Arduino boards
are published under a Creative Commons license, so experienced circuit
designers can make their own version of the module, extending it and
improving it. Even relatively inexperienced users can build the breadboard
version of the module in order to understand how it works and save money.

TYPES OF ARDUINOS

 Arduino board was designed in the Ivrea Interaction Design Institute intended for
students without a background in electronics and programming concepts.
 This board started altering to adapt to new requirements and challenges, separating
its presence from simple 8-bit boards to products for IoT (Internet of Things)
applications, 3D printing, wearable, and embedded surroundings.
 All boards are entirely open-source, allowing users to build them separately and
finally adapt them to their exact needs.
 Over the years the different types of Arduino boards have been used to build
thousands of projects, from daily objects to compound scientific instruments. An
international community of designers, artists, students, programmers, hobbyists,
and experts has gotten together around this open-source stage, their donations have
added up to an unbelievable amount of available knowledge that can be of immense
help to beginners and specialists alike.

What are the Types of Arduino Boards?

 Arduino board is an open-source platform used to make electronics projects. It


consists of both a microcontroller and a part of the software or Integrated
Development Environment (IDE) that runs on your PC, used to write & upload
computer code to the physical board.
 The platform of an Arduino has become very famous with designers or students just
starting out with electronics, and for an excellent cause.

Types of Arduino Boards


 Unlike earlier programmable circuit boards, the Arduino does not require a separate
part of hardware in order to program a new code onto the board you can just use a
USB cable.
 As well, the Arduino IDE uses a basic version of C++, making it simpler to learn the
program.
 At last, the Arduino board offers a typical form factor that breaks out the functions
of the microcontroller into a more available package.

Why Arduino Boards?

 Arduino board has been used for making different engineering projects and
different applications.
 The Arduino software is very simple to use for beginners, yet flexible adequate for
advanced users.
 It runs Windows, Linux, and Mac.
 Teachers and students in the schools utilize it to design low-cost scientific
instruments to verify the principles of physics and chemistry.
 There are numerous other microcontroller platforms obtainable for physical
computing.
 The Netmedia’s BX-24, Parallax Basic Stamp, MIT’s Handyboard, Phidget, and many
others present related functionality.
 Arduino also makes simpler the working process of microcontroller, but it gives
some advantages over other systems for teachers, students, and beginners.
 Inexpensive
 Cross-platform
 The simple, clear programming environment
 Open source and extensible software
 Open source and extensible hardware
The Function of the Arduino Board

 The flexibility of the Arduino board is enormous so that one can do anything
they imagine.
 This board can be connected very easily to different modules such as obstacle
sensors, presence detectors, fire sensors, GSM Modules GPS modules, etc.
 The main function of the Arduino board is to control electronics through reading
inputs & changing it into outputs because this board works like a tool.
 This board is also used to make different electronics projects in the field of
electronics, electrical, robotics, etc.

Features of Different Types of Arduino Boards

 The features of different types of Arduino boards are listed in the tabular form.
Arduino Board Processor Memory Digital I/O Analogue I/O

16Mhz 2KB SRAM, 32KB


Arduino Uno 14 6 input, 0 output
ATmega328 flash

84MHz 96KB SRAM,


Arduino Due 54 12 input, 2 output
AT91SAM3X8E 512KB flash

16MHz 8KB SRAM, 256KB


Arduino Mega 54 16 input, 0 output
ATmega2560 flash

16MHz 2.5KB SRAM, 32KB


Arduino Leonardo 20 12 input, 0 output
ATmega32u4 flash

Different Types of Arduino Boards

 The list of Arduino boards includes the following such as


 Arduino Uno (R3)
 Arduino Nano
 Arduino Micro
 Arduino Due
 LilyPad Arduino Board
 Arduino Bluetooth
 Arduino Diecimila
 RedBoard Arduino Board
 Arduino Mega (R3) Board
 Arduino Leonardo Board
 Arduino Robot
 Arduino Esplora
 Arduino Pro Mic
 Arduino Ethernet
 Arduino Zero
 Fastest Arduino Board

Arduino Uno (R3)

 The Uno is a huge option for your initial Arduino.


 This Arduino board depends on an ATmega328P based microcontroller.
 As compared with other types of arduino boards, it is very simple to use like the
Arduino Mega type board.
 It consists of 14-digital I/O pins, where 6-pins can be used as PWM (pulse width
modulation outputs), 6-analog inputs, a reset button, a power jack, a USB
connection, an In-Circuit Serial Programming header (ICSP), etc.
 It includes everything required to hold up the microcontroller; simply attach it to a
PC with the help of a USB cable and give the supply to get started with an AC-to-DC
adapter or battery.

Arduino Uno (R3)

 Arduino Uno is the most frequently used board and it is the standard form apart
from all the existing Arduino Boards.
 This board is very useful for beginners.

Arduino Nano

 This is a small board based on the microcontrollers like ATmega328P otherwise


ATmega628 but the connection of this board is the same as to the Arduino UNO
board.
 This kind of microcontroller board is very small in size, sustainable, flexible, and
reliable.
Arduino Nano

 As compared with the Arduino Uno board, it is small in size.


 The devices like mini-USB and Arduino IDE are necessary to build the projects.
 This board mainly includes analog pins-8, digital pins-14 with the set of an I/O
pin, power pins-6 & RST (reset) pins-2.

Arduino Micro

 The Arduino Micro board mainly depends on the ATmega32U4 based


Microcontroller that includes 20-sets of pins where the 7-pins are PWM pins, 12-
analog input pins.
 This board includes different components like an ICSP header, RST button, small
USB connection, crystal oscillator-16MHz.
 The USB connection is inbuilt and this board is the shrunk version of the Leonardo
board.

Arduino Micro

Arduino Due

 This Arduino board depends on the ARM Cortex-M3 and it is the first Arduino
microcontroller board.
 This board includes digital I/O pins-54 where 12-pins are PWM o/p pins, analog
pins -12, UARTs-4, a CLK with 84 MHz, an USB OTG, DAC-2, a power jack, TWI-
2, a JTAG header, an SPI header, two buttons for reset & erase.
Arduino Due

 This board works with 3.3V where the highest voltage that the pins of input/output
can stand is 3.3V because providing a high voltage to any I/O pin can lead to damage
the board.
 This board is simply connected to a computer through a small USB cable
otherwise it can be powered through an AC to DC adapter.
 This Arduino Due board is suitable with all shields of Arduino at 3.3V.

LilyPad Arduino Board

 The Lily Pad Arduino board is a wearable e-textile technology expanded by Leah
“ Buechley” and considerately designed by “Leah and SparkFun”.
 Each board was imaginatively designed with huge connecting pads & a smooth back
to let them to be sewn into clothing using conductive thread.
 This Arduino also comprises of I/O, power, and also sensor boards which are built
especially for e-textiles. These are even washable!

LilyPad Arduino Boards

Arduino Bluetooth

 This Bluetooth mainly depends on the microcontroller like ATmega168 and this
board is also called Arduino BT.
 This kind of board includes different components like digital pins-16, analog pins-
6, crystal oscillator-16MHz, reset button, screw terminals, ICSP header.
 In this board, the screw terminals are mainly used for power.
 The programming of this Bluetooth microcontroller can be done with Bluetooth like
a wireless connection.
Arduino Diecimila

 The microcontroller board like Arduino Diecimila mainly depends on the


ATmega168.
 This board includes digital I/O pins -14 where 6-pins can be used like PWM
outputs & analog inputs-6, a USB connection, a crystal oscillator-16 MHz, an
ICSP header, a reset button & a power jack.
 This board can be connected to a computer through a USB cable and it can be
activated using a battery and an AC-DC adapter.

Arduino Diecimila

 As the name suggests, the meaning of Diecimila in Italian is 10,000 which means
that marks the truth that above 10k Arduino boards have been designed.
 In a set of USB Arduino boards, it is the latest one as compared with other
versions.
RedBoard Arduino Board

 The RedBoard Arduino board can be programmed using a Mini-B USB cable using
the Arduino IDE.
 It will work on Windows 8 without having to modify your security settings.
 It is more constant due to the USB or FTDI (Future Technology Devices
International) chip we used and also it is entirely flat on the back.
 Creating it is very simple to utilize in the project design. Just plug the board, select
the menu option to choose an Arduino UNO and you are ready to upload the
program.
 You can control the RedBoard over a USB cable using the barrel jack.
RedBoard Arduino Boards

Arduino Mega (R3) Board

 The Arduino Mega is similar to the UNO’s big brother.


 It includes lots of digital I/O pins (from that, 14-pins can be used as PWM o/ps),
6-analog inputs, a reset button, a power jack, a USB connection, and a reset
button.
 It includes everything required to hold up the microcontroller; simply attach it to a
PC with the help of a USB cable and give the supply to get started with an AC-to-DC
adapter or battery.
 The huge number of pins make this Arduino board very helpful for designing
projects that need a bunch of digital i/ps or o/ps like lots of buttons.

Arduino Mega (R3) Board

Arduino Leonardo Board

 The first development board of an Arduino is the Leonardo board.


 This board uses one microcontroller along with the USB.
 That means, it can be very simple and cheap also.
 Because this board handles USB directly, program libraries are obtainable
which let the Arduino board to follow a keyboard of the computer, mouse, etc.
Arduino Leonardo Board

Arduino Robot

 This kind of board is the first Arduino over wheels.


 This Arduino robot includes two processors on each of its boards.
 The two boards are the motor board and control board where the motor board
controls the motors & the control board is used to read the sensors for operating.
 Every board is a complete Arduino board and its programming can be done through
the Arduino IDE.
 These are microcontroller boards that depend on the ATmega32u4.
 The pins of this Robot are mapped to actuators and sensors onboard.
 The process of programming the robot is the same as the Arduino Leonardo
board.
 It is also named a small computer and it is extensively used in robotics.
 This board includes the speaker, color screen, buttons-5, motors-2, a digital
compass, an SD card reader, potentiometers-2 & floor sensors-5.
 The library of this robot can be used for controlling the sensors as well as the
actuators.
Arduino Esplora

 The Arduino Esplora includes a small computer known as a microcontroller


including a number of inputs & outputs.
 The inputs of this board are a light sensor, four buttons, a microphone, an
accelerometer, joystick, a slider, a temperature sensor, etc whereas the
outputs are a 3 color LED, a buzzer.
 This kind of Arduino board looks like a videogame controller.

Arduino Esplora

 The programming of this board can be done using Arduino Software like IDE
which takes the data from the inputs and controls the output like a keyboard or a
mouse.
 As compared with all other types of Arduino boards, this esplora is totally different
because the inputs, as well as outputs, are connected to the board already.
 So connecting the components like actuators or sensors is very simple.
 Thus, programming is somewhat different as compared with other types of Arduino
boards.
 This esplora board includes its own library so that the data from the sensors &
actuators are very easy to read and write.
Arduino Pro Mic

 The Arduino Pro Micro board is the same as the Arduino Mini board apart from the
ATmega32U4 Microcontroller.
 This pro mic board includes digital I/O pins-12, pulse width modulation (PWM)
pins-5, serial connections of Tx & Rx &10-bit ADC.
Arduino Ethernet

 The Arduino Ethernet board depends on the microcontroller like ATmega328.


 This kind of microcontroller board includes analog pins-5, digital I/O pins-14, RST
button, an RJ45 connection, crystal oscillator, a power jack, ICSP header, etc.
 The connection of the Arduino board can be done through the Ethernet shield to the
internet.
Arduino Zero

 This is a powerful as well as simple 32-bit board and it provides the best platform
for innovative projects like wearable technology, smart IoT devices, crazy
robotics, high-tech automation, etc.
 This board expands by providing improved performance, permitting a range of
project opportunities & performs like a great educational tool.

Arduino Zero

 This board includes analog input pins-6, digital I/O pins-14, a power jack, AREF
button(Analog Reference), UART port pins, a USB connector & an In-Circuit Serial
Programming (ICSP) header, a power header, etc.
 This board is power-driven through the SAMD21 microcontroller based on Atmel.
 The main feature of this is EDBG (Embedded Debugger) based on Atmel and it
provides a complete debug interface without using extra hardware.

Fastest Arduino Board

 Designing one of the best Arduino development boards that are familiar with
Arduino MEGA & UNO is the hifive1 board that includes a 320 MHz RISC-V
microcontroller unit.
 This kind of fastest board has Cortex M-7 with a 400 MHz microcontroller unit.
 Flash memory – upto 2Mbytes
 RAM – 1 Mbyte
 DMA controllers -4
 Communication peripherals- Up to 35
 16-bit Max Resolution with 3× ADCs
 D/A converters with 2× 12-bit
 Hardware with JPEG Codec
 Timers -22 & Watchdogs – 200Mhz
 HW Calendar & RTC with Sub-second Accuracy
 Cryptographic Acceleration

Hifive1 Board Features


 The features of the Hifive1 board include the following.
 Mega Form Factor or Arduino Uno
 Charger for Battery
 SD Card including Detection of Card Feature
 Ethernet*
 Optional QSPI Flash – 133Mhz
 Interfaces – CAN, SWD, Camera
 USB- OTG

The Arduino Shields

 Additionally, Arduino shields are pre-built circuit boards used to connect to a


number of Arduino boards.
 These shields fit on the top of the Arduino compatible boards to provide additional
capabilities like connecting to the internet, motor controlling, providing wireless
communication, LCD screen controlling, etc.
 The different types of Arduino shields are

Shields for Arduino Boards

 Wireless Shields
 The GSM Shield
 The Ethernet Shield
 The Proto Shields

Arduino Boards Comparison

The comparison between different Arduino boards includes the following.


Arduino Voltage CLK Digital Analog Interface of
PWM UART
Board of System Speed I/O Inputs Programming

Arduino Uno – 5V 1 USB through


16MHz 14 14 6
R3 ATMega16U2

Arduino Uno 5V 1 USB through


16MHz 14 14 6
R3 SMD ATMega16U2

5V 1 USB through
RedBoard 16MHz 14 14 6
FTDI

FTDI-
Arduino Pro 3.3V 1
8MHz 14 14 6 Compatible
3.3V/8MHz
Header

FTDI-
Arduino Pro 5V 1
16MHz 14 14 6 Compatible
5V/16MHz
Header

FTDI-
Arduino Mini 5V 1
16MHz 14 14 8 Compatible
05
Header

Arduino Pro FTDI-


Mini 3.3V 8MHz 14 14 6 1 Compatible
3.3V/8MHz Header

Arduino Pro FTDI-


Mini 5V 16MHz 14 8 6 1 Compatible
5V/16MHz Header

FTDI-
Arduino 5V 1
16MHz 14 6 6 Compatible
Ethernet
Header

FTDI-
Compatible
Arduino Fio 3.3V 8MHz 14 8 6 1 Header or
Wirelessly via
XBee†

LilyPad 3.3V 8MHz 14 6 6 1 FTDI-


Arduino 328 Compatible
Main Board Header

LilyPad FTDI-
Arduino 3.3V 8MHz 9 4 5 0 Compatible
Simple Board Header

ARDUINO TOOLCHAIN

Arduino Hardware/Software Interface

 When programming the Arduino (or anything else), it is possible to write some
pretty complex instructions and get the Arduino to do some really cool things.
 The problem is that a microcontroller (like the ATmega328 on the Uno) can only
execute simple, low-level instructions.
 These simple instructions include things like add variable a to variable b or take
variable b and put it in register x.
 And, to complicate matters, microcontrollers only speak in binary.
 For those who don’t know, binary numbers are long strings of 1s and 0s.

Arduino and other microcontrollers only understand binary.


 The statement here may make total sense to you if you’ve been working with
Arduino for a while.
 Or, if not, it may not make any sense at all.
 Either way, just go with me here for a minute.
 This is just an illustration which does not require complete understanding of the
code.
Int Temp = analogRead(sensorPin);
If (Temp > 500)

{
//more complex instructions go here
}
 How does a complex statement like this one actually run on a microcontroller that
only knows basic arithmetic, logic, moving and shifting?
 How do these statements translate into the ones and zeros that the microcontroller
can actually understand?
 Microcontrollers are electronic devices, so to speak to one you need to send
electronic signals.
 That’s where binary numbers come into play.
 A zero represents low or off, usually 0 V or close to it, while a one represents high or
on, usually either about 5 V or 3.3 V, depending on the processor.
 A good way to think of binary numbers and digital signals is like a single pole light
switch.
 The light is either on or its off, there is nothing in between.
 Zero is off and one is on.

A good way to think about digital signals.


 Going from complex instructions to simple ones the microcontroller understands
requires several layers of software that translate the high-level operations into
simpler instructions.

How the Arduino Compiler Works


 Enter the compiler. Compiling a program in Arduino is referred to as verifying.
 The terms mean the same thing, so throughout these tutorials we’ll use them
interchangeably.
 The compiler first transforms the code you write into assembly language.
 The name of the compiler we’ll be using on our Uno is AVR-GCC.
 If you’re new to this, that may sound kind of weird but try not to obsess over it. It’s
just a name.
 The assembler, which come with the IDE with the compiler, then translates the
assembly language program into machine language.
 It then creates object files, which combine machine language, data, and information
it needs to place instructions properly in memory.
 Often, the assembler creates multiple files which will eventually be put together.
 This is where the linker — another part of the compiler software package — shines.
 The linker will take all the independently assembled machine language programs
and object files and put them together.
 This produces a .hex file that the microprocessor can understand and run.
 The two figures below, though they apply to C/C++ programming in general, are a
good illustration of this process.

Arduino code compilation process. The figure below is another way to view it.
Another way to visualize the Arduino code compilation process.

 Another piece of software, called AVR Dude (for Downloader UploaDEr) starts when
we press the upload button.
 This software sends the .hex file to the ATMega328 on the Arduino board.
 On the chip resides the bootloader.
 This bootloader was put there on purpose by the folks at Arduino and works with
AVR Dude to get the. hex into the flash memory on the chip.
 All of this happens very quickly and seamlessly behind the scenes of the Arduino
IDE.
How the compiled code transfers to the Arduino board.

ARDUINO PROGRAMMING STRUCTURE

 Here, we will study in depth, the Arduino program structure and we will learn more new
terminologies used in the Arduino world.
 The Arduino software is open-source.
 The source code for the Java environment is released under the GPL (GNU General
Public License)and the C/C++ microcontroller libraries are under the LGPL.
Sketch − The first new terminology is the Arduino program called “sketch”.
Structure

 Arduino programs can be divided in three main parts: Structure, Values (variables
and constants), and Functions.
 In this tutorial, we will learn about the Arduino software program, step by step, and
how we can write the program without any syntax or compilation error.
 Let us start with the Structure. 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.
 INPUT − -
 OUTPUT − -
 RETURN − -
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.
 INPUT − -
 OUTPUT − -
 RETURN − -

Pins – Input/Output from Pins Using Sketches

1. Arduino Uno(R3)
2. Arduino Mega (R3)
3. Arduino Nano
4. Arduino Leonardo
5. Arduino Due
6. LilyPad Arduino
7. Arduino Micro
8. Arduino Pro Mini

Arduino Uno (R3)

As we discussed we know that Arduino Uno is the most standard board available
and probably the best choice for a beginner. We can directly connect the board to the
computer via a USB Cable which performs the function of supplying the power as
well as acting as a serial port.

Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it
is used to give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated
from a voltage regulator on the board

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A5 are used as an analog input and it is in the range of
0-5V.

Digital Pins: The pins 0 to 13 are used as a digital input or output for the Arduino
board.

Serial Pins: These pins are also known as a UART pin. It is used for
communication between the Arduino board and a computer or other devices. The
transmitter pin number 1 and receiver pin number 0 is used to transmit and receive
the data resp.

External Interrupt Pins: This pin of the Arduino board is used to produce the
External interrupt and it is done by pin numbers 2 and 3.

PWM Pins: This pins of the board is used to convert the digital signal into an
analog by varying the width of the Pulse. The pin numbers 3,5,6,9,10 and 11 are
used as a PWM pin.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintain SPI
communication with the help of the SPI library. SPI pins include:

1. SS: Pin number 10 is used as a Slave Select


2. MOSI: Pin number 11 is used as a Master Out Slave In
3. MISO: Pin number 12 is used as a Master In Slave Out
4. SCK: Pin number 13 is used as a Serial Clock

LED Pin: The board has an inbuilt LED using digital pin-13. The LED glows only
when the digital pin becomes high.

AREF Pin: This is an analog reference pin of the Arduino board. It is used to
provide a reference voltage from an external power supply.

Arduino Mega (R3)

The most important thing about this board is that the board has more input-output
pins so it is very beneficial for the Advanced Users or the people who want more
pins for their projects.

Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it
is used to give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated
from a voltage regulator on the board

GND: This pin of the board is used to ground the Arduino board.
Reset: This pin of the board is used to reset the microcontroller It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A15 are used as an analog input and it is in the range
of 0-5V. The analog pins on this board can be used as a digital Input or Output pins.

Serial pins: It is used for communication between the Arduino board and a
computer or other devices.

The TXD and RXD are used to transmit & receive the serial data resp. It includes
serial 0, Serial 1, serial 2, Serial 3 as follows:

1. Serial 0: It consists of Transmitter pin number 1 and receiver pin number 0


2. Serial 1: It consists of Transmitter pin number 18 and receiver pin number 19
3. serial 2: It consists of Transmitter pin number 16 and receiver pin number 17
4. Serial 3: It consists of Transmitter pin number 14 and receiver pin number 15

External Interrupts pins: This pin of the Arduino board is used to produce the
External interrupt and it is done by the pin numbers 0,3,21,20,19,18.

I2C: This pin of the board is used for I2C communication.

1. Pin number 20 signifies Serial Data Line (SDA)and it is used for holding the
data.
2. Pin number 21 signifies Serial Clock Line (SCL) and it is used for offering
data synchronization among the devices.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintainSPI
communication with the help of the SPI library. SPI pins include:

1. MISO: Pin number 50 is used as a Master In Slave Out


2. MOSI: Pin number 51 is used as a Master Out Slave In
3. SCK: Pin number 52 is used as a Serial Clock
4. SS: Pin number 53 is used as a Slave Select

LED Pin: The board has an inbuilt LED using digital pin-13. The LED glows only
when the digital pin becomes high.

AREF Pin: This is an analog reference pin of the Arduino board. It is used to
provide a reference voltage from an external power supply.
Arduino Nano

The Arduino Uno and nano are similar, but the only difference is that its size. The
UNO size is 2 times the nano size, so the Arduino nano is more breadboard friendly.
It is used for portable projects. The board has a mini USB cable slot.

Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it
is used to give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated
from a voltage regulator on the board

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.
Analog Pins: The pins A0 to A7 are used as an analog input and it is in the range of
0-5V.

Digital Pins: The pins D0 to D13 are used as a digital input or output for the
Arduino board.

Serial Pins: This pin is also known as a UART pin. It is used for communication
between the Arduino board and a computer or other devices. The transmitter pin
number 1 and receiver pin number 2 is used to transmit and receive the data resp.

External Interrupt Pins: This pin of the Arduino board is used to produce the
External interrupt and it is done by pin numbers 2 and 3.

PWM Pins: This pins of the board is used to convert the digital signal into an
analog by varying the width of the Pulse. The pin numbers 3,5,6,9,10 and 11 are
used as a PWM pin.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintainSPI
communication with the help of the SPI library. SPI pins include:

1. SS: Pin number 10 is used as a Slave Select


2. MOSI: Pin number 11 is used as a Master Out Slave In
3. MISO: Pin number 12 is used as a Master In Slave Out
4. SCK: Pin number 13 is used as a Serial Clock

I2C: This pin of the board is used for I2C communication.

1. Pin A4 signifies Serial Data Line (SDA)and it is used for holding the data.
2. Pin A5 signifies Serial Clock Line (SCL) and it is used for offering data
synchronization among the devices.

LED Pin: The board has an inbuilt LED using digital pin-13. The LED glows only
when the digital pin becomes high.

AREF Pin: This is an analog reference pin of the Arduino board. It is used to
provide a reference voltage from an external power supply.

Arduino Leonardo
The Arduino Leonardo has more number of digital input/ output and analog input
pins. The Arduino Leonardo can be powered via the micro USB connection or with
an external power supply.

Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it
is used to give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated
from a voltage regulator on the board

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A11 are used as an analog input and it is in the range
of 0-5V.
Digital Pins: The pins 4, 6, 8, 9, 10, and 12 are used as a digital input or output for
the Arduino board.

Serial Pins: This pin is also known as a UART pin. It is used for communication
between the Arduino board and a computer or other devices. The transmitter pin
number 1 and receiver pin number 0 is used to transmit and receive the data resp.

External Interrupt Pins: This pin of the Arduino board is used to produce the
External interrupt and it is done by pin numbers 2 and 3.

I2C: This pin of the board is used for I2C communication.

1. Pin number 2 signifies Serial Data Line (SDA)and it is used for holding the
data.
2. Pin number 3 signifies Serial Clock Line (SCL) and it is used for offering data
synchronization among the devices.

LED Pin: The board has an inbuilt LED using digital pin-13. The LED glows only
when the digital pin becomes high.

AREF Pin: This is an analog reference pin of the Arduino board. It is used to
provide a reference voltage from an external power supply.

Arduino Due

Arduino Due is more preferable when there are many peripherals that need to
connect the board. This board has many numbers of PWM and ADC outputs so it
can be more beneficial to use the Due board where you will need more PWM and
ADC pins. It is the perfect board for powerful larger scale Arduino projects like
designing complex systems like CNC or 3D printer.
Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it
is used to give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated
from a voltage regulator on the board

IOREF: It stands for Input-Output voltage REFerence. It allows the shields to check
the operating voltage of the board.

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A11 are used as an analog input and it is in the range
of 0-5V.
Digital Pins: The pins 0 to 53 are used as a digital input or output for the Arduino
board.

PWM Pins: This pins of the board is used to convert the digital signal into an
analog by varying the width of the Pulse. The pin numbers 2 to 13 are used as PWM
pins.

SPI Pins: This pin is also known as a UART pin. It is used for communication
between the Arduino board and a computer or other devices. The transmitter pin and
receiver pin are used to transmit and receive the data resp.

I2C Communication: This pin of the board is used for I2C communication.

1. Serial Data Line (SDA): It is used for holding the data.


2. Serial Clock Line (SCL): It is used for offering data synchronization among
the devices.

Voltage for ADC: This pin of the Arduino board is used to map the voltage value to
the integer value. The voltage from 0 to 5 is mapped into the integer value from 0 to
1023.

Erase Button: This pin of the board is used to erase the Flash Memory of the
microcontroller. To erase, on the power of the board press and hold the Erase button
for a few seconds.

LilyPad Arduino

Arduino Lilypad is very unique in its shape and applications among the other
Arduino boards. This Arduino Lilypad is based on the circular PCB with the wide
holes at the corner and is optimized for the e-textiles and wearable projects. The
Arduino Lilypad does not have built-in USB to UART converter as it is present in
other Arduino modes.
VCC: This pin of the Arduino board is connected to +5V or +3.3V for providing
supply to the board

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A5 are used as an analog input and it is in the range of
0-5V.

Digital Pins: The board contains 14 digital pins that can be used as an input or
output.

Serial Pins: This pin is also known as a UART pin. It is used for communication
between the Arduino board and a computer or other devices. The transmitter and are
used to transmit and receive the data resp.

PWM: These pins of the board are used to convert the digital signal into an analog
by varying the width of the Pulse. The pin numbers 9, 10, 15, 16, and 17 are used as
PWM pins.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintainSPI
communication with the help of the SPI library. SPI pins include:

1. SS: Pin number 16 is used as a Slave Select


2. MOSI: Pin number 17 is used as a Master Out Slave In
3. MISO: Pin number 18 is used as a Master In Slave Out
4. SCK: Pin number 19 is used as a Serial Clock

I2C Communication: This pin of the board is used for I2C communication.

1. Serial Data Line (SDA): It is used for holding the data.


2. Serial Clock Line (SCL): It is used for offering data synchronization among
the devices.

Arduino Micro

Arduino Micro is the smallest board in the Arduino Community. The Arduino Micro
has more number of analog input pins than the UNO board. It is essentially a
shrunk-down version of the Arduino Leonardo

Image Source: javatpoint.com


Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it
is used to give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated
from a voltage regulator on the board

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A11 are used as an analog input and it is in the range
of 0-5V.

Digital Pins: The pins 4, 6, 8, 9, 10, and 12 are used as a digital input or output for
the Arduino board.

External Interrupt Pins: This pin of the Arduino board is used to produce the
External interrupt and it is done by pin number 0, 1, 2, and 3.

PWM Pins: This pins of the board is used to convert the digital signal into an
analog by varying the width of the Pulse. The pin numbers 3, 5, 6, 9, 10, 11, and 13
are used as PWM pins.

Serial Pins: This pin is also known as a UART pin. It is used for communication
between the Arduino board and a computer or other devices. The transmitter pin
number 1 and receiver pin number 0 is used to transmit and receive the data resp.

I2C: This pin of the board is used for I2C communication.

1. Pin number 2 signifies Serial Data Line (SDA)and it is used for holding the
data.
2. Pin number 3 signifies Serial Clock Line (SCL) and it is used for offering data
synchronization among the devices.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintainSPI
communication with the help of the SPI library. SPI pins include:
1. SS: It is used as a Slave Select
2. MOSI: It is used as a Master Out Slave In
3. MISO: It is used as a Master In Slave Out
4. SCK: It is used as a Serial Clock

LED Pin: The board has an inbuilt LED using digital pin-13. The LED glows only
when the digital pin becomes high.

AREF Pin: This is an analog reference pin of the Arduino board. It is used to
provide a reference voltage from an external power supply.

Arduino Pro Mini

The Arduino Pro mini has the new pin called the RAW pin. The RAW PIN is the
input to the on-board regulator. You can connect up to 12V to the RAW pin and
VCC will remain at a constant voltage. This Arduino board is preferred by advanced
users for greater flexibility and small size.

Vin: This is the input voltage pin of the Arduino board used to provide input supply
from an external power source.
VCC: This pin of the Arduino board is connected to +5V or +3.3V for providing
supply to the board

GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets
the microcontroller.

Analog Pins: The pins A0 to A7 are used as an analog input and it is in the range of
0-5V.

Digital Pins: The pins 2 to 13 are used as a digital input or output for the Arduino
board.

External Interrupt Pins: This pin of the Arduino board is used to produce the
External interrupt and it is done by the pin number 4 and 5

PWM Pins: This pin of the board is used to convert the digital signal into an analog
by varying the width of the Pulse. The pin numbers 3, 5, 6,9,10, and 12 are used as a
PWM pin.

Analog Comparator: Pin number 6 -AIN0 and pin number 7- AIN1 are connected
to the internal comparator.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintainSPI
communication with the help of the SPI library. SPI pins include:

1. SS: Pin number 10 is used as a Slave Select


2. MISO: Pin number 11 is used as a Master In Slave Out
3. MOSI: Pin number 12 is used as a Master Out Slave In
4. SCK: Pin number 13 is used as a Serial Clock

Arduino Sketch: Structure and Flow

Introduction

Arduino IDE is an open-source software for writing code and uploading it to the
Arduino board. It runs on different operating systems such as Windows, Mac OS
X, and Linux. The environment is written in Java and is based on Processing and
other open-source software.
Arduino Sketch
Arduino sketch is the name that Arduino uses for a program. It’s the unit of code
that is uploaded to and run on an Arduino board. A basic Arduino sketch consists
of two functions:

 setup()
 loop()

The purpose of these functions will be explained later in the tutorial.


For now, open the Arduino IDE and click on the File tab. Then, click on New (or
press Control + N on your keyboard) to have a look at the two functions.

void setup()
{

void loop()
{
}
Arduino

Writing a Sketch

In this example, we will write a sketch, i.e. create a program in Arduino IDE that
will display the text Hello World on the screen. We will use the serial monitor
window to write our message.

Serial communication is very useful for controlling electronic components or


devices that are connected to (interfaced with) the Arduino board from the
computer. You can use it for debugging (finding errors in) Arduino programs when
writing new programs.

To write in the serial monitor, you must first initialize the serial monitor, you can
do it with the help of the following statement:
Serial.begin(9600);
Here 9600 is the baud rate at which you are communicating over USB with your
device.
The statement to write on Serial Monitor is:
Serial.println(“Your Message”);
Now, modify the code by initializing the serial monitor and writing the print
statement inside the setup() function. You will end with this code:
// Hello World Program

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello World");
}

void loop() {
// put your main code here, to run repeatedly:
}
Arduino
Save your sketch by going to the File tab and then clicking on Save.

How to Run the Sketch

Follow the steps below to run the sketch:

1. Connect your evive to your computer using a USB cable.


2. Click the Upload button to load the program to the Arduino.
3. Now open the Arduino IDE Serial Monitor Window to see the sketch run and
print the text message. The text that the program shows should be visible in the
serial monitor window.

Arduino Sketch Program Flow

In an Arduino sketch, program statements (individual lines of code) are executed,


i.e. run from top to bottom. This top-to-bottom execution of statements can be
altered only by flow control statements.
There are a few things to notice in your sketch:

1. void setup(): It is the function initialization/declaration process of the function


named setup(). As the function does not return any value, it is initialized with the
keyword void, meaning empty.
2. Serial.begin(9600);
Serial.println(“Hello World”);
These statements are present in the setup function’s body.
3. { is the opening brace of the functions that tells that all statements starting from
here are inside the functions.
4. } is the closing brace of the function.
5. ; is used to terminate the statement.
In the Hello World sketch, statements in the setup() function will run first, from
top to bottom. The statement Serial.begin(9600); is the first statement in
the setup() function, so it is the first to run. This statement sets up the speed of the
serial port to 9600 baud. The baud setting in the serial monitor window must match
this value so that the evive and serial monitor window can communicate at the
same speed.
The second statement to run in the setup() function is Serial.println(“Hello,
world!”);, which sends the text Hello World out for display in the serial monitor
window. In this statement, any text can be put between the opening and closing
quotes (” “) and it will be displayed in the serial monitor window.

The setup() Function

Statements in the setup() function are executed only once when the sketch is run
(which you must have noticed in the Hello World sketch).

The loop() Function

Statements in the loop() function will run continuously from top to bottom and
then back to the top.
If the loop() function contains two statements, the first statement will be executed
first, then the second statement, then the first statement again and so on. Hence, the
statements in the main loop will be executed continuously until the Arduino is
switched off or reset.
In our Hello World sketch, since there are no statements in the loop() function,
program execution ends up in the loop and gets stuck there, doing nothing.
It is important to have the loop() function in the sketch, even if it is empty because
without it the microcontroller on the Arduino board will try to execute whatever it
finds next in memory after the statements in the setup() function have been
executed. But the loop() function prevents it from doing so by keeping the program
execution in the loop.
Below is an example sketch that demonstrates the main loop execution. Copy the
code to your Arduino IDE, upload the code to your evive, and start Serial Monitor.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("I am in the setup() function");
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("I am executing the first statement in the loop() function");
delay(1000);
Serial.println("I am in the loop() function");
delay(1000);
Serial.println("I am executing the last the statement in the setup() function");
}
Arduino

The delay() function in the statement delay(1000); introduces a waiting period of 1


second

This is what you’ll get in the serial monitor:


The text in the setup() function is displayed only once when the serial monitor
window is first opened and then the Arduino is reset. After this, program execution
enters the loop() function and repeatedly executes the statements in the loop from
top to bottom and back to the top again in a never-ending loop.

Arduino IDE: Variables

Introduction

A variable is used to store a value or information so that we can refer to or/and


manipulate it at a later stage during the life of the Arduino sketch. Memory is set
aside for storing the variable and the variable is given a name which allows us to
access it in the sketch.
Types of variables

Below are some variable types that are frequently used in Arduino sketches:

Size in
Datatype Description
Bytes

char 1 Byte It stores 8 bit numerical ASCII value of characters like alphabets,
symbols etc. It can also store a signed number that is in range of -128 to
127. Character literals are written in single quotes like 'a', '#' etc and
their ASCII numerical is stored at corresponding variable location.

unsigned 1 Byte It can store 8 bit numerical ASCII values of characters, symbols etc and
char can also store any unsigned number in range of 0 to 255. Character
literals are written in single quotes like 'a', '#' etc and their ASCII
numerical is stored at corresponding variable location.

int 2 Stores a 2 byte(16 bits) signed integer value that is in range of -32,768
Bytes to 32,767.

unsigned 2 Stores an unsigned integer value of 2 bytes(16 bits) that is in range of 0


int Bytes to 65,536.

long 4 Stores a 4 byte (32 bit) signed integer value that is in range of -
Bytes 2,147,483,648 to 2,147,483,647.

unsigned 4 Stores an unsigned 4 byte(32 bit) integer that is in range of 0 to


long Bytes 4,294,967,295 (2^32 - 1).

float 4 Stores a signed 4 byte(32-bit) value that is integer or a value with


Bytes decimal point (say 12.15) that is in range of -3.4028235E+38 to
3.4028235E+38.

double 4 Same as float for evive.


Bytes

Defining a Variable

The standard form of variable definition is:


Variable_Datatype Varible_Name;
Variable_Datatype can be int or float depending on the type of variable you
want. Variable_Name is the name of the variable. The variable is referenced or
used by its name later in the program.
By giving the variable a type and name, space is made available in memory for this
variable.
E.g. if you want to count the number of times the main loop is executed, you must
first define a variable count as shown below:
int count;

How to Name a Variable

You can give a variable any name as long as it sticks to the rules set out below. It
is best to give variables meaningful names that help you and others understand the
sketch better.

1. Variables can consist of both uppercase (A-Z) and lowercase(a-z) letters.


2. Variables can contain numbers 0 to 9, but cannot start with a number.
3. Variables may not have the same names as Arduino language keywords, e.g. you
cannot have a variable named float.
4. Variables must have unique names, i.e. you cannot have two variables with the
same name.
5. Variable names are case-sensitive, so Count and count are
two different variables.
6. Variables may not contain any special characters, except the underscore (_).

Assigning Value to a Variable

You can assign a value to a variable with the following statement:


Variable_Name = Expression;
The expression will give a valid number that will be assigned to the variable. Let’s
count how many times the loop is executed. Below is the Arduino sketch:
//This code prints number of times for which the loop functions has run.
int count;

void setup() {
Serial.begin(9600);
count = 0;
}

void loop() {
count = count + 1;
Serial.print("Loop has been executed ");
Serial.print(count);
Serial.println(" times.");
delay(1000);
}
Arduino

Let’s make a new sketch, where we’ll add two float variables to a new variable.
Below is the code:
float Num1 = 10.00456;
float Num2 = 5.52401;
float Result;

void setup() {
Serial.begin(9600);
Serial.print("The first number is ");
Serial.println(Num1);
Serial.print("The second number is ");
Serial.println(Num1);
Result = Num1 + Num2;
Serial.print("Sum with 2 digit precision is ");
Serial.println(Result);
Serial.print("Sum with 5 digit precision is ");
Serial.println(Result, 5);
}

void loop() {

}
Arduino
When the value of the variable is sent to the serial monitor
window, println() automatically rounds the number off to two decimal places, i.e.
the number of digits after the decimal.
The second time that println() is used to send the value of the variable to the serial
monitor window, the number of decimal places is specified as 5. This is done by
passing a second parameter value of 5 to the println() function.

Arduino IDE: Arithmetic Operators


Arduino IDE is capable of doing basic arithmetic operations. Arduino has the
following five arithmetic operators for basic mathematical operations:

Operators Operator Symbol Example

Addition + a = b + 2017

Subtraction - b = 2017 - a

Multiplication * d = 2.5 * e

Division / e = d / 2.5

Remainder % f = d % 2.5

Addition

The addition operator is used for adding two numbers. Here is an example:
int Num1 = 15;
int Num2 = 3;
int Sum;

void setup(){
Serial.begin(9600);
Sum = Num1 + Num2;
Serial.print("Addition of Num1 and Num2 is ");
Serial.println(Sum);
}

void loop() {
}
Arduino

Serial Monitor Response: Addition of Num1 and Num2 is 18


In the code above, two variables are defined, namely Num1 and Num2. A value is
assigned to both of them as well. The third variable, i.e. Sum is defined and no
value is assigned to it; hence, it contains a random number. This variable is used to
store the sum of Num1 and Num2; this value will overwrite the random value
when you assign it to Sum. After statement 5 is executed, the Sum will have a
value of 18.

Subtraction

The subtraction operator subtracts one number from another. It is represented by


the plus sign (+). Given below is an example:
int Num1 = 15;
int Num2 = 3;
int Result;

void setup(){
Serial.begin(9600);
Result = Num1 - Num2;
Serial.print("Subtraction of Num1 and Num2 is ");
Serial.println(Result);
}

void loop() {
}
Arduino

Serial Monitor Response: Subtraction of Num1 and Num2 is 12


The result of this operation is 12.

Multiplication

The multiplication operator multiplies one number with another. It is represented


by the asterisk (*). Given below is an example:
int Num1 = 15;
int Num2 = 3;
int Result;

void setup(){
Serial.begin(9600);
Result = Num1 * Num2;
Serial.print("Multiplication of Num1 and Num2 is ");
Serial.println(Result);
}

void loop() {
}
Arduino

Serial Monitor Response: Multiplication of Num1 and Num2 is 15


The result of this operation is 45.

Division

The division operator divides one number by another. It is represented by the


division sign (/). Given below is an example:
int Num1 = 15;
int Num2 = 3;
int Result;

void setup(){
Serial.begin(9600);
Result = Num1 / Num2;
Serial.print("Division of Num1 and Num2 is ");
Serial.println(Result);
}

void loop() {
}
Arduino

Serial Monitor Response: Division of Num1 and Num2 is 5


The result of this operation is 5.
There is, however, one more thing you should know about division. Till now we
have used division only for integers and have got an integer as a result. But what if
the result is a floating-point number (numbers with a decimal) and not an integer?
Let’s find out with the help of the example given below:
int Num1 = 6;
int Num2 = 5;
int Result;

void setup(){
Serial.begin(9600);
Result = Num1 / Num2;
Serial.print("Division of Num1 and Num2 is ");
Serial.println(Result);
}

void loop() {
}
Arduino

Serial Monitor Response: Division of Num1 and Num2 is 1


The result will be 1 because the numbers after the decimal point are discarded
when the result is stored in the variable because its data type is int. However, if we
use float as the data type of the variable to store the result, we get the correct
result.
int Num1 = 6;
int Num2 = 5;
float Result;

void setup(){
Serial.begin(9600);
Result = (float) Num1 / Num2;
Serial.print("Division of Num1 and Num2 is ");
Serial.println(Result);
}

void loop() {
}
Arduino

Serial Monitor Response: Division of Num1 and Num2 is 1.20


When using constant values in calculations that store the result in a floating point
variable, use a decimal point and a zero for whole numbers, e.g. 5.0 instead of 5.

Remainder

The remainder operator calculates the remainder after one number is divided by
another number. It is represented by the percentage sign (%). Given below is an
example:
int Num1 = 6;
int Num2 = 5;
int Result;

void setup(){
Serial.begin(9600);
Result = Num1 % Num2;
Serial.print("Remainder of Num1 and Num2 is ");
Serial.println(Result);
}

void loop() {
}
Arduino

Serial Monitor Response: Remainder of Num1 and Num2 is 1


The result of this operation is 1.

Arduino IDE: Boolean or Logical Operators


Logical operators evaluate either one or two relational or logical statements. There
are 3 logical operators in Arduino IDE:
Logical Operator Operator Symbol Example

OR || a || b

AND && a && b

NOT ! !a

Logic OR (||) Operator

Structure

(Statement 1) || (Statement2)
The logic OR operator results in true if either Statement1 or Statement2 or both are
true. If both the statements are false, then it will result in false. Below is its truth
table:

Statement1 Statement2 Statement1 || Statemen2

0 0 0

0 1 1

1 0 1

1 1 1
Below is an example showing how to use the logic OR operator:
int Result1;
int Result2;
int Result3;
int Result4;

void setup() {
Serial.begin(9600);
Result1 = (5 < 3) || (6 > 5);
Result2 = (5 < 3) || (6 > 5);
Result3 = (5 > 3) || (6 < 5);
Result4 = (5 < 3) || (6 < 5);

Serial.print("Result 1: ");
Serial.println(Result1);
Serial.print("Result 2: ");
Serial.println(Result2);
Serial.print("Result 3: ");
Serial.println(Result3);
Serial.print("Result 4: ");
Serial.println(Result4);
}

void loop() {

}
Arduino

Copy

Serial monitor output:


Result 1: 1
Result 2: 1
Result 3: 1
Result 4: 0

Logic AND (&&) Operator


Structure

(Statement 1) && (Statement2)


The logic AND operator gives true only if both Statement 1 and Statement 2 are
true. If either Statement1 or Statement2 or both are false, then it will result in false.
Below is the truth table:

Statement1 Statement2 Statement1 || Statemen2

0 0 0

0 1 0

1 0 0

1 1 1
Below is an example showing how to use the logic AND operator:
int Result1;
int Result2;
int Result3;
int Result4;

void setup() {
Serial.begin(9600);

Result1 = (5 > 3) && (6 > 5);


Result2 = (5 < 3) && (6 > 5);
Result3 = (5 > 3) && (6 < 5);
Result4 = (5 < 3) && (6 < 5);

Serial.print("Result 1: ");
Serial.println(Result1);
Serial.print("Result 2: ");
Serial.println(Result2);
Serial.print("Result 3: ");
Serial.println(Result3);
Serial.print("Result 4: ");
Serial.println(Result4);
}

void loop() {

}
Arduino

Serial monitor output:


Result 1: 1
Result 2: 0
Result 3: 0
Result 4: 0

Logic NOT (!) Operator

Structure

! Statement
The NOT operator checks whether the Statement evaluates to 0 or not. If it is 0 it
results in true; otherwise, it results in false.
Below is an example showing how to use the NOT operator:
int Result1;
int Result2;
void setup() {
Serial.begin(9600);

Result1 = !(5 > 3);


Result2 = !(5 < 3);

Serial.print("Result 1: ");
Serial.println(Result1);
Serial.print("Result 2: ");
Serial.println(Result2);
}

void loop() {

}
Arduino

Serial monitor output:


Result 1: 0
Result 2: 1

Arduino IDE: Comparison or Relational Operator


A relational operator is an operator that tests a relation between two entities. The
result of a relational operator is either true or false.
In programming, true is represented by ‘1’, and false is represented by ‘0’.
There are 6 relational operators in Arduino IDE:

Relational Operators Operator Symbol Example

Greater than > currentYear > 2015

Less than < 2015 < currentYear

Greater than or equal to >= currentYear >= 2017

Less than or equal to <= 2017 <= currentYear

Equal to == centuryYear == 2000

Not equal to != centuryYear != 1999

Greater than (>)

It results in true only if the first number is greater than the second number. If the
first number is equal to or less than the second, it results in a false. Given below is
an example:
int Result1;
int Result2;

void setup() {
Serial.begin(9600);
Result1 = (7 > 4);
Result2 = (7 > 10);
Serial.print("Is 7 greater than 4: ");
Serial.println(Result1);
Serial.print("Is 7 greater than 10: ");
Serial.println(Result2);
}

void loop() {
}
Arduino

The result of the operation will be:


Is 7 greater than 4: 1
Is 7 greater than 10: 0

Less than (<)

It results in true only if the first number is less than the second number. If the first
number is equal to or greater than the second, it results in a false. Given below is
an example:
int Result1;
int Result2;

void setup() {
Serial.begin(9600);
Result1 = (7 < 4);
Result2 = (7 < 10);
Serial.print("Is 7 less than 4: ");
Serial.println(Result1);
Serial.print("Is 7 less than 10: ");
Serial.println(Result2);
}

void loop() {
}
Arduino

The result of the operation is:


Is 7 less than 4: 0
Is 7 less than 10: 1

Greater than or equal to (>=)

It results in true if the first number is either greater than or equal to the second
number. If the first number is less than the second, it results in a false. Given below
is an example:
void setup() {
Serial.begin(9600);
Result1 = (7 >= 10);
Result2 = (7 >= 7);
Serial.print("Is 7 greater than or equal to 10: ");
Serial.println(Result1);
Serial.print("Is 7 greater than or equal to 7: ");
Serial.println(Result2);
}

void loop() {
}
Arduino

The result of the operation is:


Is 7 greater than or equal to 10: 0
Is 7 greater than or equal to 7: 1

Less than or equal to (<=)


It results in true if the first number is either less than or equal to the second
number. If the first number is greater than the second, it results in a false. Given
below is an example:
int Result1;
int Result2;

void setup() {
Serial.begin(9600);
Result1 = (7 <= 4);
Result2 = (7 <= 7);
Serial.print("Is 7 less than or equal to 4: ");
Serial.println(Result1);
Serial.print("Is 7 less than or equal to 7: ");
Serial.println(Result2);
}

void loop() {
}
Arduino

The result of the operation is:


Is 7 less than or equal to 4: 0
Is 7 less than or equal 7: 1

Equal to (==)

It results in true only if the first number is equal to the second number; otherwise, it
results in false. Given below is an example:
int Result1;
int Result2;
void setup() {
Serial.begin(9600);
Result1 = (7 == 10);
Result2 = (7 == 7);
Serial.print("Is 7 equal to 10: ");
Serial.println(Result1);
Serial.print("Is 7 equal to 7: ");
Serial.println(Result2);
}

void loop() {
}
Arduino

The result of the operation is:


Is 7 equal to 10: 0
Is 7 equal to 7: 1

Not equal to (!=)

It results in true if the first number is not equal to the second number; otherwise, it
results in false. Given below is an example:
int Result1;
int Result2;

void setup() {
Serial.begin(9600);
Result1 = (7 != 10);
Result2 = (7 != 7);
Serial.print("Is 7 not equal to 10: ");
Serial.println(Result1);
Serial.print("Is 7 not equal to 7: ");
Serial.println(Result2);
}

void loop() {
}
Arduino

The result of the operation is:


Is 7 not equal to 10: 1
Is 7 not equal to 7: 1

Arduino IDE: Conditional (if-else-if) Statements


Conditional statements check whether a programmer-specified Boolean condition
is true or false. They make it possible to test a variable against a value/compare a
variable with another variable and make the program act in one way if the
condition is met, and another if it isn’t. They make the program very powerful and
be able to be used for a vast variety of purposes.
This tutorial discusses the following conditional statements:

 if statement
 if-else statement
 if-else-if statement.

The if statement

Given below is the structure of an if statement:


If (conditional expression) {
Body of the if statement
}
The conditional expression can be anything that can result either in true or false. If
the statement is true, the code in the body of the statement is executed. However, if
the expression turn out as false, the code in the body is simply skipped.
Below is an example showing how to use the if statement:
int Student1_Marks = 92;
int Student2_Marks = 20;

void setup() {
Serial.begin(9600);

if (Student1_Marks > 33){


Serial.println("The student1 marks is greater than 33. He passed the exam.");
}
if (Student1_Marks <= 33){
Serial.println("The student1 marks is less than or eqaul to 33. He failed the
exam."); } if (Student2_Marks > 33){
Serial.println("The student2 marks is greater than 33. He passed the exam.");
}
if (Student2_Marks <= 33){
Serial.println("The student2 marks is less than or eqaul to 33. He failed the
exam.");
}
}

void loop() {

}
Arduino
Result on the Serial Monitor:
The student1 marks is greater than 33. He passed the exam.
The student2 marks is less than or eqaul to 33. He failed the exam.
In the code above, Student1 has marks greater than 33; hence, the first statement is
true and is executed. For Student2, the second statement is true; hence, the second
statement is executed.

The if-else statement


When using an if statement, the code in its body runs only when the if statement
evaluates to true. If it evaluates to false, program execution skips the code in the
body of the if statement and goes to the statement the body of the if statement.
By adding an else statement, the code in the body of the else statement will run,
but only when its corresponding if statement evaluates to false.
If (conditional expression) {
Body of the if statement when the conditional expression is true
}
else {
Body of the else statement when the conditional expression is false
}
When the conditional expression evaluates to true:

 Code in the body of the if statement runs.


 Code in the body of the else statement does not run.

When the conditional expression evaluates to false:

 Code in the body of the if statement does not run.


 Code in the body of the else statement runs.

Below is an example showing how to use the if-else statement:


int Student1_Marks = 92;
int Student2_Marks = 20;

void setup() {
Serial.begin(9600);

if (Student1_Marks > 33){


Serial.println("The student1 marks is greater than 33. He passed the exam.");
}
else{
Serial.println("The student1 marks is less than or eqaul to 33. He failed the
exam.");
}

if (Student2_Marks > 33){


Serial.println("The student2 marks is greater than 33. He passed the exam.");
}
else{
Serial.println("The student2 marks is less than or eqaul to 33. He failed the
exam.");
}
}

void loop() {

}
Arduino
Result on the Serial Monitor:
The student1 marks is greater than 33. He passed the exam.
The student2 marks is less than or eqaul to 33. He failed the exam.

The if-else-if statement

The if-else-if statement allows more than one conditional expression to be


evaluated than the if-else statement.
Below is the basic structure:
if (conditional expression 1) {
Body of the if statement when conditional expression 1 is true
}
else if (conditional expression 2) {
Body of the else-if statement when conditional expression 1 is false and
conditional expression 2 is true
}
else {
Body of the else statement when conditional expression 1 and 2 are both false
}
When conditional expression 1 evaluates to true:

 Code in the body of the first if statement runs.


 Codes in the body of the else-if statement and else statement doesn’t run.

When conditional expression 1 evaluates to false and conditional expression


2 evaluates to true:
 Code in the body of the else-if statement runs.
 Codes in the body of the if statement and else statement doesn’t run.

When both conditional expression 1 and 2 evaluate to false:

 Code in the body of the else statement runs.


 Codes in the body of the if statement and if-else statement don’t run.

Below is an example showing how to use the if-else-if statement:


int Marks = 95;
char Grade;

void setup() {
Serial.begin(9600);

if (Marks <= 33){


Grade = 'E';
}
else if (Marks <= 40){
Grade = 'D';
}
else if (Marks <= 60){
Grade = 'C';
}
else if (Marks <= 80){
Grade = 'B';
}
else if (Marks <= 100){
Grade = 'A';
}

Serial.print("Your grade is: ");


Serial.println(Grade);
}

void loop() {

}
Arduino
Result on the Serial Monitor:
Your grade is: A

Arduino IDE: Increment & Decrement Operator

Increment Operator

The increment operator is an Arduino arithmetic operator that increments an


integer variable by a value of one. This is useful in certain types of loops.
Two possible structures of increment operator:

 Variable_Name++: As the ‘++’ sign is after the variable name, it is a post-


increment operation. This means that the variable is first used in the
statement and incremented after the statement execution.
 ++Variable_Name: As the ‘++’ sign is before the variable name, it is a pre-
increment operation. This means that the variable is
incremented before the execution of the statement.

Example showing the working of the post-increment operation:


int count = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Count Value: ");
Serial.println(count++);
delay(1000);
}
Arduino
Example showing the working of the pre-increment operation:
int count = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Count Value: ");
Serial.println(++count);
delay(1000);
}
Arduino
Decrement Operator

The decrement operator is used to decrement an integer variable by a value of one.


Two possible structures of increment operator:

 Variable_Name – -: As the ‘–‘ sign is after the variable name, it is a post-


decrement operation. This means that the variable is first used in the
statement and decremented after the statement execution.
 – – Variable_Name: As the ‘–‘ sign is before the variable name, it is a pre-
decrement operation. This means that the variable is
decremented before the execution of the statement.

Example of post-decrement operation:


int count = 100;
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Count Value: ");
Serial.println(count--);
delay(1000);
}
Arduino

Example of pre-decrement operation:


int count = 100;

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Count Value: ");
Serial.println(--count);
delay(1000);
}
Arduino

Arduino IDE: for Loop


A loop statement allows us to execute a statement or group of statements multiple
times.
Below is the general form of a loop statement in most programming languages :
The for loop

A for loop executes statements a predetermined number of times. The control


expression for the loop is initialized, tested, and manipulated within the for loop
parentheses. It is easy to debug the looping behavior of the structure because it is
independent of the activity inside the loop.
Each for loop has up to three expressions, which determine its operation. The
following example shows the general for loop syntax in Arduino IDE. Notice that
the three expressions in the for-loop argument parentheses are separated with
semicolons.
for (initialisation; control statement; increment or decrement) {
Body of for loop
}
Below is the flow chart showing how a for loop works:
Example:
void setup() {
Serial.begin(9600);
for (int i = 0; i < 10; i++){
Serial.print("Value of i is: ");
Serial.println(i);
}
}

void loop() {
}
Arduino IDE: while Loop
While loops execute a statement, or a group of statements continuously, and
infinitely, until the expression inside the parenthesis, () results in false. Something
must change the tested variable inside the body of the while loop or the control of
the program will never exit the while loop. When you don’t know how many times
the loop should execute, you should go for a while loop.

Structure

The following example shows the general while loop syntax in Arduino IDE. The
control expression, i.e. the condition is written inside the parentheses. Unlike
the for loop, the while loop has only one expression within its parentheses, and the
looping behavior depends on the activity inside the loop’s body.
while (condition) {
Body of while loop
}
Below is the flowchart showing how a while loop works:
Example

int i = 0;

void setup() {
Serial.begin(9600);
while (i < 10) {
Serial.print("Value of i is: ");
Serial.println(i);
i++;
}
}

void loop() {
}
Link for these notes:
Note: https://ai.thestempedia.com/docs/evive/evive-arduino-ide-tutorials/

INTRODUCTION TO ARDUINO SHIELDS

What is Arduino Shields?

 Arduino shields are the boards, which are plugged over the Arduino board to
expand its functionalities.

 There are different varieties of shields used for various tasks, such as Arduino motor
shields, Arduino communication shields, etc.

 Shield is defined as the hardware device that can be mounted over the board to
increase the capabilities of the projects.

 It also makes our work easy.

 For example, Ethernet shields are used to connect the Arduino board to the Internet.
 The pin position of the shields is similar to the Arduino boards.

 We can also connect the modules and sensors to the shields with the help of the
connection cable.

 Arduino motor shields help us to control the motors with the Arduino board.

Why do we need Shields?

 The advantages of using Arduino shields are listed below:

 It adds new functionalities to the Arduino projects.


 The shields can be attached and detached easily from the Arduino board. It does
not require any complex wiring.
 It is easy to connect the shields by mounting them over the Arduino board.
 The hardware components on the shields can be easily implemented.

Types of Shields

 The popular Arduino shields are listed below:


 Ethernet shield
 Xbee Shield
 Proto shield
 Relay shield
 Motor shield
 LCD shield
 Bluetooth shield
 Capacitive Touchpad Shield

Ethernet shield

 The Ethernet shields are used to connect the Arduino board to the Internet.

 We need to mount the shield on the top of the specified Arduino board.

 The USB port will play the usual role to upload sketches on the board.

 The latest version of Ethernet shields consists of a micro SD card slot.

 The micro SD card slot can be interfaced with the help of the SD card library.
 The Ethernet shield is shown below:

Xbee Shield

 We can communicate wirelessly with the Arduino board by using the Xbee Shield
with Zigbee.

 It reduces the hassle of the cable, which makes Xbee a wireless communication
model.

 The Xbee wireless module allows us to communicate outdoor upto 300 feet and
indoor upto 100 feet.

 The Xbee shield is shown below:

Proto shield
 Proto shields are designed for custom circuits.

 We can solder electronic circuits directly on the shield.

 The shield consists of two LED pads, two power lines, and SPI signal pads.

 The IOREF (Input Output voltage REFerence) and GND (Ground) are the two power
lines on the board.

 We can also solder the SMD (Surface Mount Device) ICs on the prototyping area. A
maximum of 24 pins can be integrated onto the SMD area

 The proto shield is shown below:

Relay shield

 The Arduino digital I/O pins cannot bear the high current due to its voltage and
current limits.

 The relay shield is used to overcome such situation.

 It provides a solution for controlling the devices carrying high current and voltage.

 The shield consists of four relays and four LED indicators.


 It also provides NO/NC interfaces and a shield form factor for the simple connection
to the Arduino board.

 The LED indicators depicts the ON/OFF condition of each relay.

 The relay used in the structure is of high quality.

 The NO (Normally Open), NC (Normally Closed), and COM pins are present on each
relay.

 The applications of the Relay shield include remote control, etc.

 The relay shield is shown below:

Motor shield

 The motor shield helps us to control the motor using the Arduino board.

 It controls the direction and working speed of the motor.

 We can power the motor shield either by the external power supply through the
input terminal or directly by the Arduino.

 We can also measure the absorption current of each motor with the help of the
motor shield.

 The motor shield is based on the L298 chip that can drive a step motor or two DC
motors.
 L298 chip is a full bridge IC.

 It also consists of the heat sinker, which increases the performance of the motor
shield.

 It can drive inductive loads, such as solenoids, etc.

 The operating voltage is from 5V to 12V.

 The applications of the motor shield are intelligent vehicles, micro-robots, etc.

 The Motor shield is shown below:

LCD shield

 The keypad of LCD (Liquid Crystal Display) shield includes five buttons called as up,
down, left, right, and select.

 There are 6 push buttons present on the shield that can be used as a custom menu
control panel.

 It consists of the 1602 white characters, which are displayed on the blue backlight
LCD.

 The LED present on the board indicates the power ON.

 The five keys present on the board helps us to make the selection on menus and
from board to our project.
 The LCD shield is popularly designed for the classic boards such as Duemilanove,
UNO, etc.

 The LCD shield is shown below:

Bluetooth shield

 The Bluetooth shield can be used as a wireless module for transparent serial
communication.

 It includes a serial Bluetooth module. D0 and D1 are the serial hardware ports in the
Bluetooth shield, which can be used to communicate with the two serial ports (from
D0 to D7) of the Arduino board.

 We can install Groves through the two serial ports of the Bluetooth shield called a
Grove connector.

 One Grove connector is digital, while the other is analog.

 The communication distance of the Bluetooth shield is upto 10m at home without
any obstacle in between.

 The Bluetooth shield is shown below:


Capacitive Touchpad shield

 It has a touchpad interface that allows to integrate the Arduino board with the touch
shield.

 The Capacitive touchpad shield consists of 12 sensitive touch buttons, which


includes 3 electrode connections and 9 capacitive touch pads.

 The board can work with the logic level of 3.3V or 5V.

 The capacitive shield is shown below:


Integrating sensors and actuators with an Arduino

 Integrating sensors and actuators with an Arduino board is a fundamental step in


creating IoT and embedded systems projects.

 Here's a general guide on how to do it:

1. Select Sensors and Actuators:


Choose the sensors and actuators that are appropriate for your
project. Common sensors include temperature, humidity, motion, light,
and distance sensors. Actuators can include motors, servos, LEDs, and
relays.

2. Gather Components:
Ensure you have all the necessary components, including the Arduino
board, sensors, actuators, breadboard, jumper wires, and any additional
modules required (e.g., motor drivers or relay modules).

3. Wiring:
Connect the sensors and actuators to the Arduino using jumper wires
and a breadboard if needed. Most sensors and actuators require power
(VCC), ground (GND), and data pins. Consult the datasheets or
documentation for specific wiring details.

4. Install Libraries:
Many sensors and actuators require Arduino libraries to simplify
communication. Install the required libraries using the Arduino IDE's
Library Manager. This will give you access to pre-written functions that
interact with the sensors and actuators.

5. Code:
Write the Arduino code to read data from sensors and control
actuators. Use the functions provided by the installed libraries to interface
with the components. You'll need to understand the datasheets and library
documentation to properly configure and use the sensors and actuators.

6. Test and Debug:


Upload the code to your Arduino board and test the setup. Monitor the
Serial Monitor in the Arduino IDE to see sensor readings or debug messages.
Make sure everything is functioning as expected.

7. Iterate and Refine:


It's common to encounter issues during the testing phase. Debug your
code and wiring if needed. Adjust sensor settings and calibration as required
to ensure accurate readings.

8. Integrate with IoT Platforms (Optional):


If your goal is to create an IoT project, you can integrate your Arduino
with IoT platforms like Blynk, ThingSpeak, or Adafruit IO. These platforms
allow you to remotely monitor and control your Arduino-based system using
the internet.

9. Enclosure and Mounting (Optional):

Depending on your project, you might want to enclose your Arduino


board, sensors, and actuators in a protective case. This is especially
important if your project will be deployed in real-world environments.

10.Documentation:
Document your wiring, code, and any setup instructions. This will be
helpful if you need to revisit the project in the future or if you want to share
your project with others.

PIR Sensor Interfacing with Arduino UNO


PIR Sensor

PIR sensor is used for detecting infrared heat radiations. This makes them useful in applications
involving detection of moving living objects that emit infrared heat radiations.

The output (in terms of voltage) of PIR sensor is high when it senses motion; whereas it is
low when there is no motion (stationary object or no object).
Connection Diagram of PIR Sensor with Arduino

Interfacing PIR Sensor with Arduino UNO

Note:

 PIR sensor: Never keep PIR Sensor close to the Wi-Fi antenna, ESP32, or Node MCU.
 PIR (Passive Infrared) sensor close to a WiFi antenna impacts the sensor's performance.
 PIR sensors detect changes in infrared radiation for motion detection.
 WiFi signals emit electromagnetic radiation that can interfere with the PIR sensor. Which
causes false detection.
 So always keep the PIR sensor and WiFi antenna as far apart as possible.
 Also, you can try to shield the PIR sensor from the WiFi signal. This can be done by
using metal shields or Faraday cages around the PIR sensor.

Detect Motion using PIR Sensor and Arduino Uno

Motion detection of living objects using PIR sensor using Arduino.


Upon detection of motion, "Object detected" is printed on serial monitor of Arduino. When
there is no motion, "No object in sight" is printed on serial monitor of Arduino.

PIR Sensor code for Arduino Uno

const int PIR_SENSOR_OUTPUT_PIN = 4; /* PIR sensor O/P pin */


int warm_up;
void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
Serial.begin(9600); /* Define baud rate for serial communication */
delay(20000); /* Power On Warm Up Delay */
}
void loop() {
int sensor_output;
sensor_output = digitalRead(PIR_SENSOR_OUTPUT_PIN);
if( sensor_output == LOW )
{
if( warm_up == 1 )
{
Serial.print("Warming Up\n\n");
warm_up = 0;
delay(2000);
}
Serial.print("No object in sight\n\n");
delay(1000);
}
else
{
Serial.print("Object detected\n\n");
warm_up = 1;
delay(1000);
}
}

Connection Diagram of PIR Sensor with Arduino Uno


Interrupt

PIR Sensor Interfacing with Arduino using interrupt


PIR Sensor code for Arduino Uno using Interrupt

const int PIR_SENSOR_OUTPUT_PIN = 2; /* PIR sensor O/P pin */

void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT_PULLUP);

attachInterrupt(digitalPinToInterrupt(2), pir, FALLING); /* Interrupt on


rising edge on pin 2 */
Serial.begin(9600); /* Define baud rate for serial communication */
delay(20000); /* Power On Warm Up Delay */
}

void loop() {
}
void pir(){
Serial.println("Object Detected");
}

Link for PIR Working: https://youtu.be/N9-0IhPIHng

Interfacing IR Sensor Module with Arduino

An infrared proximity sensor or IR Sensor is an electronic device that emits infrared lights to

sense some aspect of the surroundings and can be employed to detect the motion of an object. As

this is a passive sensor, it can only measure infrared radiation. This sensor is very common in the

electronic industry to design an obstacle avoidance robot or any other proximity detection-

based system.

IR Sensor Pinout
The IR sensor has a 3-pin connector that interfaces it to the outside world. The connections are as

follows:

VCC is the power supply pin for the IR sensor which we connect to the 5V pin on the Arduino.
OUT pin is a 5V TTL logic output. LOW indicates no motion is detected; HIGH means motion

is detected.

GND Should be connected to the ground of the Arduino.

How Does an IR Motion Sensor Module Work?


The working of the IR sensor module is very simple, it consists of two main components: the

first is the IR transmitter section and the second is the IR receiver section. In the transmitter

section, IR led is used and in the receiver section, a photodiode is used to receive infrared signal
and after some signal processing and conditioning, you will get the output.

An IR proximity sensor works by applying a voltage to the onboard Infrared Light Emitting

Diode which in turn emits infrared light. This light propagates through the air and hits an object,

after that the light gets reflected in the photodiode sensor. If the object is close, the reflected light
will be stronger, if the object is far away, the reflected light will be weaker. If you look closely

toward the module. When the sensor becomes active it sends a corresponding Low

signal through the output pin that can be sensed by an Arduino or any kind of

microcontroller to execute a particular task. The one cool thing about this module is that it

has two onboard LEDs built-in, one of which lights on when power is available and another one

turns on when the circuit gets triggered.

IR Motion Sensor Module – Parts


For most of the Arduino projects, this sensor is used to detect proximity or to build obstacle

avoidance robots. This Sensor is popular among beginners as these are low power, low cost,

rugged, and feature a wide sensing range that can be trimmed down to adjust the sensitivity.

This sensor has three pins two of which are power pins leveled VCC and GND and the other one

is the sense/data pin which is shown in the diagram above. It has an onboard power LED and a

signal LED the power LED turns on when power is applied to the board the signal LED turns on

when the circuit is triggered. This board also has a comparator Op-amp that is responsible for

converting the incoming analog signal from the photodiode to a digital signal. We also have a

sensitivity adjustment potentiometer; with that, we can adjust the sensitivity of the device. Last
and finally, we have the photodiode and the IR emitting LED pair which all together make the

total IR Proximity Sensor Module.

IR Sensor with Arduino UNO – Connection


Diagram
Now that we have a complete understanding of how an IR sensor works, we can connect all the

required wires to Arduino as shown below.

Connecting the IR sensor to any microcontroller is really simple. As we know this sensor outputs

a digital signal and processing this signal is very easy. There exist two methods to do so first,

you can always check the port in an infinite loop to see when the port changes its state from high

to low, or the other way is to do it with an interrupt if you are making a complicated project the

interrupt method is recommended. Power the IR with 5V or 3.3V and connect ground to ground.
Then connect the output to a digital pin D9. We have just used a Male to Female Jumper wire to

connect the IR sensor module with Arduino board as shown below.

Arduino Code for Interfacing IR Motion


Sensor Module with Arduino
The Arduino IR sensor module code is very simple and easy to understand. We are just

basically keeping track of whether the input to pin D9 is HIGH or LOW.


We initialize our code by declaring two global variables, the first one holds the pin value where

the IR sensor is connected and the second one holds the value where the LED is connected

Next, we have our setup function. In the setup function, we initialize the serial with 115200

baud. Next, we print a statement to check if the serial monitor window is properly working or

not, and then we initialize the IRSensor pin as input and the LED pin as output.

void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is working or
not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}

Next, we have our infinite loop. In the infinite loop, we first read the sensor pin with

the digitalRead() function and store the value to sensorStatus variable. Then we check to see if

the output of the sensor is high or low, if the output of the sensor is high that means no motion is

detected, else motion is detected, we also print this status in the serial monitor window.

void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
Serial.println("Motion Detected!"); // print Motion Detected! on the
serial monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Ended!"); // print Motion Ended! on the serial
monitor window
}
}

Link to learn More about Arduino:


https://www.electronicwings.com/arduino/pir-sensor-interfacing-with-
arduino-uno

Servo Motor Interfacing with Arduino Uno


Overview of Servo Motor

Servo Motor

A servo motor is an electric device used for precise control of angular rotation. It is used in
applications that demand precise control over motion, like in case of control of a robotic arm.

The rotation angle of the servo motor is controlled by applying a PWM signal to it.

By varying the width of the PWM signal, we can change the rotation angle and direction of
the motor.

Connection Diagram of Servo Motor with Arduino


Inter
facing Servo Motor With Arduino UNO
Servo Motor Example using Arduino

Controlling position of servo motor using a potentiometer.

Here, we will be using the Servo library that comes along with the Arduino IDE.

There are two examples in this library. We will be using those two examples.

To open the Knob example, go to File > Examples > Servo* > Knob

To open the Sweep example, go to File > Examples > Servo* > Sweep

Code For Servo Position Control Using Potentiometer and Arduino

/*

Controlling a servo position using a potentiometer (variable resistor)

*/

#include <Servo.h>

Servo myservo; /* create servo object to control a servo */

int potpin = 1; /* analog pin used to connect the potentiometer */

int val; /* variable to read the value from the analog pin */

void setup() {

Serial.begin(9600);

myservo.attach(9); /* attaches the servo on pin 9 to the servo object */

void loop() {

val = analogRead(potpin); /* reads the value of the potentiometer (value between 0 and
1023) */

Serial.print("Analog Value : ");


Serial.print(val);

Serial.print("\n");

val = map(val, 0, 1023, 0, 180); /* scale it to use it with the servo (value between 0 and 180)
*/

Serial.print("Mapped Value : ");

Serial.print(val);

Serial.print("\n\n");

myservo.write(val); /* sets the servo position according to the scaled value */

delay(1000); /* waits for the servo to get there */

Video of Servo Motor Control using Potentiometer and Arduino


https://youtu.be/sTw1Vo1yr58

Functions Used

Servo myservo

 This creates an object named myservo of the class Servo.

myservo.attach(pin)

 This function attaches the servo variable to a pin.


 Pin is the pin number to which the servo is attached.

myservo.write(angle)

 This function writes a value to the servo, thus controlling the position of the shaft.
 angle can take values between 0 to 180.
map(value, fromLow, fromHigh, toLow, toHigh)

 This function is used to map a number from one range to another range.
 This means that “value” having a value between “fromLow” to “fromHigh” gets
converted to equivalent values in the range of “toLow” to “toHigh”. “fromLow” gets
mapped to “toLow” and so on.

Code For Sweeping The Shaft Of Servo Back And Forth

/* Sweep

#include <Servo.h>

Servo myservo; /* create servo object to control a servo */

/* twelve servo objects can be created on most boards */

int pos = 0; /* variable to store the servo position */

void setup() {

myservo.attach(9); /* attaches the servo on pin 9 to the servo object */

void loop() {

for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree
*/

myservo.write(pos); /* tell servo to go to position in variable 'pos' */

delay(15); /* waits 15ms for the servo to reach the position */

for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */

myservo.write(pos); /* tell servo to go to position in variable 'pos' */

delay(15); /* waits 15ms for the servo to reach the position */

}
}

You might also like