XBC602A IOT UNIT IV
UNIT IV – PYTHON PROGRAMMING (9 + 3 Hours)
Syllabus Content
1. Introduction to Python Programming
- Basics of Python
- Syntax and structure
- Variables and data types
- Control statements (if,
loops)
- Functions
2. Introduction to Raspberry Pi
- Overview of Raspberry Pi
- Features and architecture
- GPIO (General Purpose Input
Output) pins
- Interfacing basics
3. Implementation of IoT with Raspberry Pi
- Interfacing sensors using
Raspberry Pi
- Reading sensor data using
Python
- Controlling actuators
- Data communication
4. IoT Application Development using Raspberry Pi
(Repeated
in your syllabus, but interpreted as practical implementation)
- Real-time IoT system design
- Sensor-to-cloud integration
- Automation using Python
scripts
1. Introduction to Python Programming
1.1 Meaning of Python
Python is
a high-level, interpreted, general-purpose programming language. It is
simple, readable, and widely used in IoT because it supports fast development,
hardware interfacing, data handling, and networking.
1.2 Features of Python
- Easy to learn and use
- Simple syntax
- Interpreted language
- Object-oriented
- Platform independent
- Large standard library
- Supports hardware and web
integration
1.3 Why Python for IoT
Python is
popular in IoT because:
- It can run on small computers
like Raspberry Pi
- It supports GPIO control
- It provides libraries for
sensors and communication
- It is suitable for rapid
prototyping
- It integrates with cloud and
web services easily
1.4 Basic Syntax of Python
print("Hello, IoT")
1.5 Variables and Data Types
Python
variables do not require explicit declaration.
temperature = 28
device_name = "Raspberry Pi"
status = True
Common
data types
- int
- float
- str
- bool
1.6 Control Statements
Conditional
statement
temp = 35
if temp > 30:
print("Fan ON")
else:
print("Fan
OFF")
Looping
for i in range(5):
print(i)
1.7 Functions in Python
def greet():
print("Welcome to IoT Programming")
greet()
2. Introduction to Raspberry Pi
2.1 What is Raspberry Pi
Raspberry
Pi is a small, low-cost single-board computer developed for education,
embedded systems, and IoT applications. It can run an operating system, execute
Python programs, connect to the internet, and interface with electronic
components.
2.2 Features of Raspberry Pi
- ARM-based processor
- GPIO pins for interfacing
- USB ports
- HDMI port
- Wi-Fi and Bluetooth
- Micro SD card storage
- Linux-based OS support
2.3 Raspberry Pi in IoT
Raspberry
Pi acts as:
- IoT controller
- Data collector
- Gateway device
- Mini server
- Edge computing node
2.4 GPIO Pins
GPIO
means General Purpose Input Output. These pins are used to connect
sensors and actuators.
GPIO can
be configured as:
- Input
- Output
Examples:
- Input: switch, PIR sensor
- Output: LED, buzzer, relay
3. Implementation of IoT with
Raspberry Pi
3.1 Basic Steps
- Connect sensor/actuator to
Raspberry Pi
- Write Python program
- Read input from sensor
- Process data
- Perform output action
- Display/store/send data
3.2 Sensor Interfacing
Sensors
provide input signals to Raspberry Pi through GPIO pins.
Examples:
- Temperature sensor
- Motion sensor
- Light sensor
3.3 Actuator Interfacing
Actuators
perform physical action based on program logic.
Examples:
- LED
- Buzzer
- Relay
- Motor
3.4 Data Communication
Raspberry
Pi can send sensor data to:
- Local display
- Cloud platform
- Mobile app
- Web server
4. IoT Application Development
using Raspberry Pi
4.1 Real-Time IoT System
A
real-time IoT system continuously monitors environmental data and acts
instantly.
Example
- Motion detected → buzzer ON
- Temperature high → fan ON
- Light low → lamp ON
4.2 Sensor-to-Cloud Model
Flow
Sensor → Raspberry Pi → Internet → Cloud → Dashboard
4.3 Python in Automation
Python
scripts can be used to:
- Read GPIO values
- Perform decision making
- Send alerts
- Control actuators
automatically
SAMPLE
PYTHON PROGRAMS FOR IoT WITH OUTPUT
Program 1: Blink an LED
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
LED = 18
GPIO.setup(LED, GPIO.OUT)
for i in range(5):
GPIO.output(LED, GPIO.HIGH)
print("LED ON")
time.sleep(1)
GPIO.output(LED, GPIO.LOW)
print("LED OFF")
time.sleep(1)
GPIO.cleanup()
Output
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
Program 2: Read Push Button and
Control LED
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
button = 23
led = 18
GPIO.setup(button, GPIO.IN)
GPIO.setup(led, GPIO.OUT)
while True:
if
GPIO.input(button) == 1:
GPIO.output(led, GPIO.HIGH)
print("Button Pressed - LED ON")
else:
GPIO.output(led, GPIO.LOW)
print("Button Not Pressed - LED OFF")
time.sleep(1)
Sample Output
Button Not Pressed - LED OFF
Button Pressed - LED ON
Button Pressed - LED ON
Button Not Pressed - LED OFF
Program 3: PIR Sensor with Buzzer
import RPi.GPIO as GPIO
import time
pir = 17
buzzer = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(pir, GPIO.IN)
GPIO.setup(buzzer, GPIO.OUT)
while True:
if
GPIO.input(pir):
GPIO.output(buzzer, GPIO.HIGH)
print("Motion Detected - Buzzer ON")
else:
GPIO.output(buzzer, GPIO.LOW)
print("No Motion - Buzzer OFF")
time.sleep(1)
Sample Output
No Motion - Buzzer OFF
No Motion - Buzzer OFF
Motion Detected - Buzzer ON
Motion Detected - Buzzer ON
No Motion - Buzzer OFF
Program 4: LDR-Based Light
Monitoring
(Using
digital module or ADC-supported setup)
light = 0
if light == 0:
print("Bright Light")
else:
print("Darkness Detected")
Output
Bright Light
Program 5: Temperature-Based Fan
Control
(Logic
illustration)
temperature = 34
if temperature > 30:
print("Temperature
High - Fan ON")
else:
print("Temperature Normal - Fan OFF")
Output
Temperature High - Fan ON
LAB
EXPERIMENTS USING RASPBERRY PI
Experiment 1: Blinking an LED
Aim
To
interface an LED with Raspberry Pi and blink it using Python.
Components Required
- Raspberry Pi
- LED
- 220Ω resistor
- Breadboard
- Jumper wires
Procedure
- Connect LED to GPIO pin 18
through resistor
- Connect cathode to GND
- Write Python code
- Run the program
Expected Output
LED
blinks ON and OFF at 1-second intervals.
Experiment 2: Push Button with
LED
Aim
To
interface a push button and control LED using Raspberry Pi.
Components
- Raspberry Pi
- Push button
- LED
- Resistor
- Breadboard
Procedure
- Connect button to GPIO 23
- Connect LED to GPIO 18
- Write and execute Python
code
Expected Output
When
button is pressed, LED glows. Otherwise, LED remains OFF.
Experiment 3: PIR Sensor Motion
Detection
Aim
To detect
motion using PIR sensor and activate buzzer.
Components
- Raspberry Pi
- PIR sensor
- Buzzer
- Jumper wires
Procedure
- Connect PIR output to GPIO
17
- Connect buzzer to GPIO 27
- Run Python program
Expected Output
When
motion is detected, buzzer sounds.
Experiment 4: LDR-Based Automatic
Light System
Aim
To
monitor light intensity and glow LED in darkness.
Components
- Raspberry Pi
- LDR module
- LED
- Resistor
Procedure
- Connect LDR module to input
pin
- Connect LED to output pin
- Execute program logic
Expected Output
LED turns
ON in darkness and OFF in bright light.
Experiment 5: Relay Control using
Raspberry Pi
Aim
To
control an external electrical device using relay.
Components
- Raspberry Pi
- Relay module
- Bulb/load
- Jumper wires
Procedure
- Connect relay input to GPIO
- Write Python script to
switch relay
- Observe the load
Expected Output
Relay
switches ON/OFF and controls connected device.
TWO-MARK
QUESTIONS
- Define Python and mention
any two features of Python.
- What is Raspberry Pi?
- Expand GPIO and state its
purpose.
- Mention any two applications
of Raspberry Pi in IoT.
- What is the role of Python
in IoT?
- Differentiate between input pin
and output pin in GPIO.
- Write any two advantages of
Raspberry Pi.
- What is meant by sensor
interfacing?
- What is actuator
interfacing?
- Mention any two real-time
IoT applications using Raspberry Pi.
15-MARK QUESTIONS
- Explain Python programming
basics and discuss the importance of Python in IoT application development
using suitable examples.
- Describe Raspberry Pi
architecture and explain the role of GPIO pins in interfacing sensors and
actuators.
- Explain the implementation
of IoT with Raspberry Pi using Python programs for LED, button, and PIR
sensor interfacing.
- Design a Raspberry Pi-based
IoT system and explain its working with suitable block diagram and
applications.
15 MCQs
- Python is a
a. Low-level language
b. High-level language
c. Machine language
d. Assembly language - Raspberry Pi is a
a. Sensor
b. Single-board computer
c. Relay
d. Router - GPIO stands for
a. General Program Input Output
b. General Purpose Input Output
c. Global Pin Input Output
d. General Power Input Output - Which language is commonly
used with Raspberry Pi for IoT?
a. COBOL
b. Python
c. FORTRAN
d. Pascal - Raspberry Pi stores OS in
a. Hard disk
b. CD-ROM
c. Micro SD card
d. RAM - LED is an example of
a. Sensor
b. Input device
c. Actuator
d. Processor - PIR sensor is used to detect
a. Temperature
b. Humidity
c. Motion
d. Pressure - Which pin mode is used to
glow LED?
a. INPUT
b. OUTPUT
c. ANALOG
d. SERIAL - Which function is used to
clean GPIO settings?
a. GPIO.end()
b. GPIO.clear()
c. GPIO.cleanup()
d. GPIO.reset() - A buzzer is a
a. Sensor
b. Output device
c. Input device
d. Storage device - Raspberry Pi can be used as
a. Gateway
b. Cloud only
c. Sensor only
d. Compiler only - Python is preferred in IoT
because it is
a. Complex
b. Easy to use
c. Expensive
d. Slow only - The full form of LED is
a. Light Emitting Device
b. Light Emitting Diode
c. Low Emitting Diode
d. Linear Emitting Device - Which of the following is an
input device?
a. LED
b. Relay
c. PIR sensor
d. Buzzer - Raspberry Pi is mainly used
in IoT for
a. Cooking
b. Data collection and control
c. Printing
d. Typing
Comments
Post a Comment