0% found this document useful (0 votes)
2 views

iot_file

The document outlines a series of experiments using a Raspberry Pi to familiarize users with Linux commands, Python programming, and GPIO control. It includes step-by-step instructions for executing basic and advanced Python scripts, as well as controlling LEDs and switches. Each experiment is designed to enhance understanding of both the Raspberry Pi hardware and software capabilities.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

iot_file

The document outlines a series of experiments using a Raspberry Pi to familiarize users with Linux commands, Python programming, and GPIO control. It includes step-by-step instructions for executing basic and advanced Python scripts, as well as controlling LEDs and switches. Each experiment is designed to enhance understanding of both the Raspberry Pi hardware and software capabilities.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment 1: Start Raspberry Pi and Try Various Linux Commands

Objective:
Familiarize yourself with basic Linux commands on the Raspberry Pi.

Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse

Steps:
1. Booting Up the Raspberry Pi:
• Connect your Raspberry Pi to the monitor, keyboard, and mouse.
• Power on the Raspberry Pi.
• Log in with your username and password.
2. Opening the Terminal:
• Open the Terminal application from the desktop environment.
3. Executing Basic Commands:
• ls: List directory contents
sh
Copy code
ls

This command lists all files and directories in the current directory.
• cd: Change directory
sh
Copy code
cd /path/to/directory

Use this command to navigate to a different directory.


• touch: Create an empty file
sh
Copy code
touch myfile.txt

This command creates a new, empty file named myfile.txt.

• mv: Move or rename a file or directory


sh
Copy code
mv myfile.txt mynewfile.txt

This renames myfile.txt to mynewfile.txt.


• rm: Remove a file
sh
Copy code
rm mynewfile.txt

This command deletes mynewfile.txt.


• man: Display the manual for a command
sh
Copy code
man ls

This shows the manual page for the ls command.


• mkdir: Create a directory
sh
Copy code
mkdir mydirectory

This creates a new directory named mydirectory.


• rmdir: Remove an empty directory
sh
Copy code
rmdir mydirectory

This removes the directory named mydirectory.


• tar: Archive files
sh
Copy code
tar -cvf archive.tar /path/to/directory

This creates an archive of the specified directory.


• gzip: Compress a file
sh
Copy code
gzip archive.tar

This compresses the archive.tar file into archive.tar.gz.


• cat: Concatenate and display file content
sh
Copy code
cat myfile.txt

This displays the contents of myfile.txt.


• more: View file content one screen at a time
sh
Copy code
more myfile.txt
This lets you view myfile.txt content page by page.
• less: View file content with backward movement
sh
Copy code
less myfile.txt

This is similar to more, but with the ability to scroll backwards.


• ps: Display current processes
sh
Copy code
ps aux

This shows a detailed list of all running processes.


• sudo: Execute a command as the superuser
sh
Copy code
sudo apt-get update

This runs the apt-get update command with superuser privileges.


• cron: Schedule tasks to run at specific intervals
sh
Copy code
crontab -e

This opens the cron table for editing, allowing you to schedule tasks.
• chown: Change file owner
sh
Copy code
sudo chown newuser myfile.txt

This changes the owner of myfile.txt to newuser.


• chgrp: Change file group
sh
Copy code
sudo chgrp newgroup myfile.txt

This changes the group ownership of myfile.txt to newgroup.


• ping: Check network connectivity
sh
Copy code
ping google.com

This sends ICMP ECHO_REQUEST packets to google.com to check


connectivity.
Experiment 2: Run Some Python Programs on Raspberry Pi
Objective:
Run basic Python programs on the Raspberry Pi to familiarize yourself with Python scripting and
input/output operations.

Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse

Steps:
1. Boot Up the Raspberry Pi and Open Terminal:
• Power on your Raspberry Pi.
• Open the Terminal application.
2. Run the Python Programs:
• a) Read your name and print Hello message with name
python
Copy code
# hello_name.py
name = input("Enter your name: ")
print(f"Hello, {name}!")

Running the Program:


sh
Copy code
python3 hello_name.py

• b) Read two numbers and print their sum, difference, product, and division
python
Copy code
# arithmetic_operations.py
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

sum_result = num1 + num2


diff_result = num1 - num2
prod_result = num1 * num2
div_result = num1 / num2 if num2 != 0 else "undefined"

print(f"Sum: {sum_result}")
print(f"Difference: {diff_result}")
print(f"Product: {prod_result}")
print(f"Division: {div_result}")

Running the Program:


sh
Copy code
python3 arithmetic_operations.py

• c) Word and character count of a given string


python
Copy code
# word_char_count.py
input_string = input("Enter a string: ")

word_count = len(input_string.split())
char_count = len(input_string)

print(f"Word count: {word_count}")


print(f"Character count: {char_count}")

Running the Program:


sh
Copy code
python3 word_char_count.py

• d) Area of a given shape (rectangle, triangle, and circle) reading shape and
appropriate values from standard input
python
Copy code
# area_of_shapes.py
import math

