Advent of Embedded Linux — Day 5: Setting Up the Raspberry Pi

Day 5 — Setting Up the Raspberry Pi with a Real Temperature Sensor

Overview

Welcome back! Today marks a significant milestone in our Advent of Embedded Linux journey — we’re moving from virtual environments to real hardware. We’ll set up a Raspberry Pi with a DS18B20 temperature sensor and build a custom Linux image using the Yocto Project.

Unlike our previous QEMU-based posts, this tutorial involves actual hardware interaction. The DS18B20 is a temperature sensor that communicates over the 1-Wire protocol, making it perfect for embedded Linux projects. By the end of this post, you’ll have a fully functional temperature sensing system running on your Raspberry Pi.

What You Will Do

Prerequisites

Hardware Setup

DS18B20 Sensor Overview

The DS18B20 is a 1-Wire temperature sensor that provides 9-bit to 12-bit temperature measurements. It’s widely used in embedded systems due to its:

Wiring Diagram

Connect the DS18B20 to your Raspberry Pi as follows:

DS18B20 Pin Wire Color (typical) Raspberry Pi Pin
VCC Red 3.3V (Pin 1)
GND Black GND (Pin 6)
DQ (Data) Yellow GPIO4 (Pin 7)

Important: Place a 4.7kΩ pull-up resistor between the DQ (Data) line and the VCC (3.3V) line. This resistor is essential for proper 1-Wire communication. GPIO4 is the default pin used for the 1-Wire communication.

DS18B20 setup

Accessing via UART Console

I am currently accessing the Raspberry Pi using the UART console as it is often easier to catch early boot errors instead of waiting for SSH to become active. Also, SSH is not available by default in the Yocto image. If you want SSH access, you can enable it by modifying the kas configuration.

Building the Image

Step 1: Setting Up the Yocto Build Environment

In the previous post, we created an image for the RISC-V architecture. Similarly, try to create a kas configuration file for generating the image for our board.

I have updated the meta-advent layer to include the configuration for the Raspberry Pi. Just do a git pull!

kas build kas/build-rpi.yml

This command will build the core-image-minimal.

Step 2: Flashing the Image

Once the build completes, locate the output image:

ls build/tmp/deploy/images/<your-pi-model>/

The output is a .wic.bz2 file along with a .wic.bmap file, which can be flashed using bmaptool. Alternatively, extract the .bz2 image and flash it using the Raspberry Pi Imager.

Step 3: Verifying the 1-Wire Interface

Once booted, verify that the 1-Wire interface is active and the sensor is detected:

# Check if the w1 bus is available
ls /sys/bus/w1/devices/

You should see a directory starting with 28- followed by a unique identifier — this is your DS18B20 sensor. The 28 prefix indicates a DS18B20 family device.

You can navigate to the sensor’s directory and read the temperature:

cat /sys/bus/w1/devices/28-*/w1_slave

You’ll see output similar to:

70 01 55 05 7f a5 a5 66 c7 : crc=c7 YES
70 01 55 05 7f a5 a5 66 c7 t=23000

Understanding the Output

Expected Outcome

After completing this tutorial, you should have:

  1. A Raspberry Pi running a custom Yocto-built Linux image
  2. A DS18B20 temperature sensor properly wired and detected
  3. The ability to read real temperature data via the sysfs interface
  4. Understanding of the 1-Wire protocol and its Linux implementation

When reading from the sensor, you should see output like:

70 01 55 05 7f a5 a5 66 c7 : crc=c7 YES
70 01 55 05 7f a5 a5 66 c7 t=23000

This indicates:

Troubleshooting

Sensor not detected (no 28-* directory):

Build fails:

Image doesn’t boot:

What’s Next?

Now that we have real hardware working with a custom Linux image, future posts will explore:

Extras