tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/sgp30/sgp30.go (about)

     1  // SGP30 VOC sensor.
     2  //
     3  // This sensor is marked obsolete by Sensirion, but is still commonly available.
     4  //
     5  // Datasheet: https://sensirion.com/media/documents/984E0DD5/61644B8B/Sensirion_Gas_Sensors_Datasheet_SGP30.pdf
     6  package sgp30
     7  
     8  import (
     9  	"errors"
    10  	"time"
    11  
    12  	"tinygo.org/x/drivers"
    13  )
    14  
    15  const Address = 0x58
    16  
    17  var (
    18  	errInvalidCRC = errors.New("sgp30: invalid CRC")
    19  )
    20  
    21  type Device struct {
    22  	bus         drivers.I2C
    23  	commandBuf  [2]byte
    24  	responseBuf [9]byte
    25  	readyTime   time.Time
    26  	co2eq       uint16
    27  	tvoc        uint16
    28  }
    29  
    30  type Config struct {
    31  	// Nothing to configure right now.
    32  }
    33  
    34  // New returns a new SGP30 driver instance. It does not touch the device yet,
    35  // call Configure to configure this sensor.
    36  func New(bus drivers.I2C) *Device {
    37  	return &Device{
    38  		bus: bus,
    39  		// The sensor has a maximum powerup time of 0.6ms.
    40  		// See table 6 in the datasheet.
    41  		readyTime: time.Now().Add(600 * time.Microsecond),
    42  	}
    43  }
    44  
    45  // Connected returns whether something (probably a SGP30) is present on the bus.
    46  func (d *Device) Connected() bool {
    47  	d.waitUntilReady()
    48  
    49  	// Request serial ID.
    50  	d.commandBuf = [2]byte{0x36, 0x82}
    51  	err := d.bus.Tx(Address, d.commandBuf[:], nil)
    52  	if err != nil {
    53  		return false
    54  	}
    55  
    56  	// Wait 0.5ms as specified in the datasheet.
    57  	time.Sleep(500 * time.Microsecond)
    58  
    59  	// Read the serial ID from the sensor.
    60  	err = d.bus.Tx(Address, nil, d.responseBuf[:9])
    61  	if err != nil {
    62  		return false
    63  	}
    64  
    65  	// Check whether the CRC matches.
    66  	_, ok1 := readWord(d.responseBuf[:3])
    67  	_, ok2 := readWord(d.responseBuf[3:6])
    68  	_, ok3 := readWord(d.responseBuf[6:9])
    69  	ok := ok1 && ok2 && ok3
    70  
    71  	return ok
    72  }
    73  
    74  // Wait until a previous command has completed. This may be necessary on
    75  // startup, for example.
    76  func (d *Device) waitUntilReady() {
    77  	now := time.Now()
    78  	delay := d.readyTime.Sub(now)
    79  	if delay > 0 {
    80  		time.Sleep(delay)
    81  	}
    82  }
    83  
    84  // Configure starts the measurement process for the SGP30 sensor.
    85  func (d *Device) Configure(config Config) error {
    86  	d.waitUntilReady()
    87  
    88  	// Send the sgp30_iaq_init command.
    89  	d.commandBuf = [2]byte{0x20, 0x03}
    90  	err := d.bus.Tx(Address, d.commandBuf[:], nil)
    91  
    92  	// The next command will have to wait at least 10ms.
    93  	d.readyTime = time.Now().Add(10 * time.Millisecond)
    94  
    95  	return err
    96  }
    97  
    98  // Read the current CO₂eq and TVOC values from the sensor.
    99  // This method must be called around once per second per the datasheet as this
   100  // is how the sensor algorithm was calibrated.
   101  func (d *Device) Update(which drivers.Measurement) error {
   102  	d.waitUntilReady()
   103  
   104  	// Send sgp30_measure_iaq command.
   105  	d.commandBuf = [2]byte{0x20, 0x08}
   106  	err := d.bus.Tx(Address, d.commandBuf[:], nil)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	// Wait until the response is ready.
   112  	// This can take up to 12ms according to the datasheet.
   113  	time.Sleep(12 * time.Millisecond)
   114  
   115  	// Read the response.
   116  	data := d.responseBuf[:6]
   117  	err = d.bus.Tx(Address, nil, data)
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	// Decode the response.
   123  	co2eq, ok1 := readWord(data[0:3])
   124  	tvoc, ok2 := readWord(data[3:6])
   125  	if !ok1 || !ok2 {
   126  		return errInvalidCRC
   127  	}
   128  	d.co2eq = co2eq
   129  	d.tvoc = tvoc
   130  
   131  	return nil
   132  }
   133  
   134  // Returns the CO₂ equivalent value read in the previous measurement.
   135  //
   136  // Warning: this is _not_ an actual CO₂ value. The SGP30 can't actually read
   137  // CO₂. Instead, it's an approximation based on various other gases in the
   138  // environment.
   139  func (d *Device) CO2() uint32 {
   140  	return uint32(d.co2eq)
   141  }
   142  
   143  // Returns the total number of VOCs (volatile organic compounds) in parts per
   144  // billion (ppb).
   145  func (d *Device) TVOC() uint32 {
   146  	return uint32(d.tvoc)
   147  }
   148  
   149  // Read a single 16-bit word from the sensor and check the CRC. The data
   150  // parameter must be a slice of 3 bytes.
   151  func readWord(data []byte) (value uint16, ok bool) {
   152  	if len(data) != 3 {
   153  		return 0, false
   154  	}
   155  	value = uint16(data[0])<<8 | uint16(data[1])
   156  	crc := uint8(0xff)
   157  	for i := 0; i < 2; i++ {
   158  		crc ^= data[i]
   159  		for b := 0; b < 8; b++ {
   160  			if crc&0x80 != 0 {
   161  				crc = (crc << 1) ^ 0x31
   162  			} else {
   163  				crc <<= 1
   164  			}
   165  		}
   166  	}
   167  	ok = crc == data[2]
   168  	return
   169  }