UNIT 1: FOUNDATIONS & FIRST PROTOTYPE
1.0 Introduction to the IoT Lab Ecosystem
-
1.1 Course Objectives & Lab Safety Guidelines
-
Objective: Build end-to-end IoT prototypes from hardware assembly to cloud data visualization.
-
Safety: Handle components with care (ESD risk), verify power connections before powering, avoid short circuits, work in a dry, well-ventilated area.
-
-
1.2 Understanding the IoT Stack: Perception, Network, Application Layers
-
Perception Layer: Physical world interaction. Sensors (data input) & Actuators (control output). MCUs/MPUs process data.
-
Network Layer: Data transmission. Protocols (Wi-Fi, Bluetooth, MQTT, HTTP). Gateways may aggregate data.
-
Application Layer: User/enterprise interface. Cloud platforms, dashboards, mobile apps, analytics.
-
[!TIP] Exam often asks to map a given device/component to the correct layer.
-
-
1.3 Overview of the Development Workflow: Idea → Prototype → Deploy
-
Idea: Define problem, select sensors, estimate power & cost.
-
Prototype: Breadboard circuit, write/test code locally, establish connectivity.
-
Deploy: Design PCB/case, optimize power, secure device, scale.
-
2.0 Core Hardware Components & Selection
-
2.1 Microcontroller Units (MCUs) vs. Microprocessor Units (MPUs)
-
MCU: Single-chip computer (CPU, RAM, I/O). Real-time, low-power, no OS (e.g., Arduino Uno, ESP32). Best for dedicated, power-constrained sensor nodes.
-
MPU: Runs full OS (Linux, Windows). High processing, complex tasks, multitasking (e.g., Raspberry Pi 4). Best for edge computing, video processing, gateway hubs.
-
2.1.1 Key specifications:
-
CPU: Clock speed (MHz), architecture (ARM, AVR, x86).
-
Memory: Flash (code storage), SRAM (runtime data).
-
GPIO: Number of digital/analog pins, PWM capability, communication interfaces (I2C, SPI, UART).
-
Power Consumption: Active current (mA), sleep current (µA). Critical for battery life.
-
-
| Feature | MCU (e.g., ESP32) | MPU (e.g., Raspberry Pi 4) |
|---|---|---|
| OS | Bare-metal, RTOS | Full Linux (Ubuntu, Raspberry Pi OS) |
| Power | Very Low (µA in deep sleep) | High (100s mA even idle) |
| I/O | Native GPIO, ADC, DAC | Requires converter chips for true analog |
| Boot Time | Milliseconds | Seconds |
| Use Case | Sensor node, battery device | Gateway, media center, complex logic |
-
2.2 Popular Development Boards for IoT Prototyping
-
2.2.1 Arduino Family (Uno, Nano, MKR):
-
Uno (ATmega328P): 5V logic, 14 GPIO, 2KB RAM. Easy for beginners, vast library support.
-
MKR Series (SAMD21): 3.3V logic, built-in Wi-Fi/BNW on some models (MKR1000), better for IoT.
-
Strength: Simple IDE, huge community. Limitation: Limited RAM/Flash, no native Wi-Fi on base Uno.
-
-
2.2.2 ESP32 Family:
-
Core: Dual-core Xtensa LX6 (240 MHz), Wi-Fi 4 (802.11 b/g/n), Bluetooth 4.2/5.0.
-
Key Features: Ultra-low power deep sleep (~10µA), abundant GPIO, capacitive touch, Hall sensor.
-
Pinout: Many pins are input-only, some are used for flash. Always check board-specific pinout.
-
-
2.2.3 Raspberry Pi Pico & Raspberry Pi (Zero/4):
-
Pico (RP2040): Dual-core ARM Cortex-M0+, 26 GPIO, programmable I/O (PIO) for custom protocols. MCU, no OS.
-
Raspberry Pi (Zero/4): MPU, runs Linux. Full HDMI, USB host, Ethernet. Needs OS installation (Raspberry Pi Imager). Not a direct Arduino substitute.
-
-
-
2.3 Sensors & Actuators
-
2.3.1 Digital vs. Analog Sensors:
-
Digital: Output discrete signals (HIGH/LOW, I2C/SPI/UART). Immune to noise. Examples: DHT11 (temp/hum), PIR (motion), push button.
-
Analog: Output continuous voltage (0-Vcc). Requires ADC. Examples: Potentiometer, LDR (photoresistor), analog temperature sensor (LM35).
-
-
2.3.2 Actuators:
-
Relays: Electromechanical switch for high-voltage/current loads. Needs driver transistor (ULN2003).
-
Servos: Position control via PWM signal (50Hz, 1-2ms pulse). Requires power supply capable of peak current.
-
DC Motors: Requires motor driver (L298N, TB6612) for direction/speed control.
-
LEDs: Current-limiting resistor required (R = (Vcc - Vf) / If).
-
-
2.3.3 Interfacing:
-
Voltage Level Shifting: 5V Arduino → 3.3V ESP32/Pico needs a voltage divider (e.g., 2 resistors) or logic level converter.
-
Pull-up/Pull-down Resistors: Ensure defined logic state on input pins (e.g., button). Internal pull-ups (
INPUT_PULLUP) often sufficient.
-
-
-
2.4 Power Management for IoT Devices
-
2.4.1 Power Sources: USB (5V, 500mA+), Battery packs (LiPo 3.7V, 18650), Solar panel + charge controller (TP4056) + battery.
-
2.4.2 Calculating Battery Life:
-
$$\text{Battery Life (hours)} = \frac{\text{Battery Capacity (mAh)}}{\text{Average Current Draw (mA)}}$$
\boxed{\text{Battery Life (h)} = \frac{\text{Capacity (mAh)}}{\text{Avg. Current (mA)}}}
* **Avg. Current** must include active and sleep states weighted by time.
* **2.4.3 Deep Sleep Modes**: MCU shuts down most peripherals, wakes via timer or external interrupt. **Essential for multi-year battery life**. Current drops from ~80mA (active) to ~10µA (deep sleep).
3.0 Setting Up the Development Environment
-
3.1 Installing & Configuring the Arduino IDE (or PlatformIO/VS Code)
-
Arduino IDE: Download from arduino.cc. Simple, good for beginners.
-
PlatformIO (VS Code extension): Professional, better library/dependency management, multiple frameworks. Recommended for serious projects.
-
3.1.1 Board Manager: Add board definitions via URL (e.g.,
https://dl.espressif.com/dl/package_esp32_index.jsonfor ESP32) or Boards Manager UI. -
3.1.2 Library Manager: Search/install libraries (e.g., "DHT sensor library", "PubSubClient"). Check for dependencies.
-
-
3.2 Serial Monitor & Debugging Fundamentals
-
Serial.begin(baud_rate): Initialize serial communication insetup(). Common baud: 9600, 115200. -
Serial.print()/Serial.println(): Output variable values, state messages. Primary debugging tool. -
Baud Rate: Must match between code and Serial Monitor. Mismatch = garbled text.
-
-
3.3 Introduction to Circuit Simulation Tools
- Tinkercad Circuits: Browser-based. Drag-and-drop components, wire virtually, write Arduino code, simulate. Excellent for pre-lab testing and debugging logic without hardware.
4.0 Basic Programming & Interfacing (Hands-On Focus)
-
4.1 Arduino/PlatformIO Sketch Structure
-
setup(): Runs once at startup. Initialize pins, start serial, connect to Wi-Fi. -
loop(): Runs repeatedly forever. Main logic, sensor reads, data transmission.
-
-
4.2 Digital I/O Operations
-
pinMode(pin, OUTPUT/INPUT/INPUT_PULLUP): Configure pin. -
digitalWrite(pin, HIGH/LOW): Set output voltage (3.3V/5V or 0V). -
digitalRead(pin): Read input voltage (returns HIGH/LOW). -
Example: Blink LED on pin 13.
void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); }
-
-
4.3 Analog I/O & Pulse Width Modulation (PWM)
-
analogRead(pin): Reads voltage on ADC pin (e.g., A0 on Uno). Returns 0-1023 (10-bit). Maps to 0-Vref. -
analogWrite(pin, value): PWM on PWM-capable pin (~, #).value0-255 (8-bit). Simulates analog output (e.g., LED dimming). Frequency ~490Hz or 980Hz. -
PWM for Servo: Use
Servo.hlibrary.servo.write(angle)maps angle to 1-2ms pulse.
-
-
4.4 Interfacing Common Sensors
-
4.4.1 DHT11/DHT22 (Temp & Humidity):
-
Single-wire digital communication. Requires external library (
DHT sensor libraryby Adafruit). -
dht.readTemperature(),dht.readHumidity(). ReturnsNANon read failure. -
Timing critical: Read interval > 2 seconds for DHT11.
-
-
4.4.2 PIR Motion Sensor:
-
Digital output.
digitalRead(pirPin). Often has adjustable delay & sensitivity. -
Debouncing: Sensor output may bounce. Use simple delay (e.g.,
if (motion && millis() - lastMotionTime > 5000)) or state machine.
-
-
-
4.5 Using External Libraries
-
#include <LibraryName.h>at top. -
Create object:
DHT dht(DHTPIN, DHTTYPE); -
Initialize in
setup():dht.begin(); -
Call functions:
float t = dht.readTemperature();
-
5.0 IoT Connectivity: Local Network Communication
-
5.1 Wi-Fi Fundamentals for IoT
-
Station (STA) Mode: Device connects to an existing router (most common). Gets IP from router's DHCP.
-
Access Point (AP) Mode: Device acts as a router/hotspot. Other devices connect to it. Used for initial configuration (captive portal).
-
-
5.2 Implementing Wi-Fi on ESP32/ESP8266
-
Library:
#include <WiFi.h>(ESP32) or#include <ESP8266WiFi.h>. -
Key Functions:
WiFi.begin(ssid, password); // Connect while (WiFi.status() != WL_CONNECTED) { delay(500); } // Wait Serial.println(WiFi.localIP()); // Print assigned IP -
5.2.2 Handling Failures:
-
Check
WiFi.status():WL_CONNECTED,WL_NO_SSID_AVAIL,WL_CONNECT_FAILED. -
Implement reconnection logic in
loop()if disconnected.
-
-
-
5.3 Simple Data Transmission Protocols
-
5.3.1 HTTP Client:
-
Library:
HTTPClient.h(ESP32/ESP8266). -
GET:
httpClient.get("http://server.com/data?temp=25"). -
POST:
httpClient.post(url, "application/json", "{\"temp\":25}"). -
Returns HTTP status code (200 = OK).
-
-
5.3.2 Introduction to MQTT:
-
Broker: Central server (e.g., Mosquitto, HiveMQ, cloud broker). Routes messages.
-
Topic: String-based address (e.g.,
home/livingroom/temp). Hierarchical. -
Publish: Send message to a topic.
-
Subscribe: Receive messages from a topic.
-
Library:
PubSubClient.h. -
Basic Flow:
client.setServer(broker, 1883); client.connect("clientID"); client.publish("topic", "payload"); client.subscribe("topic"); client.loop(); // Must be called frequently to maintain connection
-
-
6.0 Introduction to Cloud IoT Platforms
-
6.1 Cloud Platform Roles:
-
Broker: MQTT message router.
-
Database: Time-series storage (e.g., InfluxDB, MySQL).
-
Dashboard: Real-time visualization (graphs, gauges).
-
Rules Engine: Trigger actions based on data (e.g., "if temp>30, send email").
-
-
6.2 Overview of Popular Platforms
-
6.2.1 ThingSpeak:
-
Channel: One data stream (8 fields max). Free tier.
-
API Keys:
Write API Key(device sends data),Read API Key(dashboard reads). -
Visualization: Built-in MATLAB plots, or embed in website.
-
-
6.2.2 Blynk:
-
Mobile app (iOS/Android) for rapid dashboard.
-
Auth Token: Unique per device.
-
Drag-and-drop widgets (Gauge, Graph, Button). Uses Blynk cloud or local server.
-
-
6.2.3 AWS IoT Core / Azure IoT Hub / Google Cloud IoT Core:
-
Concept: Enterprise-grade, scalable, secure (TLS/X.509 certificates), integrate with other cloud services (Lambda, Functions, BigQuery).
-
Complexity: Higher setup cost, steeper learning curve.
-
-
-
6.3 Connecting a Device to a Cloud Platform
-
6.3.1 Obtaining Credentials:
-
ThingSpeak: Channel ID, Write API Key.
-
Blynk: Auth Token.
-
AWS/Azure: Device Certificate, Private Key, Root CA.
-
-
6.3.2 Sending Data:
-
HTTP (ThingSpeak):
GET https://api.thingspeak.com/update?api_key=KEY&field1=25 -
MQTT (Blynk/AWS): Publish to platform-specific topic (e.g., Blynk:
v1for virtual pin 1).
-
-
7.0 Integrated Lab Project 1: "Smart Environmental Monitor"
-
7.1 Project Specification: Read temperature, humidity, motion; send data to cloud (ThingSpeak/Blynk); local status LEDs.
-
7.2 System Design & Component List:
-
Board: ESP32 DevKit V1.
-
Sensors: DHT11 (temp/hum), HC-SR501 (PIR).
-
Output: 2x LEDs (Green=Wi-Fi OK, Red=Cloud OK).
-
Power: USB or 5V adapter.
-
-
7.3 Step-by-Step Implementation Plan
-
Assemble Hardware:
-
DHT11: VCC→3.3V, GND→GND, Data→GPIO4 (with 10k pull-up to 3.3V).
-
PIR: VCC→5V, GND→GND, OUT→GPIO5.
-
LEDs: Green→GPIO2 (330Ω), Red→GPIO15 (330Ω), cathodes→GND.
-
-
Write Code - Sensor Reading & Wi-Fi:
-
Include libraries (
WiFi.h,DHT.h,HTTPClient.h/PubSubClient.h). -
setup(): Init Serial, pins, DHT, connect to Wi-Fi (with timeout/reconnect logic). -
loop(): Read DHT (every 2s), read PIR. Print to Serial.
-
-
Implement Data Transmission:
-
ThingSpeak (HTTP): Construct URL with API key and field values (
field1=temp,field2=hum,field3=motion).httpClient.get(). Check response. -
Blynk (MQTT):
client.publish("v1", String(temp))etc.
-
-
Add Status Indicators:
-
Green LED ON if
WiFi.status() == WL_CONNECTED. -
Red LED ON if last HTTP/MQTT send was successful.
-
-
-
7.4 Testing & Debugging:
-
Serial Monitor: Verify sensor readings, Wi-Fi IP, HTTP status codes.
-
Check Cloud Dashboard: ThingSpeak channel/Blynk app for incoming data.
-
Troubleshoot: Ping device IP, verify API keys, check DHT wiring (10k pull-up), ensure PIR output is stable.
-
-
7.5 Viewing Data: ThingSpeak channel view (live graph), Blynk app dashboard (real-time widgets).
8.0 Essential Lab Practices & Documentation
-
8.1 Circuit Schematic Drawing: Use Fritzing (breadboard view) or draw.io (schematic view). Show all connections, power rails, component labels.
-
8.2 Code Commenting & Version Control:
-
Comment: Explain why (not what).
// Read DHT11 every 2 secondsnot// delay 2000. -
Git Basics:
git init,git add .,git commit -m "Add DHT reading",git push. Use GitHub/GitLab.
-
-
8.3 Lab Report Structure:
-
Objective: What you aimed to build/measure.
-
Methodology: Components list, circuit diagram, code flow (pseudocode).
-
Results: Screenshots of Serial Monitor, Cloud Dashboard, working hardware.
-
Observations: Issues faced (e.g., DHT read failures), power consumption measured, Wi-Fi range.
-
Conclusion: Did it work? What would you improve? (e.g., add deep sleep, OLED display).
-
-
8.4 Troubleshooting Checklist:
-
Power: Is Vcc correct (3.3V vs 5V)? Is GND common? Is power supply sufficient?
-
Connections: Loose wires? Wrong pin in code vs. hardware?
-
Code Logic: Baud rate match?
delay()enough for sensor? Library included? -
Network: Correct SSID/password? Firewall blocking? Broker address/port correct?
-
Credentials: API key/Auth token copied correctly? No extra spaces?
-
[!TIP] Exam Focus: Be prepared to draw a simple block diagram of the IoT stack for a given project, calculate battery life for a node with given active/sleep currents, compare Arduino vs ESP32 vs Raspberry Pi, and write a basic
loop()that reads a sensor and sends an HTTP GET request.