Unit Iii Iot and Arduino Programming
Unit Iii Iot and Arduino Programming
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.
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 !
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.
2011 – Smart TV: The introduction of Smart TVs brought IoT to the living
room, enabling internet connectivity for streaming, app usage, and
interactive content.
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.
Connectivity
Data processing
Interface
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.
Temperature Sensors
Image Sensors
Obstacle Sensors
RF Sensor
IR Sensor
MQ-02/05 Gas Sensor
LDR Sensor
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.
Collect and Transmit Data : For this purpose sensors are widely used
they are used as per requirements in different application areas.
Characteristics of IoT
A device that is connected to another device right now may not be connected
in another instant of time.
Interconnectivity
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
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
Application Domains
Modern Applications
Smart cities
Healthcare
Earthquake detection
Smartphone detection
Traffic monitoring
Wearables
Security
Agriculture
Industry
Advantages of IoT
Improved efficiency and automation of tasks.
Improved decision-making.
Cost savings.
Disadvantages of IoT
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
IOT CONFIGURATIONS
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.
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:
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.
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.
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.
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.
Thoroughly test the entire IoT system before deployment. This includes
testing device connectivity, data flow, security measures, and the overall
functionality of the system.
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
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 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:
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.
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.
The features of different types of Arduino boards are listed in the tabular form.
Arduino Board Processor Memory Digital I/O Analogue I/O
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
Arduino Micro
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.
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!
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
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 Robot
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
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.
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
Wireless Shields
The GSM Shield
The Ethernet Shield
The Proto Shields
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
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 FTDI-
Arduino 3.3V 8MHz 9 4 5 0 Compatible
Simple Board Header
ARDUINO TOOLCHAIN
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.
{
//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.
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.
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 − -
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
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:
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.
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:
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.
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:
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. 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.
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.
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:
I2C Communication: This pin of the board is used for I2C communication.
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
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.
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.
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:
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()
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.
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.
Statements in the setup() function are executed only once when the sketch is run
(which you must have noticed in the Hello World sketch).
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
Introduction
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.
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.
Defining 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.
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.
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
Subtraction
void setup(){
Serial.begin(9600);
Result = Num1 - Num2;
Serial.print("Subtraction of Num1 and Num2 is ");
Serial.println(Result);
}
void loop() {
}
Arduino
Multiplication
void setup(){
Serial.begin(9600);
Result = Num1 * Num2;
Serial.print("Multiplication of Num1 and Num2 is ");
Serial.println(Result);
}
void loop() {
}
Arduino
Division
void setup(){
Serial.begin(9600);
Result = Num1 / Num2;
Serial.print("Division of Num1 and Num2 is ");
Serial.println(Result);
}
void loop() {
}
Arduino
void setup(){
Serial.begin(9600);
Result = Num1 / Num2;
Serial.print("Division of Num1 and Num2 is ");
Serial.println(Result);
}
void loop() {
}
Arduino
void setup(){
Serial.begin(9600);
Result = (float) Num1 / Num2;
Serial.print("Division of Num1 and Num2 is ");
Serial.println(Result);
}
void loop() {
}
Arduino
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
OR || a || b
NOT ! !a
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:
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
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);
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
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);
Serial.print("Result 1: ");
Serial.println(Result1);
Serial.print("Result 2: ");
Serial.println(Result2);
}
void loop() {
}
Arduino
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
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
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
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
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
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
if statement
if-else statement
if-else-if statement.
The if statement
void setup() {
Serial.begin(9600);
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.
void setup() {
Serial.begin(9600);
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.
void setup() {
Serial.begin(9600);
void loop() {
}
Arduino
Result on the Serial Monitor:
Your grade is: A
Increment Operator
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
void loop() {
Serial.print("Count Value: ");
Serial.println(count--);
delay(1000);
}
Arduino
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Count Value: ");
Serial.println(--count);
delay(1000);
}
Arduino
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/
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.
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.
Types of Shields
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 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.
Proto shield
Proto shields are designed for custom circuits.
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
Relay shield
The Arduino digital I/O pins cannot bear the high current due to its voltage and
current limits.
It provides a solution for controlling the devices carrying high current and voltage.
The NO (Normally Open), NC (Normally Closed), and COM pins are present on each
relay.
Motor shield
The motor shield helps us to control the motor using the Arduino board.
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.
The applications of the motor shield are intelligent vehicles, micro-robots, etc.
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 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.
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.
The communication distance of the Bluetooth shield is upto 10m at home without
any obstacle in between.
It has a touchpad interface that allows to integrate the Arduino board with the touch
shield.
The board can work with the logic level of 3.3V or 5V.
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.
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 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
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.
void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT_PULLUP);
void loop() {
}
void pir(){
Serial.println("Object Detected");
}
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.
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
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
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
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
}
}
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.
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
/*
*/
#include <Servo.h>
int val; /* variable to read the value from the analog pin */
void setup() {
Serial.begin(9600);
void loop() {
val = analogRead(potpin); /* reads the value of the potentiometer (value between 0 and
1023) */
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(val);
Serial.print("\n\n");
Functions Used
Servo myservo
myservo.attach(pin)
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.
/* Sweep
#include <Servo.h>
void setup() {
void loop() {
for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree
*/
for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */
}
}