Skip to main content

Command Palette

Search for a command to run...

Rpi Basics

Updated
β€’5 min read

🐧 Day 2: Linux & Terminal Basics on Raspberry Pi


🎯 Learning Objectives

By the end of today, learners will:

  • Navigate the Linux filesystem using the terminal.

  • Use essential Linux commands for managing files and directories.

  • Install and remove software using apt.

  • Understand file permissions and user roles.

  • Enable and use SSH & VNC to remotely access the Raspberry Pi.


ssh pi1@raspberry1.local

🧠 Topics & Content


πŸ—‚οΈ 1. Linux Filesystem Structure

Explain the hierarchy using a tree:

/
β”œβ”€β”€ bin        # Essential binary programs
β”œβ”€β”€ boot       # Boot loader files
β”œβ”€β”€ dev        # Device files
β”œβ”€β”€ etc        # System configuration files
β”œβ”€β”€ home       # User home directories
β”‚   └── pi     # Home directory for user 'pi'
β”œβ”€β”€ lib        # System libraries
β”œβ”€β”€ media      # Mounted drives (USB, SD)
β”œβ”€β”€ opt        # Optional application software
β”œβ”€β”€ tmp        # Temporary files
β”œβ”€β”€ usr        # User programs and data
└── var        # Variable data (e.g., logs)

βœ… Tip: Your working directory is usually /home/pi
Use pwd to check where you are.


πŸ’» 2. Basic Linux Commands

Practice the following:

CommandDescription
pwdPrint current directory
lsList contents
cd foldernameChange directory
cd ..Go to parent directory
mkdir nameCreate new folder
rm fileDelete file
rm -r folderDelete folder recursively
nano file.txtOpen file in editor
topShow system processes

Practice Task:

cd ~
mkdir day2
cd day2
nano hello.txt

πŸ“¦ 3. Package Management with APT

APT (Advanced Package Tool) is used for installing and managing software.

  • Update package list:

      sudo apt update
    
  • Upgrade installed packages:

      dsudo apt upgrade -y
    
  • Install a package:

      sudo apt install cowsay
      cowsay "Linux is fun!"
    
  • Remove a package:

      sudo apt remove cowsay
    

πŸ” Explore: Try installing neofetch, htop, or python3-pip


πŸ” 4. File Permissions and Users

Use ls -l to check permissions:

-rw-r--r-- 1 pi pi  1234 Jun 14  hello.txt

Breakdown:

  • rw- β†’ owner can read/write

  • r-- β†’ group can read

  • r-- β†’ others can read

Change permissions:

chmod 755 myscript.sh

Change ownership:

sudo chown pi:pi myfile

User management:

whoami
sudo adduser student

πŸ”— 5. Remote Access: SSH and VNC

  • SSH (for terminal access over network)

  • VNC (for GUI desktop access)

βœ… Enable SSH & VNC:

sudo raspi-config
# Go to "Interface Options" > Enable SSH and VNC

Alternatively:

sudo systemctl enable ssh
sudo systemctl start ssh

πŸ–₯️ Install VNC Viewer on PC/Mac:

To check IP:

hostname -I

πŸ› οΈ Activities

  1. Create and Navigate Folders
mkdir ~/projects/day2
cd ~/projects/day2
  1. Edit and View Files
nano notes.txt
cat notes.txt
  1. Install Software
sudo apt install figlet
figlet "Hello Pi"
  1. Enable SSH & VNC and connect from another device

πŸ“‹ Homework

  • Try installing any fun terminal program (cmatrix, sl, etc.)

  • Create a bash script named myinfo.sh that displays:

    • Current user (whoami)

    • Date/time (date)

    • IP address (hostname -I)


βœ… Summary

  • Terminal is your gateway to full control in Linux.

  • You can move, create, and edit files efficiently.

  • With apt, you can install nearly anything.

  • SSH and VNC let you work remotely from any device.

LED Blinking with Raspberry Pi

To make an LED blink on a Raspberry Pi, you'll need:


🧰 Hardware Requirements

  • Raspberry Pi (any model with GPIO)

  • 1x LED

  • 1x 330Ξ© resistor

  • Breadboard and jumper wires


βš™οΈ Wiring

Connect like this:

LED PinConnects to
Anode (+)GPIO pin (e.g., GPIO 17) via resistor
Cathode (–)GND on Raspberry Pi

πŸ“Œ Example:

  • GPIO 17 β†’ Resistor β†’ LED anode

  • LED cathode β†’ GND


You can use the gpiozero or RPi.GPIO library.

βœ… Using gpiozero (simpler):

from gpiozero import LED
from time import sleep

led = LED(17)  # GPIO pin 17

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

βœ… Using RPi.GPIO:

import RPi.GPIO as GPIO
import time

LED_PIN = 17

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

try:
    while True:
        GPIO.output(LED_PIN, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(LED_PIN, GPIO.LOW)
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()

▢️ To Run the Script

  1. Save it as blink.py

  2. Open terminal and run:

python3 blink.py

Let me know if you want to:

  • Control the LED via a web interface

  • Blink with different patterns

  • Use a button to control it


LED.html

<!DOCTYPE html>
<html>
<head>
    <title>LED Control</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding-top: 50px;
        }
        button {
            padding: 15px 30px;
            font-size: 18px;
            margin: 10px;
            cursor: pointer;
            border: none;
            border-radius: 8px;
        }
        .on {
            background-color: #4CAF50;
            color: white;
        }
        .off {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>
<body >
    <h1>LED Control</h1>
    <p>Current LED status: <strong>{{ 'ON' if status else 'OFF' }}</strong></p>
    <a href="/on"><button class="on">Turn ON</button></a>
    <a href="/off"><button class="off">Turn OFF</button></a>
</body>
</html>

flask_led.py

from flask import Flask, render_template
import RPi.GPIO as GPIO

app = Flask(__name__)

# GPIO setup
LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)

def is_led_on():
    return GPIO.input(LED_PIN) == GPIO.HIGH

@app.route('/')
def index():
    return render_template('led.html', status=is_led_on())

@app.route('/on')
def turn_on():
    GPIO.output(LED_PIN, GPIO.HIGH)
    return render_template('led.html', status=is_led_on())

@app.route('/off')
def turn_off():
    GPIO.output(LED_PIN, GPIO.LOW)
    return render_template('led.html', status=is_led_on())

@app.route('/cleanup')
def cleanup():
    GPIO.cleanup()
    return "GPIO Cleaned up!"

if __name__ == "__main__":
    try:
        app.run(host="0.0.0.0", port=5000, debug=True)
    finally:
        GPIO.cleanup()

More from this blog

Aarav's Blogs

19 posts