Language: EN

raspberry-pi-cheatsheet

Raspberry Pi Cheatsheet

Initial Setup

Install Operating System

Download Raspberry Pi OS (or another compatible OS) and use Raspberry Pi Imager or balenaEtcher to write it to a microSD card.

Access via SSH

If you don’t have a screen connected, enable SSH by creating an empty file named ssh in the /boot partition of the microSD card.

bash

touch /media/boot/ssh

Configure Wi-Fi

Create a wpa_supplicant.conf file in the /boot partition to configure the Wi-Fi connection:

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="Your_SSID"
    psk="Your_Password"
    key_mgmt=WPA-PSK
}

Basic Raspberry Pi Commands

Update System

Update the system and installed packages.

sudo apt update && sudo apt upgrade

Reboot and Shutdown

Reboot or shut down your Raspberry Pi from the command line.

sudo reboot
sudo shutdown -h now

Expand Filesystem

To take advantage of all the available space on the microSD.

sudo raspi-config --expand-rootfs

Configure System

Open the configuration menu to change options like language, keyboard, hostname, etc.

sudo raspi-config

GPIO Management

Install GPIO Libraries

Install the library to control the GPIO pins of the Raspberry Pi.

sudo apt install python3-rpi.gpio

Control GPIO from Python

Basic example to turn on and off an LED connected to a GPIO pin.

import RPi.GPIO as GPIO
import time

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

GPIO.output(18, GPIO.HIGH)  # Turn on LED
time.sleep(1)
GPIO.output(18, GPIO.LOW)   # Turn off LED

GPIO.cleanup()

Read GPIO Input

Example to read the state of a button connected to a GPIO pin.

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    if GPIO.input(17) == GPIO.LOW:
        print("Button pressed")

Remote Access and Administration

Access via VNC

Enable VNC on the Raspberry Pi for remote graphical access.

sudo raspi-config
# Navigate to "Interfacing Options" > "VNC" and enable it.

Connect using a VNC client like RealVNC Viewer.

Remote Connection via SSH

Connect to your Raspberry Pi using SSH from another machine.

ssh pi@raspberrypi.local

If the hostname is different, replace raspberrypi.local with the correct IP or name.

Package and Software Management

Install Additional Software

Use apt to install additional software like Node.js, Python, or web servers.

sudo apt install package-name

Service Management

Control services like Apache, MySQL, or SSH.

sudo systemctl start service-name
sudo systemctl stop service-name
sudo systemctl enable service-name  # Start on boot

System Monitoring

Check CPU and Memory Usage

Get a summary of system resource usage.

htop

Monitor Temperature

Check the CPU temperature.

vcgencmd measure_temp

Peripheral Access

Camera

Enable Camera

Enable the camera module from raspi-config.

sudo raspi-config
# Navigate to "Interfacing Options" > "Camera" and enable it.

Capture Image with Camera

Use raspistill to take a photo.

raspistill -o image.jpg

Record Video

Use raspivid to record video with the camera.

raspivid -o video.h264 -t 10000  # Record for 10 seconds

I2C and SPI

Enable I2C and SPI

Enable the I2C and SPI interfaces in raspi-config.

sudo raspi-config
# Navigate to "Interfacing Options" > "I2C" or "SPI" and enable them.

Check Connected I2C Devices

Check the devices connected to the I2C interface.

sudo i2cdetect -y 1

Automation and Scripts

Configure Scripts at Startup

Add scripts to /etc/rc.local or create a systemd service to run them at startup.

sudo nano /etc/rc.local
# Add your command before the line "exit 0"

Create Scheduled Tasks with Cron

Use cron to run tasks automatically at regular intervals.

crontab -e
# Example: run a script every day at 3 AM
0 3 * * * /path/to/script.sh