shape = input("Enter the shape (rectangle, triangle, circle):


").lower()

if shape == "rectangle":
length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
area = length * width
elif shape == "triangle":
base = float(input("Enter the base: "))
height = float(input("Enter the height: "))
area = 0.5 * base * height
elif shape == "circle":
radius = float(input("Enter the radius: "))
area = math.pi * (radius ** 2)
else:
area = None
print("Invalid shape entered.")

if area is not None:


print(f"The area of the {shape} is: {area}")

Running the Program:


sh
Copy code
python3 area_of_shapes.py
Experiment 3: Run Some Advanced Python Programs on Raspberry Pi
Objective:
Run advanced Python programs on the Raspberry Pi to familiarize yourself with loops, exception
handling, time operations, and file handling.

Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse

Steps:
1. Boot Up the Raspberry Pi and Open Terminal:
• Power on your Raspberry Pi.
• Open the Terminal application.
2. Run the Python Programs:
• a) Print a name 'n' times, where name and n are read from standard input,
using for and while loops
Using For Loop:
python
Copy code
# print_name_for.py
name = input("Enter your name: ")
n = int(input("Enter the number of times to print your name: "))

for i in range(n):
print(name)

Using While Loop:


python
Copy code
# print_name_while.py
name = input("Enter your name: ")
n = int(input("Enter the number of times to print your name: "))

count = 0
while count < n:
print(name)
count += 1

Running the Programs:


sh
Copy code
python3 print_name_for.py
python3 print_name_while.py

• b) Handle Division by Zero Exception


python
Copy code
# handle_division_by_zero.py
try:
num1 = float(input("Enter the numerator: "))
num2 = float(input("Enter the denominator: "))
result = num1 / num2
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
else:
print(f"The result of the division is: {result}")

Running the Program:


sh
Copy code
python3 handle_division_by_zero.py

• c) Print current time for 10 times with an interval of 10 seconds


python
Copy code
# print_current_time.py
import time
from datetime import datetime

for i in range(10):
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Current time: {current_time}")
time.sleep(10)

Running the Program:


sh
Copy code
python3 print_current_time.py

• d) Read a file line by line and print the word count of each line
Creating a Sample File: Create a file named sample.txt with some text in it.
sh
Copy code
echo "Hello world\nThis is a sample file\nIt has multiple lines\
nEach with words" > sample.txt

Reading the File and Printing Word Count:


python
Copy code
# word_count_per_line.py
with open("sample.txt", "r") as file:
for line in file:
word_count = len(line.split())
print(f"Line: {line.strip()} - Word count: {word_count}")

Running the Program:


sh
Copy code
python3 word_count_per_line.py
Experiment 4: Control LEDs and Switches with Raspberry Pi
Objective:
Use the GPIO pins on the Raspberry Pi to control LEDs and read input from switches using Python
programs.

Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse
• Breadboard
• Jumper wires
• LEDs (at least 2)
• Resistors (220Ω)
• Push-button switches (at least 2)

Steps:
1. Boot Up the Raspberry Pi and Open Terminal:
• Power on your Raspberry Pi.
• Open the Terminal application.
2. Install Required Python Library:
• Ensure the RPi.GPIO library is installed. You can install it using:
sh
Copy code
sudo apt-get update
sudo apt-get install python3-rpi.gpio

3. Run the Python Programs:


• a) Light an LED through Python program
Circuit Setup:
• Connect the longer leg (anode) of the LED to a GPIO pin (e.g., GPIO17).
• Connect the shorter leg (cathode) of the LED to a resistor, and then to the
ground (GND).
Python Program:
python
Copy code
# light_led.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

GPIO.output(17, GPIO.HIGH) # Turn on the LED


time.sleep(5) # Keep the LED on for 5 seconds
GPIO.output(17, GPIO.LOW) # Turn off the LED
GPIO.cleanup()

Running the Program:


sh
Copy code
python3 light_led.py

• b) Get input from two switches and switch on corresponding LEDs


Circuit Setup:
• Connect two push-button switches to GPIO pins (e.g., GPIO18 and GPIO23)
and ground.
• Connect two LEDs to GPIO pins (e.g., GPIO24 and GPIO25) and ground via
resistors.
Python Program:
python
Copy code
# switch_control_leds.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# Setup switches
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# Setup LEDs
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

try:
while True:
if GPIO.input(18) == GPIO.HIGH:
GPIO.output(24, GPIO.HIGH)
else:
GPIO.output(24, GPIO.LOW)

if GPIO.input(23) == GPIO.HIGH:
GPIO.output(25, GPIO.HIGH)
else:
GPIO.output(25, GPIO.LOW)

time.sleep(0.1)

except KeyboardInterrupt:
pass

GPIO.cleanup()

Running the Program:


sh
Copy code
python3 switch_control_leds.py
• c) Flash an LED at a given on time and off time cycle, where the two times are
taken from a file
Circuit Setup:
• Connect an LED to a GPIO pin (e.g., GPIO17) and ground via a resistor.
Create the Configuration File:
• Create a file named led_config.txt with the following content (example
values):
txt
Copy code
1 # On time in seconds
2 # Off time in seconds

Python Program:
python
Copy code
# flash_led_from_file.py
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

with open("led_config.txt", "r") as file:


on_time = float(file.readline().strip())
off_time = float(file.readline().strip())

try:
while True:
GPIO.output(17, GPIO.HIGH)
time.sleep(on_time)
GPIO.output(17, GPIO.LOW)
time.sleep(off_time)

except KeyboardInterrupt:
pass

GPIO.cleanup()

Running the Program:


sh
Copy code
python3 flash_led_from_file.py

You might also like