Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: Overview
weight: 2

### FIXED, DO NOT MODIFY
layout: learningpathall
---

## Overview

This Learning Path walks you through deploying an efficient large language model (LLM) locally on the Raspberry Pi 5, powered by an Arm Cortex-A76 CPU. This will allow you to control your smart home using natural language, without relying on cloud services. With rapid advances in Generative AI and the power of Arm Cortex-A processors, you can now run advanced language models directly in your home on the Raspberry Pi 5.

You will create a fully local, privacy-first smart home system that leverages the strengths of Arm Cortex-A architecture. The system can achieve 15+ tokens per second inference speeds using optimized models like TinyLlama and Qwen, while maintaining the energy efficiency that makes Arm processors a good fit for always-on applications.

## Why Arm Cortex-A for Edge AI?

The Raspberry Pi 5's Arm Cortex-A76 processor can manage high-performance computing tasks like AI inference. Key architectural features include:

- The **superscalar architecture** allows the processor to execute multiple instructions in parallel, improving throughput for compute-heavy tasks.
- **128-bit NEON SIMD support** accelerates matrix and vector operations, which are common in the inner loops of language model inference.
- The **multi-level cache hierarchy** helps reduce memory latency and improves data access efficiency during runtime.
- The **thermal efficiency** enables sustained performance without active cooling, making it ideal for compact or always-on smart home setups.

These characteristics make the Raspberry Pi 5 well-suited for workloads like smart home assistants, where responsiveness, efficiency, and local processing are important. Running LLMs locally on Arm-based devices brings several practical benefits. Privacy is preserved, since conversations and routines never leave the device. With optimized inference, the system can offer responsiveness under 100 ms, even on resource-constrained hardware. It remains fully functional in offline scenarios, continuing to operate when internet access is unavailable. Developers also gain flexibility to customize models and automations. Additionally, software updates and an active ecosystem continue to improve performance over time.

## Arm Ecosystem Advantages

For the stack in this setup, Raspberry Pi 5 benefits from the extensive developer ecosystem:

- Optimized compilers including GCC and Clang with Arm-specific enhancements
- Native libraries such as gpiozero and lgpio are optimized for Raspberry Pi
- Community support from open-source projects where developers are contributing Arm-optimized code
- Arm maintains a strong focus on backward compatibility, which reduces friction when updating kernels or deploying across multiple Arm platforms
- The same architecture powers smartphones, embedded controllers, edge devices, and cloud infrastructure—enabling consistent development practices across domains

## Performance Benchmarks on Raspberry Pi 5

The table below shows inference performance for several quantized models running on a Raspberry Pi 5. Measurements reflect single-threaded CPU inference with typical prompt lengths and temperature settings suitable for command-based interaction.

| Model | Tokens/Sec | Avg Latency (ms) |
| ------------------- | ---------- | ---------------- |
| qwen:0.5b | 17.0 | 8,217 |
| tinyllama:1.1b | 12.3 | 9,429 |
| deepseek-coder:1.3b | 7.3 | 22,503 |
| gemma2:2b | 4.1 | 23,758 |
| deepseek-r1:7b | 1.6 | 64,797 |


What does this table tell us? Here are some performance insights:

- Qwen 0.5B and TinyLlama 1.1B deliver fast token generation and low average latency, making them suitable for real-time interactions like voice-controlled smart home commands.
- DeepSeek-Coder 1.3B and Gemma 2B trade off some speed for improved language understanding, which can be useful for more complex task execution or context-aware prompts.
- DeepSeek-R1 7B offers advanced reasoning capabilities with acceptable latency, which may be viable for offline summarization, planning, or low-frequency tasks.

## Supported Arm-Powered Devices

This Learning Path focuses on the Raspberry Pi 5, but you can adapt the concepts and code to other Arm-powered devices:

### Recommended Platforms

| Platform | CPU | RAM | GPIO Support | Model Size Suitability |
|------------------|----------------------------------|----------------|-------------------------------|-----------------------------|
| **Raspberry Pi 5** | Arm Cortex-A76 quad-core @ 2.4GHz | Up to 16GB | Native `lgpio` (high-performance) | Large models (8–16GB) |
| **Raspberry Pi 4** | Arm Cortex-A72 quad-core @ 1.8GHz | Up to 8GB | Compatible with `gpiozero` | Small to mid-size models |
| **Other Arm Devices** | Arm Cortex-A | 4GB min (8GB+ recommended) | Requires physical GPIO pins | Varies by RAM |

Additionally, the platform must:

