IoT Fundamentals and Arduino Programming

  • This is an individual assignment.

  • The expected word limit is between 1500 - 2000 words.

  • This individual assignment is worth 20% of the total marks assigned for the unit.

  • Pay extra attention to the instructions provided at the end of this assignment on submission requirements, late submission, and plagiarism policy.

  • Due date: 30 March 2024 23:59.

    • Report Topic Selection (10 pts)

    • Selection of IoT Hardware and Architecture (20 pts)

    • Introduction (15 pts)

    • Current State of the Art (35 pts total)

    • Bill of Materials and Cost Estimation (10 pts)

    • Conclusion (5 pts)

  • Week 1 – Introduction

  • Week 2 – IoT hardware and Software

  • Week 3 – IoT programming 1

  • Week 4 – IoT programming 2

  • Week 5 – IoT Data Management

  • Week 6 – APIs and Webservers

  • Semester Break

  • Week 7 – IoT Data Analytics

  • Week 8 – Guest Lecture

  • Week 9 – IoT Networking

  • Week 10 – Cloud computing for IoT applications

  • Week 11 – Advanced Topic in IoT 1 (Artificial Intelligence)

  • Week 12 – Advanced Topic in IoT 2 (Security)

  • Teach fundamentals of developing an IoT-based solution for Smart Homes, Smart Cities etc., using IoT sensors and devices.

  • Students will learn the skills to work with current popular IoT sensor and platforms such as Arduino and will have the opportunity to apply these skills in developing innovative IoT-based system.

IoT Architecture

  • Things / IoT Devices

  • Cloud

  • Edge

IoT Sensor Nodes

*Hardware / Software

  • Sensors

    • Accelerometer

    • Temperature

    • Ultrasonic / Proximity

    • Gas

    • Infrared

  • Boards

    • Feather 32u4

      • Hardware / Software

      • Serial Communication

    • Adafruit LoRa Radio FeatherWing

      • Hardware / Software

      • Serial Communication

    • TEENSY 3.2 (Arduino)

      • Hardware / Software

      • Serial Communication

    • FireBeetle ESP32 (Arduino) +

      • Hardware / Software

      • Serial Communication

  • Connectivity

    • XBee ZigBee

    • Bluetooth

    • Wi-Fi

    • BLE 4.1

    • Wi-Fi

Sensors

  • Temperature Sensors

  • Analog Soil Moisture

  • Humidity

  • Infrared Thermometer

  • Gas Sensor

  • CO2 Sensor

  • Digital Push Button

Structure

  • void setup() {

    • // Runs once when the board is powered on or reset

    • }

  • void loop() {

    • // Repeats continuously

    • }

Variable Declaration

  • int

    • Size: 2 bytes (16-bit) on ATmega328-based boards (e.g., Arduino Uno, Nano, Mega)

    • 4 bytes (32-bit) on ESP32, ARM-based boards, and some newer architectures

    • Range (for 16-bit systems like Arduino Uno/Nano): -32,768 to 32,767

    • Range (for 32-bit systems like ESP32, Arduino Due): -2,147,483,648 to 2,147,483,647

Variable Declaration Examples:

  • intledPin=13;int ledPin = 13; // Store LED pin number

  • inttemperature=25;int temperature = -25;

Usage in Code:
void setup() {
 Serial.begin(9600);
 Serial.println(temperature);
}

void loop() {
 digitalWrite(ledPin, HIGH);
 delay(1000);
 digitalWrite(ledPin, LOW);
 delay(1000);
}

Variable Declaration - 16-bit int (unsigned)

  • Unsigned

    • 00 (00000000 00000000 in binary)

    • 2161=65,5352^{16}−1 = 65,535 (11111111 11111111 in binary)

    • (1×215)+(1×214)+(1×213)++(1×21)+(1×20)(1 \times 2^{15})+(1 \times 2^{14})+(1 \times 2^{13})+…+(1 \times 2^{1})+(1 \times 2^{0})

    • (1×32768)+(1×16384)+(1×8192)+(1×4096)+(1×2048)+(1×1024)+(1×512)+(1×256)+(1×128)+(1×64)+(1×32)+(1×16)+(1×8)+(1×4)+(1×2)+(1×1)=32768+16384+8192+4096+2048+1024+512+256+128+64+32+16+8+4+2+1=65,535(1 \times 32768) + (1 \times 16384) + (1 \times 8192) + (1 \times 4096) + (1 \times 2048) + (1 \times 1024) + (1 \times 512) + (1 \times 256) + (1 \times 128) + (1 \times 64) + (1 \times 32) + (1 \times 16) + (1 \times 8) + (1 \times 4) + (1 \times 2) + (1 \times 1) = 32768 +16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 65,535

Variable Declaration - 16-bit int (signed)

  • Signed

    • 21611=32,7682^{16-1}−1 = −32,768 to 32,767

    • Binary - Decimal (Signed 16-bit)

      • 00000000 00000000 = 0

      • 00000000 00000001 = 1

      • 01111111 11111111 = 32,767 (max positive)

      • 10000000 00000000 = -32,768 (min negative)

      • 11111111 11111111 = -1

Float

  • IEEE 754 standard

  • Represents them in binary with three parts:

    • Sign bit

    • Exponent

    • Fraction

  • For a 32-bit float (single precision), the structure is:

    • 1-bit for sign (0 = positive, 1 = negative)

    • 8-bits for exponent

    • 23-bits for fraction part

