Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Raspberry Pi!
sidebar:
attrs:
class: pi
---

The Raspberry Pi is an affordable, yet surprisingly powerful, "pocket-sized" computer designed to help you explore coding, electronics, and digital systems.

![Raspberry Pi](./images/raspberrypi.png)
<div class="caption">Image from <a href="https://learn.littlebirdelectronics.com.au/guides/getting-started-with-the-raspberry-pi-4">Little Bird Electronics</a></div>

Despite its small size, the Raspberry Pi is a **fully functional computer** capable of running a Linux-based operating system, such as Raspberry Pi OS (a variant of Debian). This means you get the full experience of coding with a Unix system, combined with the fun of interacting with physical hardware components such as LEDs, buttons, joystick modules and more!

:::note

The Raspberry Pi is not required to learn the coding concepts covered in this book, but with it, you'll get some unique learning opportunities:

- **Hands-On Fun:** You get to see the results of your code come alive — lighting up LEDs, moving motors, or even playing sounds!
- **Explore Embedded Systems:** Discover how code interacts with real-life gadgets and electronics and gain more understanding of how software and hardware can work together.
- **Creative Exploration:** Tinker with sensors, buttons, and displays to create fun projects such as controlling the LEDs on a 7-segment display using inputs from a sensor or your code commands.
- **Easy to Start:** Even if you’re new to programming, the Raspberry Pi offers a gentle introduction with plenty of support when you get stuck.

:::

:::tip[Official Documentation]

