Module 4
Introduction to Arduino, Installation, Implementation of IoT with
Arduino, Introduction to Raspberry Pi, Interfacing Raspberry Pi
with basic peripherals, Implementation of IoT with Raspberry Pi.
Introduction to Arduino
Arduino is an open-source electronics prototyping platform based on easy-to-use
hardware and software, designed to help students, hobbyists, engineers, and
researchers create interactive electronic projects. It acts as a small computer,
capable of sensing inputs and controlling outputs based on program logic.
Arduino combines:
1. Arduino Board (Hardware) – A physical microcontroller-based board.
2. Arduino IDE (Software) – Used to write, compile, and upload instructions to
the board.
Because Arduino is simple, affordable, and flexible, it is widely used in robotics,
automation, IoT, embedded systems, home automation, smart agriculture, and
research experiments.
What is Arduino
Arduino is a microcontroller-based open-source development board that allows
users to interact with electronic components like sensors, actuators, motors, LEDs,
displays, etc. Users write programs (called sketches) in the Arduino IDE and
upload them to the board, which executes the instructions continuously.
Key Features of Arduino
1. Open-source hardware and software
o Anyone can modify the board design or source code.
2. Easy programming using Arduino IDE
o Simple C/C++ style language.
3. Large community support
o Thousands of libraries and examples available.
4. Low cost & widely available
o Affordable for students and beginners.
5. Cross-platform
o Works on Windows, Linux, and macOS.
6. Supports sensors and IoT interfacing
o Many shields and modules available.
Arduino Board Components (Basic Hardware)
Typical Arduino boards, like the Arduino Uno, include:
Component Description
ATmega328p Microcontroller Brain of the board
USB connector Connects to PC for programming & power
Power jack External power supply
Digital I/O pins Interface for sensors, LEDs, motors
Analog input pins Accept analog signals (0-5V)
Reset button Restarts the program
Voltage regulator Maintains stable power
Crystal oscillator Provides clock signal to microcontroller
How Arduino Works
Arduino follows four major steps:
1. Connect Sensors or Inputs
o Example: temperature sensor, IR sensor, switch, etc.
2. Write the Code
o Code is written in Arduino IDE.
3. Upload the Program
o Program is transferred to the microcontroller via USB.
4. Generate Output
o Example: turn on LED, rotate motor, send data to LCD, Wi-Fi
transmission, etc.
Arduino Software (IDE)
Arduino IDE is an application used for programming Arduino boards.
It provides:
Text editor to write code
Console for error display
Upload & verify buttons
Built-in libraries
Basic Structure of Arduino Program
void setup() {
// Code here runs only once
}
void loop() {
// Code here runs repeatedly
}
Types of Arduino Boards
Some commonly used boards:
Arduino Board Features / Usage
Arduino Uno Beginner-friendly, most widely used
Arduino Mega 54 digital pins, robotics & advanced projects
Arduino Nano Small-sized, USB programming
Arduino Leonardo Built-in USB support
Arduino Due 32-bit processor, high performance
Arduino MKR Wi-Fi / ESP32 IoT & wireless communication
Applications of Arduino
Field Example Applications
Home Automation Smart lights, smart door lock, security alarms
IoT Projects Weather station, air quality monitor
Robotics Line-following robot, obstacle-avoiding robot
Agriculture Smart irrigation, soil moisture monitoring
Healthcare Pulse monitoring system
Industry Machine automation, data logging
Advantages of Arduino
Easy learning curve
Compatible with many sensors & modules
Real-time prototyping
Supports wireless technologies (Wi-Fi, Bluetooth, LoRa)
Limitations of Arduino
Limited memory compared to Raspberry Pi
Cannot run operating systems
Not suitable for high-processing tasks (e.g., ML, graphics)
Arduino is a powerful and beginner-friendly prototyping platform used across
education, research, automation, IoT, and embedded systems. It enables users to
build real-world smart systems through simple hardware-software integration.
1. Installation for Arduino IoT Projects
To implement IoT with Arduino, you need both hardware setup and software
setup.
A. Hardware Requirements
1. Arduino Board – Arduino Uno, Nano, or MKR Wi-Fi (for IoT projects).
2. Sensors – For example:
o Temperature and Humidity sensor (DHT11/DHT22)
o Gas sensors (MQ2, MQ135)
o Light sensor (LDR)
3. Actuators – LEDs, motors, relays, etc.
4. Communication Modules – For connecting to the Internet:
o Wi-Fi: ESP8266, ESP32
o Ethernet: Ethernet shield
o GSM: SIM900 module
5. Power supply – USB cable or battery pack.
6. Breadboard and jumper wires – For prototyping.
B. Software Requirements
1. Arduino IDE
o Download from [Link]
o Install and configure for your board.
2. Libraries
o Install required sensor libraries:
DHT sensor library
Adafruit MQTT library (for IoT cloud communication)
Wi-Fi library (ESP8266WiFi, WiFiNINA)
3. IoT Platform / Cloud
o Blynk, ThingSpeak, Adafruit IO, or Node-RED to monitor and control
data.
4. Drivers
o Install Arduino board drivers if necessary.
2. Implementation of IoT with Arduino
Implementing IoT projects with Arduino generally involves these steps:
Step 1: Connect the Hardware
Connect the Arduino to sensors and actuators.
Connect the communication module (Wi-Fi, Ethernet) for Internet access.
Example: Temperature sensor (DHT11) with ESP8266 Wi-Fi Module
DHT11 VCC → 5V Arduino
DHT11 GND → GND Arduino
DHT11 Data → Digital Pin 2 Arduino
ESP8266 Module → Arduino (TX/RX pins)
Step 2: Program Arduino
Open Arduino IDE.
Include necessary libraries:
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
Define sensor pins and Wi-Fi credentials:
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
DHT dht(DHTPIN, DHTTYPE);
Setup function: initialize Wi-Fi and sensor
void setup() {
[Link](9600);
[Link]();
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("Connected to Wi-Fi");
}
Loop function: read sensor and send data to cloud
void loop() {
float temp = [Link]();
float hum = [Link]();
if ([Link]() == WL_CONNECTED) {
HTTPClient http;
String serverPath = "[Link]
api_key=YOUR_API_KEY&field1=" + String(temp) + "&field2=" + String(hum);
[Link](serverPath);
int httpResponseCode = [Link]();
[Link]();
}
delay(15000); // Wait for 15 seconds before sending next data
}
Step 3: Set Up IoT Cloud
Create a free account on ThingSpeak / Blynk / Adafruit IO.
Create a channel or dashboard.
Get the API key for your device.
Program Arduino to send sensor readings using HTTP or MQTT protocol.
Step 4: Monitor and Control
On IoT cloud dashboard, you can:
o Monitor live sensor data
o Control actuators remotely (like turning on/off a fan or LED)
o Generate alerts or logs
Step 5: Test the System
Ensure Arduino is uploading correct data to the cloud.
Check for sensor accuracy and Wi-Fi connectivity.
Make adjustments to code or hardware connections if needed.
3. Advantages of Using Arduino for IoT
1. Cost-effective and beginner-friendly
2. Flexible for various sensors and communication modules
3. Large open-source community for support
4. Rapid prototyping for IoT projects
4. Example IoT Applications Using Arduino
Application Hardware Description
Smart Home Arduino + DHT11 + Monitors temperature and
Temperature Control Relay controls fan/AC
Arduino + MQ135 + Sends pollution data to cloud
Air Quality Monitoring
ESP8266 dashboard
Arduino + Soil Moisture Automatically waters plants
Smart Irrigation
Sensor + Relay based on soil moisture
Introduction to Raspberry Pi
Raspberry Pi is a small, low-cost, single-board computer developed by the
Raspberry Pi Foundation in the UK. It was created to promote computer science
education and digital skills, but over time, it has become widely used in IoT,
robotics, home automation, and embedded projects.
Unlike Arduino, which is a microcontroller, Raspberry Pi is a full-fledged
computer capable of running an operating system and performing complex tasks.
Key Features of Raspberry Pi
1. Low-cost and compact – Small credit-card size, affordable for students and
hobbyists.
2. Runs Linux-based OS – Most commonly Raspberry Pi OS, but supports
Ubuntu, Windows IoT, and more.
3. Multi-purpose – Can be used as a desktop computer, server, or IoT device.
4. GPIO pins – General Purpose Input/Output pins for interfacing with sensors,
actuators, and electronics.
5. Connectivity options – Ethernet, Wi-Fi, Bluetooth, HDMI, USB ports.
6. Supports programming languages – Python, C/C++, Java, Scratch, and more.
7. Community support – Huge online community with tutorials, forums, and
project ideas.
Hardware Overview of Raspberry Pi
A typical Raspberry Pi 4 board includes:
Component Description
CPU Quad-core ARM Cortex processor
RAM 2GB, 4GB, or 8GB variants
GPIO pins 40 pins for digital and analog interfacing
USB ports 2 × USB 3.0, 2 × USB 2.0
HDMI ports 2 micro-HDMI for display output
Ethernet port Wired internet connectivity
Wi-Fi & Bluetooth Wireless connectivity
MicroSD slot For OS installation and storage
Power port USB-C for 5V power supply
Camera & Display interfaces CSI and DSI connectors for camera and touchscreen
How Raspberry Pi Works
1. Install an Operating System on a microSD card (e.g., Raspberry Pi OS).
2. Power the board via USB-C or adapter.
3. Connect peripherals – Keyboard, mouse, monitor, network.
4. Boot the OS – Raspberry Pi runs like a normal PC.
5. Write programs – Use Python or other supported languages.
6. Interface with sensors and actuators via GPIO pins for IoT or embedded
projects.
Applications of Raspberry Pi
Field Example Applications
Education Learning programming, robotics, electronics
IoT Smart home automation, weather stations, security cameras
Robotics Line-following robots, obstacle avoidance robots
Media Home media server, retro gaming console
AI & ML Edge AI projects, image recognition
Industry Sensor monitoring, process automation
Advantages of Raspberry Pi
1. Can run full operating systems and applications.
2. Powerful computing for advanced IoT, AI, and ML applications.
3. Extensive community support and tutorials.
4. Flexible connectivity options for networking and cloud integration.
Limitations of Raspberry Pi
1. Cannot directly interface high-power electronics; needs additional circuits.
2. More expensive than microcontrollers for simple sensor projects.
3. Power supply required continuously.
4. Slower boot time compared to microcontrollers like Arduino.
Raspberry Pi is ideal for projects that require computation, networking, and IoT
integration, while Arduino is better for real-time control of electronics and sensors.
Often, they are used together in IoT projects for best results.
Introduction
Raspberry Pi can function as a standalone computer and also interface with
external devices for practical IoT and embedded projects. Peripherals are external
devices connected to Raspberry Pi for input, output, or storage purposes.
Basic peripherals include:
Keyboard & Mouse – Input devices
Monitor / Display – Output device
Camera – Input for images or video
Sensors & Actuators – Input and output for IoT
Storage devices – USB drives, external HDD/SSD
1. Keyboard and Mouse Interfacing
Purpose: Provides user input to the Raspberry Pi, similar to a PC.
Connection:
o Connect via USB ports.
o Wireless keyboard/mouse can be connected via Bluetooth or USB
dongle.
Usage: Typing commands, controlling GUI, programming, running
applications.
Steps:
1. Plug USB keyboard and mouse.
2. Boot Raspberry Pi.
3. Check if input devices are recognized (usually automatic in Raspberry Pi
OS).
2. Monitor / Display Interfacing
Purpose: Output visual interface for the Raspberry Pi OS or programs.
Connection:
o Use HDMI cable to connect monitor to Raspberry Pi.
o For small displays, use DSI interface (for official Raspberry Pi
touchscreen).
Usage: View GUI, dashboards, IoT monitoring graphs, or program output.
Steps:
1. Connect HDMI cable from Raspberry Pi to monitor.
2. Power on the monitor.
3. Boot Raspberry Pi and access desktop environment.
3. Camera Interfacing
Purpose: Capture images or video for projects like security systems or IoT
surveillance.
Connection:
o Use CSI (Camera Serial Interface) port on Raspberry Pi.
o Connect Raspberry Pi Camera Module ribbon cable.
Software Setup:
o Enable camera in Raspberry Pi configuration.
o Access camera via Python using picamera library or OpenCV.
Example Code (Python):
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(5) # Preview for 5 seconds
[Link]('/home/pi/[Link]')
camera.stop_preview()
4. Sensor Interfacing
Raspberry Pi GPIO pins allow direct connection with sensors.
Digital Sensors: PIR motion sensor, push buttons
Analog Sensors: Require ADC (Analog-to-Digital Converter) because
Raspberry Pi doesn’t have built-in ADC
Communication Protocols: I2C, SPI, UART
Example: Interfacing a Temperature Sensor (DHT11)
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4 # GPIO pin
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print(f'Temperature: {temperature}°C, Humidity: {humidity}%')
5. Actuator Interfacing
Actuators include LEDs, relays, motors, buzzers.
Connection: Use GPIO pins with proper resistors or motor drivers.
Example: LED Control
import [Link] as GPIO
import time
[Link]([Link])
[Link](17, [Link])
[Link](17, [Link]) # LED ON
[Link](1)
[Link](17, [Link]) # LED OFF
[Link]()
6. USB Storage Interfacing
Purpose: Store or retrieve data from Raspberry Pi.
Connection: Plug USB pen drive or external HDD into USB port.
Usage: Transfer files, logging sensor data, or running portable applications.
Key Points for Interfacing Peripherals
1. Check Power Requirements: Some peripherals may need external power.
2. Use Proper GPIO Pin Configuration: Avoid damaging Raspberry Pi by using
correct voltage (3.3V for GPIO pins).
3. Install Required Libraries: Python libraries like [Link], Adafruit_DHT,
OpenCV for camera.
4. Follow Communication Protocols: I2C, SPI, UART for sensors and
modules.
Applications of Peripheral Interfacing
Peripheral Application Example
Keyboard & Mouse Programming, GUI control
Monitor / Display IoT dashboards, visualization
Camera Surveillance, face detection
Sensors Temperature/humidity monitoring, smart home
Actuators Motor control, lighting automation
USB Storage Data logging, backup
Interfacing these basic peripherals allows Raspberry Pi to act as a full-fledged IoT
platform, capable of collecting data, processing it, and interacting with the physical
environment.
Introduction
Raspberry Pi is widely used in IoT (Internet of Things) because it can act as a
mini-computer capable of collecting data from sensors, processing it, and sending
it to the cloud or other devices for monitoring and control. Its GPIO pins, along
with internet connectivity, make it suitable for IoT applications such as smart
homes, agriculture, and industrial automation.
1. Hardware Requirements for IoT with Raspberry Pi
Component Purpose
Raspberry Pi Board (3/4 or
Main processing unit
Zero)
To collect data (temperature, humidity, gas, motion,
Sensors
etc.)
Actuators To perform actions (LEDs, relays, motors)
Wi-Fi / Ethernet Internet connectivity
Power supply 5V/3A adapter
Breadboard & Jumper wires For prototyping
Optional: Camera For surveillance or image processing
2. Software Requirements
Software Purpose
Raspberry Pi OS
Operating system
(Raspbian)
Python Programming language for interfacing sensors and IoT
Libraries [Link], Adafruit_DHT, paho-mqtt, requests
ThingSpeak, Blynk, Adafruit IO, Node-RED for
IoT Platforms
monitoring
3. Steps to Implement IoT with Raspberry Pi
Step 1: Set up Raspberry Pi
1. Install Raspberry Pi OS on a microSD card.
2. Connect keyboard, mouse, and monitor for initial setup.
3. Connect to Wi-Fi or Ethernet.
4. Update the system:
sudo apt update
sudo apt upgrade
Step 2: Connect Sensors and Actuators
Connect sensors to GPIO pins.
Use resistors, motor drivers, or relays where necessary.
Example: DHT11 temperature sensor connected to GPIO4.
Step 3: Write IoT Program
Use Python to read data from sensors.
Use HTTP or MQTT protocols to send data to a cloud platform.
Example: Sending Temperature Data to ThingSpeak
import Adafruit_DHT
import requests
import time
sensor = Adafruit_DHT.DHT11
pin = 4
api_key = 'YOUR_THINGSPEAK_API_KEY'
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if temperature is not None:
url = f'[Link]
api_key={api_key}&field1={temperature}&field2={humidity}'
response = [Link](url)
print(f'Status Code: {response.status_code}, Temp: {temperature}C,
Humidity: {humidity}%')
[Link](15)
Step 4: Configure IoT Cloud Dashboard
1. Create an account on ThingSpeak / Blynk / Adafruit IO.
2. Create a channel to receive data.
3. Use API key from cloud to send sensor readings.
4. Monitor live data on the dashboard and generate graphs/alerts.
Step 5: Implement Control for Actuators
IoT is not only data collection but also remote control.
Example: Control a fan based on temperature reading.
Python Example:
import [Link] as GPIO
[Link]([Link])
[Link](17, [Link]) # Fan relay
if temperature > 30:
[Link](17, [Link]) # Turn ON fan
else:
[Link](17, [Link]) # Turn OFF fan
4. IoT Communication Protocols with Raspberry Pi
1. HTTP / HTTPS – Sending sensor data via API.
2. MQTT (Message Queuing Telemetry Transport) – Lightweight protocol for
IoT devices.
3. WebSocket – For real-time communication.
4. CoAP (Constrained Application Protocol) – For resource-limited devices.
5. Applications of IoT Using Raspberry Pi
Application Description
Smart Home Control lights, fans, and appliances remotely
Application Description
Environmental Monitoring Monitor temperature, humidity, air quality
Agriculture Soil moisture monitoring and smart irrigation
Industrial Automation Monitor machinery, send alerts for maintenance
Health Monitoring Collect patient data and send to cloud
Security Systems Capture images/videos with camera and send alerts
6. Advantages of Using Raspberry Pi for IoT
Can run full OS and programs for complex processing.
Supports multiple sensors and actuators.
Flexible connectivity (Wi-Fi, Ethernet, Bluetooth).
Can handle data logging, cloud integration, and analytics.
7. Limitations
Needs continuous power supply.
Not real-time; GPIO control has slight delay compared to microcontrollers.
May require additional circuits to interface high-power devices.
Raspberry Pi enables full-fledged IoT solutions by combining sensor data
collection, cloud integration, and actuator control. Its computing power allows for
advanced IoT applications such as smart homes, industrial automation, and AI-
based projects.
Introduction
Raspberry Pi is a compact single-board computer that is ideal for IoT applications
because it can collect data from sensors, process it, and communicate over the
internet. Unlike microcontrollers (e.g., Arduino), Raspberry Pi can run a full
operating system and handle complex tasks such as data processing, cloud
integration, and visualization.
IoT implementation with Raspberry Pi typically involves data acquisition,
processing, and remote monitoring or control.
1. Hardware Requirements
Component Purpose
Raspberry Pi (3, 4, or
Main computing unit
Zero)
Collect environmental or system data (temperature,
Sensors
humidity, motion, etc.)
Actuators Devices to perform actions (LEDs, motors, relays, fans)
Wi-Fi / Ethernet Connect Raspberry Pi to the internet
Power Supply 5V/3A adapter
Breadboard & Jumper
Prototyping and connections
Wires
Optional: Camera For security or image-based IoT applications
2. Software Requirements
Software / Library Purpose
Raspberry Pi OS
Operating system for Raspberry Pi
(Raspbian)
Python Programming language to interface with sensors and cloud
[Link], Adafruit_DHT, paho-mqtt, requests for IoT
Libraries
communication
ThingSpeak, Blynk, Adafruit IO, Node-RED for data
IoT Platforms
visualization and remote control
3. Steps to Implement IoT with Raspberry Pi
Step 1: Set up Raspberry Pi
1. Install Raspberry Pi OS on a microSD card.
2. Connect keyboard, mouse, and monitor for initial setup.
3. Connect Raspberry Pi to Wi-Fi or Ethernet.
4. Update system packages:
sudo apt update
sudo apt upgrade
Step 2: Connect Sensors and Actuators
Connect sensors (e.g., DHT11 for temperature/humidity) to GPIO pins.
Connect actuators (e.g., LEDs, relays) to GPIO pins via proper resistors or
driver circuits.
Use protocols like I2C, SPI, UART for modules if required.
Step 3: Write Python Program
Read data from sensors.
Send data to an IoT cloud platform.
Control actuators based on sensor readings.
Example: Temperature and Humidity to ThingSpeak
import Adafruit_DHT
import requests
import time
sensor = Adafruit_DHT.DHT11
pin = 4
api_key = 'YOUR_THINGSPEAK_API_KEY'
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if temperature is not None:
url = f'[Link]
api_key={api_key}&field1={temperature}&field2={humidity}'
response = [Link](url)
print(f'Temp: {temperature}°C, Humidity: {humidity}%, Status:
{response.status_code}')
[Link](15)
Step 4: Set up IoT Cloud
1. Create an account on ThingSpeak / Blynk / Adafruit IO.
2. Create a dashboard or channel for data visualization.
3. Obtain the API key for the channel.
4. Ensure Raspberry Pi sends sensor readings using HTTP or MQTT.
Step 5: Remote Monitoring and Control
IoT allows both data collection and actuator control.
Example: Turn on a fan if temperature exceeds a threshold.
import [Link] as GPIO
[Link]([Link])
[Link](17, [Link]) # Fan connected to GPIO17
if temperature > 30:
[Link](17, [Link]) # Fan ON
else:
[Link](17, [Link]) # Fan OFF
4. IoT Communication Protocols
HTTP / HTTPS: Sending data via APIs.
MQTT: Lightweight protocol for IoT communication.
WebSockets: Real-time bidirectional communication.
CoAP: For constrained devices with low power.
5. Example IoT Applications Using Raspberry Pi
Application Description
Smart Home Control lights, fans, and appliances remotely
Environmental Monitoring Measure temperature, humidity, and air quality
Smart Agriculture Automated irrigation based on soil moisture
Application Description
Industrial IoT Monitor machines, predictive maintenance
Health Monitoring Collect and transmit patient data to cloud
Security Systems Capture images/video with camera and send alerts
6. Advantages
Can run full OS and perform data processing.
Can interface with multiple sensors and actuators.
Flexible connectivity options: Wi-Fi, Ethernet, Bluetooth.
Can integrate with cloud for monitoring and analytics.
7. Limitations
Needs continuous power supply.
GPIO control has slight latency compared to microcontrollers.
Requires external circuits for high-power devices.
Slightly higher cost than microcontrollers for simple tasks.
Question Bank
Define the Internet of Things (IoT) and explain how Raspberry Pi is used in IoT
applications.
List the essential hardware components required to implement an IoT project
using Raspberry Pi.
Mention the software tools and libraries commonly used for Raspberry Pi IoT
projects.
Explain how to interface a DHT11 temperature and humidity sensor with
Raspberry Pi.
Describe the method to control an LED or a relay using Raspberry Pi GPIO
pins in an IoT setup.
What are the differences between digital and analog sensor interfacing with
Raspberry Pi?
How can Raspberry Pi send sensor data to a cloud platform such as ThingSpeak
or Adafruit IO?
Explain the use of MQTT and HTTP protocols for IoT communication with
Raspberry Pi.
Discuss an IoT application implemented using Raspberry Pi for smart home
automation.
List the advantages and limitations of using Raspberry Pi for IoT projects.