Example: 6.75

  • 1-bit for Sign (0)

  • 8-bits for Exponent ( 6 -> 110)

  • 23-bits for Fraction (0.75 -> (1x2-1 + 1x2-2)

  • 110.11 Normalised 1.1011

  • Stored Exponent = Actual Exponent+127 -> 2+127=129

  • 010000001101100000000000000000000 \mid 10000001 \mid 10110000000000000000000

String

  • Use char[] for memory-efficient, fixed-length strings (best for small MCUs like ATmega328).

    • char msg[] = "IoT programming is fun";

    • Length: 22

    • Size in memory: 23 ('\0')

  • Use String for easier manipulation, but avoid excessive use in low-RAM boards.

    • String msg = "IoT programming is fun";

Feature Comparison:

Feature

char[] (C-string)

String Class

Memory Usage

Fixed (efficient)

Dynamic (more RAM usage)

Length Calculation

strlen(msg)

msg.length()

Flexibility

Hard to modify

Easy to modify (e.g., +, .replace())

String Examples

char[] Example

void setup() {
 Serial.begin(9600);
 char msg[] = "IoT programming is fun";
 msg[19] = 'a';
 msg[20] = 'w';
 msg[21] = 'e';
 Serial.print("Modified Message: ");
 Serial.println(msg);
}

void loop() {}

String Example

void setup() {
 Serial.begin(9600);
 String msg = "IoT programming is fun";
 msg.replace("fun", "awesome");
 Serial.print("Modified Message: ");
 Serial.println(msg);
}

void loop() {}

Pin Modes & Digital I/O

  • pinMode(13, OUTPUT);

  • digitalWrite(13, HIGH);

  • digitalWrite(13, LOW);

  • int buttonState = digitalRead(2);

Analog I/O

  • analogWrite(9, 128); //PWM

  • We cannot use analogWrite() on A0 in most Arduino boards like Arduino Uno

  • int sensorValue = analogRead(A0);

    • Takes an input voltage (0V - 5V) on an analog pin (A0 - A5).

    • Converts it to a digital value (0 - 1023) using a 10-bit ADC (Analog-to-Digital Converter).

    • Returns an integer value (0 - 1023) that represents the measured voltage.

Example:

  • If analogRead(A0) = 512 and AREF = 5V

  • V=(ADCvalue×VAREF)/1023V = (ADC value \times V_{AREF}) / 1023

  • V=2.5vV = 2.5v

PWM (Pulse Width Modulation)

  • PWM (Pulse Width Modulation) is a technique used to simulate an analogue output using digital signals.

  • It works by rapidly switching a signal ON and OFF at a fixed frequency, where the ratio of ON-time to the total period determines the effective output voltage.

    • A fixed frequency (how fast it repeats)

    • A variable duty cycle (percentage of time the signal is HIGH in one cycle)

  • In Arduino Uno, PWM is available on digital pins: 3, 5, 6, 9, 10, 11.

PWM Example

void loop() {
 for (int brightness = 0; brightness <= 255; brightness++) {
 analogWrite(ledPin, brightness);
 delay(10);
 }
 for (int brightness = 255; brightness >= 0; brightness--) {
 analogWrite(ledPin, brightness);
 delay(10);
 }
}

CONTROL STRUCTURES

IF-ELSE

if (buttonState == HIGH) {
 digitalWrite(13, HIGH);
} else {
 digitalWrite(13, LOW);
}

FOR

for (int i = 0; i < 10; i++) {
 Serial.println(i);
}

WHILE

while (digitalRead(2) == LOW) {
 digitalWrite(13, HIGH);
}

SWITCH

switch (buttonState) {
 case HIGH:
 digitalWrite(13, HIGH);
 break;
 case LOW:
 digitalWrite(13, LOW);
 break;
}

SERIAL COMMUNICATION

  • Serial.begin(9600);

  • Serial.println(”IoT Programming is fun!”);

  • int value = Serial.parseInt(); // Read an integer from Serial

FUNCTIONS

void blinkLED(int pin, int duration) {
 digitalWrite(pin, HIGH);
 delay(duration);
 digitalWrite(pin, LOW);
 delay(duration);
}

Functions Example

void customFunction();

void setup() {
 Serial.begin(9600);
 for (int i = 0; i < 3; i++) {
 Serial.println(i);
 }
 customFunction();
}

void customFunction() {
 Serial.println(”IoT Programming is fun!”);
}

void loop() {
 // runs indefinitely
}

Delay

#define LED_PIN 9

void setup() {
 pinMode(LED_PIN, OUTPUT);
}

void loop() {
 digitalWrite(LED_PIN, HIGH);
 delay(500);
 digitalWrite(LED_PIN, LOW);
 delay(500);
}

millis()

#define LED_PIN 9 // LED connected to Pin 9
unsigned long previousMillis = 0;
const long interval = 500;

void setup() {
 pinMode(LED_PIN, OUTPUT);
}

void loop() {
 unsigned long currentMillis = millis(); // Get current time
 if (currentMillis - previousMillis >= interval) {
 previousMillis = currentMillis;
 digitalWrite(LED_PIN, !digitalRead(LED_PIN));
 }
}

Libraries & Custom Functions

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
 lcd.begin(16, 2); // Initialize 16x2 LCD
 lcd.print(”IoT Programming is Fun!”);
}

void loop() {}
  • Tutorial 3 - Analog Sensors