- GPIO pins available for hardware control
- Use Python 3.8 or newer
- Ability to run [Ollama](https://ollama.com/)

Continue to the next section to start building a smart home system that highlights how Arm-based processors can enable efficient, responsive, and private AI applications at the edge.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
title: Set up software dependencies
weight: 3

### FIXED, DO NOT MODIFY
layout: learningpathall
---

{{% notice Note %}}
This guide assumes you have set up your Raspberry Pi with Raspberry Pi OS and network connectivity. For Raspberry Pi 5 setup help, see: [Raspberry Pi Getting Started](https://www.raspberrypi.com/documentation/)
{{% /notice %}}

## Connect to Your Raspberry Pi 5

### Option 1: Using a display

The easiest way to work on your Raspberry Pi is connecting it to an external display through one of the micro HDMI ports. This setup also requires a keyboard and mouse to navigate.

### Option 2: Using SSH

You can also use SSH to access the terminal. To use this approach you need to know the IP address of your device. Ensure your Raspberry Pi 5 connects to the same network as your host computer. Access your device remotely via SSH using the terminal or any SSH client.

Replace `<user>` with your Pi's username (typically `pi`), and `<pi-ip>` with your Raspberry Pi 5's IP address.

```bash
ssh <user>@<pi-ip>
```

## Set up the dependencies

Create a directory called `smart-home` in your home directory and navigate into it:

```bash
mkdir $HOME/smart-home
cd $HOME/smart-home
```

The Raspberry Pi 5 includes Python 3 pre-installed, but you need additional packages:

```bash
sudo apt update && sudo apt upgrade
sudo apt install python3 python3-pip python3-venv git curl build-essential gcc python3-lgpio
```

### Configure the virtual environment

The next step is to create and activate a Python virtual environment. This approach keeps project dependencies isolated and prevents conflicts with system-wide packages:

```bash
python3 -m venv venv
source venv/bin/activate
```

Install all required libraries and dependencies:

```bash
pip install ollama gpiozero lgpio psutil httpx orjson numpy fastapi uvicorn uvloop numpy
```

### Install Ollama

Install Ollama using the official installation script for Linux:

```bash
curl -fsSL https://ollama.com/install.sh | sh
```

Verify the installation:

```bash
ollama --version
```
If installation was successful, the output from the command should match that below.
```output
ollama version is 0.11.4
```

## Download and Test a Language Model

Ollama supports various models. This guide uses deepseek-r1:7b as an example, but you can also use `tinyllama:1.1b`, `qwen:0.5b`, `gemma2:2b`, or `deepseek-coder:1.3b`.

The `run` command will set up the model automatically. You will see download progress in the terminal, followed by the interactive prompt when ready.

```bash
ollama run deepseek-r1:7b
```

{{% notice Troubleshooting %}}
If you run into issues with the model download, here are some things to check:

- Confirm internet access and sufficient storage space on your microSD card
- Try downloading smaller models like `qwen:0.5b` or `tinyllama:1.1b` if you encounter memory issues. 16 GB of RAM is sufficient for running smaller to medium-sized language models. Very large models may require more memory or run slower.
- Clear storage or connect to a more stable network if errors occur
{{% /notice %}}

With the model set up through `ollama`, move on to the next section to start configuring the hardware.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: Test GPIO pins
weight: 4

### FIXED, DO NOT MODIFY
layout: learningpathall
---

The next step is to test the GPIO functionality. In this section, you will configure a LED light to simulate a smart-home device.

## Verify GPIO Functionality

Bring out your electronics components. Connect the anode (long leg) of an LED in series with a 220Ω resistor to GPIO 17 (physical pin 11). Connect the cathode (short leg) to a ground (GND) pin. See image below for the full setup:

![Raspberry Pi connected to a breadboard with a green LED and jumper wires](pin_layout.jpg "Raspberry Pi connected to a breadboard with a green LED and jumper wires")

Create a Python script named `testgpio.py`:

```bash
cd $HOME/smart-home
vim testgpio.py
```

Copy this code into the file:

```python
#!/usr/bin/env python3
import time
from gpiozero import Device, LED
from gpiozero.pins.lgpio import LGPIOFactory

# Set lgpio backend for Raspberry Pi 5
Device.pin_factory = LGPIOFactory()

# Setup GPIO pin 17
pin1 = LED(17)

try:
while True:
pin1.toggle() # Switch pin 17 state
time.sleep(2) # Wait 2 seconds
except KeyboardInterrupt: # Ctrl+C pressed
pin1.close() # Clean up pin 17
```

Run the script:

```bash
python testgpio.py
```

The LED should blink every two seconds. If you observe this behavior, your GPIO setup works correctly.

{{% notice Troubleshooting %}}
If you run into issues with the hardware setup, here are some things to check:
- Try fixing missing dependencies by running the following command:
```bash
sudo apt-get install -f
```
- If you're running into GPIO permission issues, run Python scripts with `sudo` or add your user to the `gpio` group. Don't forget to log out for the changes to take effect.
```bash
sudo usermod -a -G gpio $USER
```
- Double-check wiring and pin numbers using the Raspberry Pi 5 pinout diagram
- Ensure proper LED and resistor connections
- Verify GPIO enablement in `raspi-config` if needed
- Use a high-quality power supply
{{% /notice %}}

With a way to control devices using GPIO pins, you can move on to the next section to interact with them using language models and the user interface.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
title: Smart Home Assistant
weight: 5

### FIXED, DO NOT MODIFY
layout: learningpathall
---
## About the assistant

In this section, you will run the assistant through the `smart_home_assistant.py` script. It initializes all configured smart devices on specific GPIO pins and starts a local web server for interacting with the assistant. The script processes user commands using a local language model (via Ollama), parses the model’s JSON output, and executes actions such as toggling lights or locking doors. It supports both terminal and web-based control.

The assistant is available on GitHub. Clone the code and navigate to the project directory:

```bash
git clone https://github.com/fidel-makatia/EdgeAI_Raspi5.git
cd EdgeAI_Raspi5
```

## Connect further hardware

In the previous section, you configured a LED on GPIO pin 17. The smart home assistant is by default associating this with a `living_room_light` device. The single LED setup is enough to run through this Learning Path. If you'd like to connect actual devices, or play with more mock sensors, the default configuration looks like the table below. You can repeat the steps on the previous page to verify the hardware setup on the different GPIO pins. See the image below for an example.

| Device Name | GPIO Pin | Type | Room |
| ----------------- | -------- | --------- | ----------- |
| living_room_light | 17 | LIGHT | living_room |
| living_room_fan | 27 | FAN | living_room |
| smart_tv | 22 | SMART_TV | living_room |
| bedroom_light | 23 | LIGHT | bedroom |
| bedroom_ac | 24 | AC | bedroom |
| kitchen_light | 5 | LIGHT | kitchen |
| front_door_lock | 26 | DOOR_LOCK | entrance |
| garden_light | 16 | LIGHT | outdoor |

{{% notice Note %}}
The code uses gpiozero with lgpio backend for Raspberry Pi 5 compatibility. You can use compatible output devices such as LEDs, relays, or small loads connected to these GPIO pins to represent actual smart home devices. All pin assignments are optimized for the Raspberry Pi 5's GPIO layout.
{{% /notice %}}

![Raspberry Pi connected to breadboard with LEDs, buttons, and a sensor module](hardware.jpeg "Setup that includes a blue LED (mapped to Living Room Light on GPIO 17), a red LED, push button, and a sensor module. This setup illustrates a simulated smart home with controllable devices.")


## Run the Smart Home Assistant

Run the assistant in different modes depending on your use case. The default model is `deepseek-coder:1.3b`:

{{< tabpane code=true >}}
{{< tab header="Default (Web API + CLI)" language="bash">}}
python3 smart_home_assistant.py
{{< /tab >}}
{{< tab header="Specify model" language="bash">}}
python3 smart_home_assistant.py --model qwen:0.5b
{{< /tab >}}
{{< tab header="Custom web port" language="bash">}}
python3 smart_home_assistant.py --port 8080
{{< /tab >}}
{{< tab header="CLI only" language="bash">}}
python3 smart_home_assistant.py --no-api
{{< /tab >}}
{{< /tabpane >}}

### Command Options

| Option | Description | Example |
|------------------|---------------------------------------------------------------------------------------------------|--------------------------------------------|
| `--model` | Specify the model to use with Ollama | `--model tinyllama:1.1b` |
| `--port` | Run the web server on a custom port (default: `8000`) | `--port 8080` |
| `--no-api` | Disable the web API and run in CLI-only mode

If everything is set up correctly, you should see the following output on running the default command:

![Running in Default Mode](cmd.png "Running the code in default mode")

## Interact With Your Assistant

Try asking the assistant to `turn on living room light`. If you've connected additional devices, come up with prompts to test the setup.

### Web interface

Open your browser and navigate to `http://0.0.0.0:8000`, or as printed in the terminal output.

![Web Interface Interaction](UI3.png "Interacting with the LLM through the web interface")


### Command line interface

Type commands directly in the terminal.

Sample commands:

```bash
turn on living room light
I want to watch my favorite show
its getting late, secure the house
```

![DeepSeek-Coder Interaction](gemma2.png "Interacting with deepseek-coder:1.3b")

{{% notice Troubleshooting %}}
If you're running into issues with the assistant, here are some things to check:
- Make sure your virtual environment is activated and that you installed all the packages from previous sections
- For model loading problems, check if Ollama is running and list available models:
```bash
ollama list
ollama serve
```
- If port 8000 is unavailable, run the assistant with a different port using the `--port` flag.
{{% /notice %}}

## Wrapping up

From here, you can modify the `smart_home_assistant.py` and extend the system by adding more devices, experimenting with conversational commands, or integrating sensors and automation logic into your smart home setup.

You should now know more about setting up a Raspberry Pi 5 to control real-world devices using GPIO pins, and running a smart home assistant powered by local language models through Ollama. You’ve learned how to wire basic circuits with LEDs and resistors to simulate smart devices, and how to launch and interact with the assistant through both the command-line interface and a web dashboard. Along the way, you also explored common troubleshooting steps for GPIO access, missing dependencies, and model loading issues.

Loading