Unit III Iot and Arduino Programming
Unit III Iot and Arduino Programming
ARDUINO
PROGRAMMING
Introduction to the Concept of IoT
Devices
• 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.
History of IOT
• 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.
• 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.
Arduino UNO
• Arduino UNO is based on an ATmega328P
microcontroller. It is easy to use compared
to other boards, such as the Arduino Mega
board, etc.
• The Arduino UNO includes 6 analog pin
inputs, 14 digital pins, a USB connector, a
power jack, and an ICSP (In-Circuit Serial
Programming) header.
• It is the most used and of standard form
from the list of all available Arduino Boards.
It is also recommended for beginners as it is
easy to use.
Click icon to add picture
Arduino Nano
Arduino Mega
• The Arduino Mega is based on ATmega2560
Microcontroller. The ATmega2560 is an 8-bit
microcontroller. We need a simple USB cable to
connect to the computer and the AC to DC adapter
or battery to get started with it. It has the advantage
of working with more memory space.
• The Arduino Mega includes 54 I/O digital pins and
16 Analog Input/Output (I/O), ICSP header, a reset
button, 4 UART (Universal Asynchronous
Reciever/Transmitter) ports, USB connection, and a
power jack.
Click icon to add picture
Arduino Micro
Arduino Leonardo
Arduino Due
Ex:
void loop() {
blinkLED(13); // Call the custom function
}
Conditional Statements
• Conditional statements allow you to make decisions based on
certain conditions (e.g., if, else, switch).
Ex:
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(ledPin, HIGH); // Turn LED on if button is pressed
} else {
digitalWrite(ledPin, LOW); // Turn LED off if button is not pressed
}
Loops
• Besides the loop() function, you can use for, while, and do-while loops for
repetitive tasks within the program.
Example of a for loop:
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
Libraries
• Arduino includes a lot of built-in libraries that provide
functionality for specific tasks, such as controlling motors,
sensors, displays, etc.
• Libraries are included at the top of the sketch using
#include.For example, to use an LCD display:
#include <LiquidCrystal.h> // Include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD with pin numbers
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.print("Hello, World!");
}
void loop() {
// No need to continuously update the display in this example
}
Comments
• In Arduino programming (and C/C++ in general), you can
add comments to your code to make it more readable and
easier to understand.
There are two types of comments:
• Single-line comments: Start with //. Everything after // on
that line is a comment.
• Multi-line comments: Start with /* and end with */.
Everything in between is a comment.
Commonly Used Functions
• pinMode(pin, mode): Configures a specified pin to either INPUT or
OUTPUT.
• digitalWrite(pin, value): Writes a HIGH or LOW value to a digital pin.
• digitalRead(pin): Reads the value from a digital pin (HIGH or LOW).
• analogWrite(pin, value): Writes an analog value (PWM) to a pin.
• analogRead(pin): Reads the analog value from a specified pin (0-1023).
• delay(ms): Pauses the program for a specified number of milliseconds.
IoT Protocols
• IoT protocols are the sets of rules that govern how devices
and systems in an IoT network communicate and
exchange data. They are designed to facilitate efficient
and reliable communication, especially in environments
with limited resources, like those often found in IoT
devices. Some common IoT protocols include MQTT, CoAP,
LoRaWAN, Zigbee, and Bluetooth.
MQTT(Message Queuing Telemetry
Transport)
• MQTT is a lightweight messaging protocol designed for
devices to communicate over the internet.
• Publisher-Subscriber Model: Devices (called publishers) send messages
about specific topics. Other devices (subscribers) receive those messages if
they are interested in those topics.
• Lightweight and Efficient: It’s designed for low-bandwidth and high-latency
networks, making it great for IoT devices that may not have a lot of power or
bandwidth.
• Broker: All messages go through a central server called a broker. The broker
manages the messages and ensures they are delivered to the right
subscribers.
• Use Cases: MQTT is commonly used in applications like smart home devices,
remote sensors, and any scenario where devices need to send data efficiently.
Coap: Constrained Application
Protocol
• CoAP, or Constrained Application Protocol, is a specialized
protocol designed for use in simple, resource-constrained
devices in IoT applications.
•Lightweight:
Overview: CoAP is designed for low-power devices and
networks with limited bandwidth. It uses less overhead
than more traditional protocols like HTTP.
•Request/Response Model: Similar to HTTP, CoAP follows a
request/response model. Devices (clients) send requests to
servers, which then respond with data.
• RESTful Architecture: CoAP supports RESTful principles, meaning it uses
standard methods like GET, POST, PUT, and DELETE to interact with resources
(like sensors or actuators).
• UDP-Based: CoAP typically runs over UDP (User Datagram Protocol), which is
simpler and faster than TCP, making it suitable for scenarios where reliability
can be traded off for speed.
• Multicast Support: CoAP can send messages to multiple devices
simultaneously (multicast), which is useful for managing groups of devices.
• Use Cases: It’s commonly used in smart home applications, industrial
automation, and sensor networks where devices need to communicate
efficiently.
HTTP(Hypertext Transfer
Protocol)
• HTTP, or Hypertext Transfer Protocol, is the foundational
protocol used for transmitting data over the web.
• Client-Server Model: HTTP operates on a client-server
model, where a client (like a web browser or an IoT device)
sends requests to a server, which processes the request
and sends back a response.
• Request/Response Structure: Each interaction follows a
request/response format. The client sends a request (like
asking for a web page or data), and the server responds
with the requested information.
• Stateless Protocol: HTTP is stateless, meaning each request
from a client to a server is treated as a new, independent
transaction. The server does not keep track of previous
interactions.
• Standard Methods: Common HTTP methods include:
• GET: Retrieve data from the server.
• POST: Send data to the server (like submitting a form).
• PUT: Update existing data on the server.
• DELETE: Remove data from the server.
• Flexible Data Formats: HTTP can transfer a variety of data
formats, including HTML, JSON, XML, images, and videos,
making it versatile for different applications.