zh07

package module
v0.0.0-...-22efbe8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2025 License: MIT Imports: 6 Imported by: 0

README

Driver for Winsen ZH06 and ZH07 Laser dust sensor

ZH06 and ZH07 are laser dust sensors module to check air quality.

Communication modes

There are two communication modes supported

  • Initiative upload: the sensor broadcast readings on the tty port on a regular basis
  • Question and answer: we send a command command to request a reading

The mode is selected when requesting an instance of the sensor driver.

// request an instance that will use Q&A communication mode
z0 := zh07.NewZH07q(&zh07.Config{RW: rw})
// request an instance that will use Initiative upload communication mode
z0 := zh07.NewZH07i(&zh07.Config{RW: rw})

// Init must be called to set the mode
if e := z.Init(); e != nil {
	fmt.Printf("%+v\n", e)
}

There is no difference from the user side on using either mode

Sensor models & documentation

I tested the driver using a ZH07 sensor.

There's is only documentation about the ZH06, but the ZH07 is fully compatible with it. The project was developed based on the ZH06 documentation and tested using a ZH07.

In theory, this driver should work with a ZH06 sensor, but I don't have any around to play with it.

Pinout and connection

Pin numbers and orientation Pinout

Pin # Use Comment
1 VDD DC +5v
2 GND Ground
3 - Not connected
4 RXD TTL@3.3v
5 TXD TTL@3.3v
6 - Not connected
7 - Not connected
8 PWM TTL@3.3v

DC 5v must be used to power the sensor as it's needed to drive the internal fan

Typical connection to a MCU that works with 3.3v

Typical connection to a device that works with 5v Some level shifting method is required

Connect to a Raspberry Pi

Golang usage

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"

	"github.com/padiazg/go-zh07"
	"github.com/tarm/serial"
)

func main() {
    // open TTY port
    s, err := serial.OpenPort(&serial.Config{
        Name:     "/dev/serial0",
        Baud:     9600,
        Parity:   serial.ParityNone,
        StopBits: serial.Stop1,
    })
    if err != nil {
        panic(err)
    }

    // we wrap the tty port with a bufio.ReadWriter
    rw := bufio.NewReadWriter(bufio.NewReader(s), bufio.NewWriter(s))

    // create a sensor instance
    z := zh07.NewZH07q(rw)
    if e := z.Init(); e != nil {
        fmt.Fprintf(os.Stderr, "%s\n", e)
        log.Fatal(e)
    }

    r, e := z.Read()
    if e != nil {
        fmt.Printf("Reading from tty: %v\n", e)
        os.Exit(1)
    }
    fmt.Printf("Reading:\nPM 1.0: %d\nPM 2.5: %d\nPM 10 : %d\n\n", r.PM1, r.PM25, r.PM10)
}

A more detailed and complex example can be found at go-zh07-example

Contact

Please use Github issue tracker for filling bugs or feature requests.

License

Go-zh07 is licensed under MIT License.

Documentation

Overview

Package zh07 provides a driver for Winsen ZH06 and ZH07 laser dust sensors.

The ZH06 and ZH07 are laser dust sensor modules used to check air quality by measuring particulate matter concentrations (PM1.0, PM2.5, and PM10).

This package supports two communication modes:

  • Initiative upload mode: The sensor continuously broadcasts readings
  • Question and answer mode: Readings are requested on demand

Example usage:

// Create a sensor instance for Q&A mode
sensor := zh07.NewZH07q(&zh07.Config{RW: rw})
if err := sensor.Init(); err != nil {
	log.Fatal(err)
}

// Read sensor data
reading, err := sensor.Read()
if err != nil {
	log.Fatal(err)
}
fmt.Printf("PM1.0: %d, PM2.5: %d, PM10: %d\n", reading.PM1, reading.PM25, reading.PM10)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChecksumMismatch is returned when the calculated checksum doesn't match the received checksum
	ErrChecksumMismatch = errors.New("checksum mismatch")
	// ErrInvalidFrame is returned when the received data frame is invalid
	ErrInvalidFrame = errors.New("invalid data frame")
	// ErrSensorCommunication is returned when communication with the sensor fails
	ErrSensorCommunication = errors.New("sensor communication failed")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// RW is the ReadWriter interface for communicating with the sensor
	RW *bufio.ReadWriter
}

Config holds configuration options for sensor instances.

type Reading

type Reading struct {
	PM1  int // Mass Concentration PM1.0 [μg/m³]
	PM25 int // Mass Concentration PM2.5 [μg/m³]
	PM10 int // Mass Concentration PM10 [μg/m³]
}

Reading represents a sensor reading with particulate matter concentrations.

type SensorInterface

type SensorInterface interface {
	// Init initializes the sensor and sets the communication mode
	Init() error
	// CalculateChecksum computes the checksum for data validation
	CalculateChecksum() int
	// IsReadingValid checks if the received data has a valid checksum
	IsReadingValid() bool
	// Read returns a sensor reading or an error
	Read() (*Reading, error)
}

SensorInterface defines the common interface for ZH07 sensors.

type ZH07i

type ZH07i struct {
	// contains filtered or unexported fields
}

ZH07i implements the SensorInterface for initiative upload mode. In this mode, the sensor continuously broadcasts readings.

func NewZH07i

func NewZH07i(config *Config) *ZH07i

NewZH07i creates a new ZH07i sensor instance for initiative upload mode.

func (*ZH07i) CalculateChecksum

func (z *ZH07i) CalculateChecksum() int

CalculateChecksum calculates the checksum from the payload. The checksum is calculated by adding all the first 30 bytes of the data received; the last 2 bytes are the checksum.

func (*ZH07i) Init

func (z *ZH07i) Init() error

Init initializes the sensor for initiative upload mode.

func (*ZH07i) IsReadingValid

func (z *ZH07i) IsReadingValid() bool

IsReadingValid checks if the calculated checksum matches the payload checksum.

func (*ZH07i) Read

func (z *ZH07i) Read() (*Reading, error)

Read reads particulate matter data from the sensor in initiative upload mode.

type ZH07q

type ZH07q struct {
	// contains filtered or unexported fields
}

ZH07q implements the SensorInterface for question and answer mode. In this mode, readings are requested on demand.

func NewZH07q

func NewZH07q(config *Config) *ZH07q

NewZH07q creates a new ZH07q sensor instance for question and answer mode.

func (*ZH07q) CalculateChecksum

func (z *ZH07q) CalculateChecksum() int

CalculateChecksum calculates the checksum from the payload.

func (*ZH07q) Init

func (z *ZH07q) Init() error

Init initializes the sensor for question and answer mode.

func (*ZH07q) IsReadingValid

func (z *ZH07q) IsReadingValid() bool

IsReadingValid checks if the calculated checksum matches the payload checksum.

func (*ZH07q) Read

func (z *ZH07q) Read() (*Reading, error)

Read sends a query command and reads particulate matter data from the sensor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL