Build a Temperature Monitoring System Using Zig and Raspberry Pi: Beginners Guide

132 views

Creating a project with Zig on a Raspberry Pi is an exciting way to leverage both the powerful features of the Zig programming language and the flexible hardware capabilities of the Raspberry Pi. Here’s a step-by-step guide to building a simple project:

Project Idea: Temperature Monitoring System

This project will use a temperature sensor (like the DS18B20) connected to a Raspberry Pi. We'll create a Zig program that reads temperature data from the sensor and prints it to the console.

Prerequisites

  1. Hardware:

    • Raspberry Pi (Model 3, 4, or later recommended)
    • DS18B20 temperature sensor
    • 4.7k Ohm resistor
    • Breadboard and jumper wires
  2. Software:

    • Updated Raspberry Pi OS
    • Zig installed on your Raspberry Pi
    • GPIO library for Zig (optional, but recommended)

    You can use a Zig GPIO library like zig-gpio or gpiosim. For this example, we will provide a simple GPIO handling.

Step 1: Setup the Hardware

  1. Connect the DS18B20 sensor to the Raspberry Pi:

    • Pin 1 (GND) to Raspberry Pi GND
    • Pin 2 (DQ) to Raspberry Pi GPIO4 (pin 7)
    • Pin 3 (VDD) to Raspberry Pi 3.3V (pin 1)
    • Place a 4.7k Ohm resistor between DQ and VDD (pins 2 and 3 of the DS18B20)

Step 2: Enable 1-Wire GPIO Interface

  1. Open the Raspberry Pi configuration tool:

    sudo raspi-config
    
  2. Navigate to Interfacing Options and enable 1-Wire.

  3. Reboot the Raspberry Pi:

    sudo reboot
    

Step 3: Write the Zig Program

  1. Create a new directory for your project:

    mkdir zig-temp-monitor
    cd zig-temp-monitor
    
  2. Initialize a new Zig project using Zig’s package manager:

    zig init-exe
    
  3. Edit the src/main.zig file to read temperature data:

    const std = @import("std");
    
    pub fn main() void {
        const sensor_file = "/sys/bus/w1/devices/28-XXXXXXXXXXXX/w1_slave"; // Replace with your specific sensor ID
        
        var file = std.fs.cwd().openRead(sensor_file) catch {
            std.debug.print("Failed to open sensor file.\n", .{});
            return;
        };
        
        var reader = file.reader();
        var buf: [256]u8 = undefined;
        const bytes_read = reader.readAll(buf[0..]) catch {
            std.debug.print("Failed to read sensor data.\n", .{});
            return;
        };
        
        if (bytes_read < 0) {
            std.debug.print("No data read from sensor.\n", .{});
            return;
        }
    
        const data = buf[0..bytes_read];
    
        // Parsing temperature data
        const temp_str_idx = std.mem.indexOf(u8, data, "t=") catch {
            std.debug.print("Temperature data not found.\n", .{});
            return;
        };
    
        const temp_start = temp_str_idx + 2;
    
        var temp_val: i32 = 0;
        const temp_data = data[temp_start..];
    
        temp_val = std.fmt.parseInt(i32, temp_data, 10) catch {
            std.debug.print("Failed to parse temperature data.\n", .{});
            return;
        };
    
        const temperature = temp_val / 1000.0;
    
        std.debug.print("Temperature: {}°C\n", .{temperature});
    }
    
  4. Compile and run your Zig program:

    zig build run
    

If everything is connected and configured correctly, the program will read the temperature from your DS18B20 sensor and print it to the console.

Conclusion

You now have a basic temperature monitoring system running on your Raspberry Pi using Zig. This project can be extended in many ways, such as logging data to a file, sending data to a web server, or adding more sensors. Zig's efficiency and simplicity make it a great choice for such embedded projects. Happy coding!