The official **[Raspberry Pi Documentation](https://www.raspberrypi.com/documentation/)** includes a variety of useful guides, such as setting up your Raspberry Pi, configuring settings, accessing your Pi remotely, and much more.

:::
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Use Your Raspberry Pi
sidebar:
attrs:
class: pi
---

Inside the [Raspberry Pi](/book/part-0-getting-started/2-computer-use/0-panorama/05-raspberry-pi), it has a CPU (the brain), RAM (its short-term memory), and various ports like HDMI and USB.

But the real magic happens with its GPIO pins!

## Understanding the GPIO Pins

![Raspberry Pi 40-pin header](https://www.raspberrypi.com/documentation/computers/images/GPIO-Pinout-Diagram-2.png)
<div class="caption">Image © Raspberry Pi Ltd, licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> (CC BY-SA)</div>

The **General Purpose Input/Output (GPIO)** pins give you a physical interface between your Raspberry Pi and external hardware components. Here’s how you can use them:

- **Input Mode:**
You can attach sensors (temperature and light sensors) or other input devices such as a joystick, to the pins to allow the Raspberry Pi to read signals from these sensors to understand its environment, or control a player in a game.

- **Output Mode:**
Connect LEDs, motors, buzzers, and more! With a bit of code, you can make an LED blink, a motor spin, or even create sound effects.

- **Digital and PWM Signals:**
Some pins work in a digital on/off mode (perfect for simple tasks), while others support Pulse Width Modulation (PWM), which lets you control the speed of a motor or the brightness of an LED by varying the power output.

With the GPIO pins as your bridge between software and hardware, you can build exciting programs that respond to real-world events!

<!-- Note: The line below is left out for now, but can be added back in eventually once motors and more sensors have been included in activities. -->
<!-- By the end of this book, you could even build a robot that can navigate and react to its surroundings, using motors and sensors. -->

:::note[Getting Started: Set up Your Raspberry Pi]

Before you start exploring the Raspberry Pi activities in this book, you will need to get your Raspberry Pi up and running. We have step-by-step guides for this:

- [Set up my Raspberry Pi](/book/appendix/0-installation/2-3-setup-pi/)

This page includes instructions for setting up the Raspberry Pi OS (including downloading the OS, installing it on your microSD card, and initial configuration) as well as installing the software tools you will need.

*You can skip to [3. Set up Raspberry Pi OS](/book/appendix/0-installation/2-3-setup-pi/#3-setup-raspberry-pi-os) if you have already done the initial OS installation.*

- [Build my Raspberry Pi](/book/appendix/0-installation/2-2-pi-computer)

This page includes information about the Raspberry Pi hardware as well as optional instructions to help you build a small portable setup.

:::

:::tip

*Take your time during setup* and don’t be afraid to explore! Every step (or misstep) is a learning opportunity that will build up your skills and understanding.

:::
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
---
title: Hello LED Blinking
sidebar:
attrs:
class: pi
---

import { Accordion, AccordionItem } from 'accessible-astro-components'
import { Steps } from "@astrojs/starlight/components";

Welcome to our fun, step-by-step guide on how to blink a physical LED using SplashKit on your Raspberry Pi! In this tutorial, you'll learn how to set up your hardware, write simple code to turn your LED on and off multiple times, as well as how to run your project. Get ready to see your LED come to life!

## Hardware Setup

### Components

To build the simple LED circuit shown below (in real life), here's what you need:

- **LED:** Can be any color.
*Remember: the longer leg is the anode (+), the shorter is the cathode (-).*
- **220 Ω Resistor:** This protects the LED by limiting the current.
- **Breadboard and Jumper Wires:** For making temporary connections.
- **Raspberry Pi:** With a GPIO pin (we'll use GPIO Pin 11) and a Ground (GND) pin.

:::note[Just getting started with circuits?]

These sections include links with more information about the circuit components:

<details>
<summary>LEDs and Resistors:</summary>

- [LEDs (Introduction to Raspberry Pi Pico guide)](https://projects.raspberrypi.org/en/projects/introduction-to-the-pico/7)
- [Resistors (Introduction to Raspberry Pi Pico guide)](https://projects.raspberrypi.org/en/projects/introduction-to-the-pico/6)
- [Resistors for LED Circuits](https://eepower.com/resistor-guide/resistor-applications/resistor-for-led/)

</details>
<details>
<summary>Breadboards and Jumper Wires:</summary>

- [How to Use a Breadboard for Electronics and Circuits](https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard)
- [What is inside a breadboard?](https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard#inside-breadboard)
- [How are the holes connected?](https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard#holes)
- [What is a breadboard diagram?](https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard#breadboard-diagram)
- [What are jumper wires and what kind should I use?](https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard#jumper-wires)

</details>
:::

### Wiring Instructions

![Led Blink Circuit](./images/blink-led-circuit.png)
<div class="caption">Image created with <a href="https://fritzing.org/">Fritzing</a></div>

<Steps>

1. **Put the LED and resistor onto the breadboard:**

Place the resistor across the center channel of the breadboard, or across any two separate rows.

Then place the LED across two rows (as shown in the breadboard diagram above), with the cathode (short leg) on the same row as one end of the resistor.

2. **Attach the ground wire:**

Attach a jumper wire from the other end of the resistor to a GND pin.
*(This is the black wire connected to pin 6 in the diagram above.)*

3. **Connect the LED to the GPIO Pin:**

Attach a jumper wire from the LED’s anode (long leg) to GPIO Pin 11.
*(This is the blue wire connected to pin 11 in the diagram above.)*

</Steps>

:::caution[Safety Note:]
Always double-check your connections and ground yourself before handling the circuit to avoid static discharge or accidental damage.
:::

## Software Setup

To set up the software side of this project, you can follow the steps below:

<Steps>

1. ### Create a new C# project

Open your terminal and create a new C# console project:

```shell
mkdir LedBlink
cd LedBlink
dotnet new console
dotnet add package SplashKit
code .
```

2. ### Write the code

Copy the code below, then replace the contents of your `Program.cs` file with this code:

```csharp
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Initialise the GPIO system
RaspiInit();

// Define the LED pin (using physical Pin 11)
GpioPin ledPin = GpioPin.Pin11;

// Set the LED pin to output mode
RaspiSetMode(ledPin, GpioPinMode.GpioOutput);

// Blink 1: Turn LED ON, wait, then OFF
RaspiWrite(ledPin, GpioPinValue.GpioHigh);
Delay(500); // LED ON for 500 ms
RaspiWrite(ledPin, GpioPinValue.GpioLow);
Delay(500); // LED OFF for 500 ms

// Blink 2: Turn LED ON, wait, then OFF
RaspiWrite(ledPin, GpioPinValue.GpioHigh);
Delay(500);
RaspiWrite(ledPin, GpioPinValue.GpioLow);
Delay(500);

// Blink 3: Turn LED ON, wait, then OFF
RaspiWrite(ledPin, GpioPinValue.GpioHigh);
Delay(500);
RaspiWrite(ledPin, GpioPinValue.GpioLow);
Delay(500);

// Clean up the GPIO system
RaspiCleanup();
```

:::note
You will learn more about SplashKit's Raspberry Pi code in Part 1, but for now you can just copy/paste this code without needing to know the details just yet.
:::

3. ### Start the "pigpiod" Daemon

Underneath SplashKit we use the [Pigpio library](https://abyz.me.uk/rpi/pigpio), specifically its
daemon. To interface with this daemon, it must be running.

:::tip[Daemons]
A daemon is a background process that runs continuously, waiting for requests to
perform some action. In this case, the daemon is waiting for requests to change the state of the
GPIO pins. This allows us to run multiple programs that interact with the GPIO pins.
:::

To start the `pigpiod` daemon, run the following command:

```shell
sudo pigpiod
```

To stop the daemon from running, you can use the command:

```shell
sudo killall pigpiod
```

:::note[Pigpiod Error Message]

If the `pigpiod` daemon is not running, you will the following error message in your terminal:

```shell
ERROR [default] Pigpio error: bad connect.
```

:::

4. ### Build and run the project

Use the following command to build and run the program:

```shell
dotnet run
```

</Steps>

## Expected Outcome

When you run the program, your physical LED connected to GPIO Pin 11 should blink on and off three times. Each "on" state lasts for 500 milliseconds followed by an "off" state for 500 milliseconds. Once the sequence is complete, the program cleans up the GPIO settings and exits.

![Led Blinking](./images/led-blink-slow.gif)

Enjoy watching your LED blink and have fun tinkering with your Raspberry Pi projects!
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading