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

  1. Connect sensor/actuator to Raspberry Pi
  2. Write Python program
  3. Read input from sensor
  4. Process data
  5. Perform output action
  6. 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

  1. Connect LED to GPIO pin 18 through resistor
  2. Connect cathode to GND
  3. Write Python code
  4. 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

  1. Connect button to GPIO 23
  2. Connect LED to GPIO 18
  3. 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

  1. Connect PIR output to GPIO 17
  2. Connect buzzer to GPIO 27
  3. 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

  1. Connect LDR module to input pin
  2. Connect LED to output pin
  3. 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

  1. Connect relay input to GPIO
  2. Write Python script to switch relay
  3. Observe the load

Expected Output


Relay switches ON/OFF and controls connected device.


TWO-MARK QUESTIONS

  1. Define Python and mention any two features of Python.
  2. What is Raspberry Pi?
  3. Expand GPIO and state its purpose.
  4. Mention any two applications of Raspberry Pi in IoT.
  5. What is the role of Python in IoT?
  6. Differentiate between input pin and output pin in GPIO.
  7. Write any two advantages of Raspberry Pi.
  8. What is meant by sensor interfacing?
  9. What is actuator interfacing?
  10. Mention any two real-time IoT applications using Raspberry Pi.

15-MARK QUESTIONS

  1. Explain Python programming basics and discuss the importance of Python in IoT application development using suitable examples.
  2. Describe Raspberry Pi architecture and explain the role of GPIO pins in interfacing sensors and actuators.
  3. Explain the implementation of IoT with Raspberry Pi using Python programs for LED, button, and PIR sensor interfacing.
  4. Design a Raspberry Pi-based IoT system and explain its working with suitable block diagram and applications.

15 MCQs

  1. Python is a
    a. Low-level language
    b. High-level language
    c. Machine language
    d. Assembly language
  2. Raspberry Pi is a
    a. Sensor
    b. Single-board computer
    c. Relay
    d. Router
  3. GPIO stands for
    a. General Program Input Output
    b. General Purpose Input Output
    c. Global Pin Input Output
    d. General Power Input Output
  4. Which language is commonly used with Raspberry Pi for IoT?
    a. COBOL
    b. Python
    c. FORTRAN
    d. Pascal
  5. Raspberry Pi stores OS in
    a. Hard disk
    b. CD-ROM
    c. Micro SD card
    d. RAM
  6. LED is an example of
    a. Sensor
    b. Input device
    c. Actuator
    d. Processor
  7. PIR sensor is used to detect
    a. Temperature
    b. Humidity
    c. Motion
    d. Pressure
  8. Which pin mode is used to glow LED?
    a. INPUT
    b. OUTPUT
    c. ANALOG
    d. SERIAL
  9. Which function is used to clean GPIO settings?
    a. GPIO.end()
    b. GPIO.clear()
    c. GPIO.cleanup()
    d. GPIO.reset()
  10. A buzzer is a
    a. Sensor
    b. Output device
    c. Input device
    d. Storage device
  11. Raspberry Pi can be used as
    a. Gateway
    b. Cloud only
    c. Sensor only
    d. Compiler only
  12. Python is preferred in IoT because it is
    a. Complex
    b. Easy to use
    c. Expensive
    d. Slow only
  13. The full form of LED is
    a. Light Emitting Device
    b. Light Emitting Diode
    c. Low Emitting Diode
    d. Linear Emitting Device
  14. Which of the following is an input device?
    a. LED
    b. Relay
    c. PIR sensor
    d. Buzzer
  15. Raspberry Pi is mainly used in IoT for
    a. Cooking
    b. Data collection and control
    c. Printing
    d. Typing

Comments

Popular posts from this blog

Unit 2 Data Link Layer - Functions and its Prototocols

Computer Networks

UNIT I INTRODUCTION TO DEEP LEARNING