Unit 1: IoT hardware interfacing and cloud integration
IoT and Its Applications Laboratory notes · PTU syllabus (PGCA1944)
On this page
- Unit summary
- Setting up Arduino and Raspberry Pi
- LED and buzzer with timed control
- Push button and digital sensors (IR and LDR)
- DHT11 temperature and humidity
- Relay-based motor control
- OLED display (I2C)
- Bluetooth with a smartphone
- ThingSpeak: upload and retrieve
- MySQL on Raspberry Pi
- Publishing to an MQTT broker
- Key terms
- Quick revision
- Important questions
Unit summary
This lab interfaces sensors, actuators and displays with Arduino or Raspberry Pi, communicates over Bluetooth, and sends data to the ThingSpeak cloud, a MySQL database on the Pi and an MQTT broker.
After this unit you can
- Set up Arduino and Raspberry Pi
- Interface LEDs, buzzers, buttons, IR, LDR, DHT11, relays and OLED displays
- Exchange data over Bluetooth with a smartphone
- Upload, retrieve and publish sensor data to ThingSpeak, MySQL and MQTT
PTU syllabus topics
- Arduino/Raspberry Pi setup
- LED/buzzer interfacing with timed control
- push-button and digital sensor (IR/LDR) interfacing
- DHT11 temperature/humidity sensor interfacing
- relay-based motor control
- OLED display interfacing
- Bluetooth data transmission to smartphone
- Bluetooth-controlled LED on/off
- uploading and retrieving sensor data to/from ThingSpeak cloud
- MySQL installation and querying on Raspberry Pi
- publishing temperature data to an MQTT broker
- 1
Wire the DHT11 sensor
- 2
Read temperature and humidity
- 3
Connect to Wi-Fi
- 4
Send to ThingSpeak with the API key
- 5
View live charts
- 6
Read data back for actions
Topic 1
Setting up Arduino and Raspberry Pi
- 1Install Arduino IDE
- 2Connect the board by USB
- 3Select board and port
- 4Open Blink example and upload
- 5Install libraries via Library Manager
- 1Flash Raspberry Pi OS with Imager
- 2Enable SSH and Wi-Fi
- 3Boot and update (sudo apt update)
- 4Enable I2C and SPI in raspi-config
- 5Use Python with gpiozero
- Safety: use a 220 Ω resistor with LEDs; Pi GPIO is 3.3 V — never feed it 5 V.
Topic 2
LED and buzzer with timed control
cppvoid setup() { pinMode(13, OUTPUT); pinMode(8, OUTPUT); }
void loop() {
digitalWrite(13, HIGH); tone(8, 1000); delay(1000); // on 1 s
digitalWrite(13, LOW); noTone(8); delay(2000); // off 2 s
}Topic 3
Push button and digital sensors (IR and LDR)
cppvoid setup() { pinMode(2, INPUT_PULLUP); pinMode(7, INPUT); pinMode(13, OUTPUT); Serial.begin(9600); }
void loop() {
bool pressed = digitalRead(2) == LOW; // button to GND
bool obstacle = digitalRead(7) == LOW; // IR module output low when object near
int light = analogRead(A0); // LDR divider 0–1023
digitalWrite(13, (pressed || obstacle || light < 300) ? HIGH : LOW);
Serial.println(light); delay(200);
}Topic 4
DHT11 temperature and humidity
cpp#include <DHT.h>
DHT dht(2, DHT11);
void setup() { Serial.begin(9600); dht.begin(); }
void loop() {
float h = dht.readHumidity(), t = dht.readTemperature();
if (isnan(h) || isnan(t)) { Serial.println("Read failed"); return; }
Serial.print(t); Serial.print(" C, "); Serial.print(h); Serial.println(" %");
delay(2000); // DHT11 needs ≥ 1 s between reads
}Topic 5
Relay-based motor control
cppvoid setup() { pinMode(4, OUTPUT); }
void loop() { digitalWrite(4, HIGH); delay(5000); digitalWrite(4, LOW); delay(5000); } // motor 5 s on/off- The relay isolates the low-voltage board from the motor supply; use a module with a flyback diode and opto-isolation.
Topic 6
OLED display (I2C)
cpp#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire);
void setup() { oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); oled.clearDisplay(); oled.setTextSize(2); oled.setTextColor(WHITE);
oled.setCursor(0, 0); oled.println("SBS IoT"); oled.display(); }
void loop() {}Topic 7
Bluetooth with a smartphone
cpp#include <SoftwareSerial.h>
SoftwareSerial bt(10, 11); // HC-05 TX→10, RX←11 (via divider)
void setup() { bt.begin(9600); pinMode(13, OUTPUT); }
void loop() {
if (bt.available()) { char c = bt.read(); digitalWrite(13, c == '1' ? HIGH : LOW); } // LED on/off from app
bt.print("T="); bt.println(analogRead(A0)); delay(1000); // send data to phone
}- Pair the HC-05 (PIN 1234) and use a Bluetooth serial terminal app.
Topic 8
ThingSpeak: upload and retrieve
pythonimport requests, time, Adafruit_DHT
KEY_W, CH = "WRITE_KEY", "123456"
for _ in range(5):
h, t = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4)
requests.get("https://api.thingspeak.com/update", params={"api_key": KEY_W, "field1": t, "field2": h})
time.sleep(16) # free plan: one update per 15 s
r = requests.get(f"https://api.thingspeak.com/channels/{CH}/feeds.json", params={"results": 5})
print([f["field1"] for f in r.json()["feeds"]])Topic 9
MySQL on Raspberry Pi
bashsudo apt install -y mariadb-server python3-pymysql
sudo mysql -e "CREATE DATABASE iot; CREATE USER 'pi'@'localhost' IDENTIFIED BY 'pi123'; GRANT ALL ON iot.* TO 'pi'@'localhost';"
sudo mysql iot -e "CREATE TABLE readings (id INT AUTO_INCREMENT PRIMARY KEY, temp FLOAT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);"pythonimport pymysql
db = pymysql.connect(host="localhost", user="pi", password="pi123", database="iot")
with db.cursor() as c:
c.execute("INSERT INTO readings (temp) VALUES (%s)", (28.5,)); db.commit()
c.execute("SELECT * FROM readings ORDER BY ts DESC LIMIT 5"); print(c.fetchall())Topic 10
Publishing to an MQTT broker
pythonimport paho.mqtt.client as mqtt, time, random
c = mqtt.Client(); c.connect("test.mosquitto.org", 1883)
for _ in range(5):
c.publish("sbs/lab/temperature", f"{25 + random.random() * 5:.1f}", qos=1); time.sleep(2)bashmosquitto_sub -h test.mosquitto.org -t "sbs/lab/#" -v # subscriber to verifyKey terms
- INPUT_PULLUP
- Internal resistor keeping a pin HIGH until pressed
- Relay
- Electrically operated switch
- HC-05
- Bluetooth serial module
- ThingSpeak channel
- Cloud store with up to eight fields
- MQTT topic
- Hierarchical name messages are published to
Quick revision
- Arduino and Pi setup; 3.3 V GPIO.
- LED and buzzer timing; button, IR, LDR; DHT11.
- Relay motor; OLED over I2C; HC-05 Bluetooth.
- ThingSpeak write and read API; MySQL on Pi; MQTT publish and subscribe.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Why is a resistor used with an LED?
- Q2.How does INPUT_PULLUP work?
- Q3.Why must DHT11 reads be spaced out?
- Q4.Why use a relay for a motor?
- Q5.What is the I2C address of a typical OLED?
- Q6.How do you verify MQTT messages?
Long-answer questions
- Q1.Interface DHT11 with Arduino and display readings on an OLED.
- Q2.Control an LED from a smartphone over Bluetooth.
- Q3.Upload and retrieve sensor data with ThingSpeak.
- Q4.Store sensor readings in MySQL on Raspberry Pi and publish them over MQTT.
Stuck on this unit?
Message SBS on WhatsApp for help with IoT and Its Applications Laboratory, or to ask about studying M.Sc IT at Synetic.
