Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
winkj committed Jun 15, 2018
0 parents commit 6363337
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2018, Sensirion AG
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of Sensirion AG nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Arduino library for Sensirion SDP3x and SDP8xx series

This is an unofficial library for Sensirion SDP3x and SDP8xx sensors. Both 500Pa
and 125Pa sensor types are supported.

## Getting started

To get started, have a look at the `sdp_generic` example from the "Examples" menu.
Follow the steps below to initialize for your sensor:

### Initializing for SDP3x on I2C address 0x21

I2C address is the default for the SDP3x series. The `sdp_generic` sample
will do that by default, without requiring any changes.

### Initializing for SDP3x on an I2C address other than 0x21

To use an address other than 0x21, you can pass an argument when setting up
the sensor. In the example, comment out line 6, and uncomment line 10, like so:

```c++
// SDP3XSensor sdp;

// If your SDP3x is not using the default I2C address of 0x21, uncomment the
// line below:
SDP3XSensor sdp(SDP3X_I2C_ADDR_22);
```
Alternatively, you can use `SDP3X_I2C_ADDR_23` instead of `SDP3X_I2C_ADDR_22`
### Initializing for SDP8xx
To use it with an SDP8xx series sensor, comment out line 6, and uncomment
line 14, like so:
```c++
// SDP3XSensor sdp;
// If your SDP3x is not using the default I2C address of 0x21, uncomment the
// line below:
// SDP3XSensor sdp(SDP3X_I2C_ADDR_22);
// If you're using an SDP8xx, uncomment the line below instead:
SDP8XXSensor sdp;
```
50 changes: 50 additions & 0 deletions examples/sdp_generic/sdp_generic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <Wire.h>

#include <sdpsensor.h>

// use this for an SDP3x on the default I2C address of 0x21:
SDP3XSensor sdp;

// If your SDP3x is not using the default I2C address of 0x21, uncomment the
// line below:
// SDP3XSensor sdp(SDP3X_I2C_ADDR_22);


// If you're using an SDP8xx, uncomment the line below instead:
// SDP8XXSensor sdp;


void setup() {
Wire.begin();
Serial.begin(9600);
delay(1000); // let serial console settle

int ret = sdp.init();
if (!ret) {
Serial.print("init(): success\n");
} else {
Serial.print("init(): failed, ret = ");
Serial.println(ret);
while (true) {
delay(1000);
}
}
}

void loop() {
int ret = sdp.readSample();
if (!ret) {
Serial.print("Differential pressure: ");
Serial.print(sdp.getDifferentialPressure());
Serial.print("Pa | ");

Serial.print("Temp: ");
Serial.print(sdp.getTemperature());
Serial.print("C\n");
} else {
Serial.print("Error in readSample(), ret = ");
Serial.println(ret);
}

delay(500);
}
82 changes: 82 additions & 0 deletions i2chelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2015-2018, Sensirion AG <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

<COPYRIGHT HOLDER> -> Sensirion AG

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

... can you git grep for that? I've seen it at least one more file

* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <Arduino.h>
#include <Wire.h>
#include "i2chelper.h"

int8_t I2CHelper::i2c_read(uint8_t addr, uint8_t* data, uint16_t count)

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

i2c_write has a appendCrc flag, would it make sense to add the CRC checks here too? bool checkCrc

{
Wire.requestFrom(addr, count);
if (Wire.available() != count) {
return -1;
}
for (int i = 0; i < count; ++i) {
data[i] = Wire.read();
}
return 0;
}

int8_t I2CHelper::i2c_write(uint8_t addr, const uint8_t* data, uint16_t count, bool appendCrc)
{
Wire.beginTransmission(addr);
for (int i = 0; i < count; ++i) {
if (Wire.write(data[i]) != 1) {
return 1;
}
}
if (appendCrc) {
uint8_t crc = crc8(data, count);
if (Wire.write(crc) != 1) {
return 2;
}
}

if (Wire.endTransmission() != 0) {
return 3;
}
return 0;
}

uint8_t I2CHelper::crc8(const uint8_t* data, uint8_t len)
{
// adapted from SHT21 sample code from http://www.sensirion.com/en/products/humidity-temperature/download-center/

uint8_t crc = 0xff;
uint8_t byteCtr;
for (byteCtr = 0; byteCtr < len; ++byteCtr) {
crc ^= (data[byteCtr]);
for (uint8_t bit = 8; bit > 0; --bit) {
if (crc & 0x80) {
crc = (crc << 1) ^ 0x31;
} else {
crc = (crc << 1);
}
}
}
return crc;
}
40 changes: 40 additions & 0 deletions i2chelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2015-2018, Sensirion AG <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __SDP_I2C_HELPER
#define __SDP_I2C_HELPER

#include <stdint.h>

class I2CHelper
{
public:
static int8_t i2c_write(uint8_t addr, const uint8_t* data, uint16_t count, bool appendCrc=false);
static int8_t i2c_read(uint8_t addr, uint8_t* data, uint16_t count);
static uint8_t crc8(const uint8_t* data, uint8_t len);
};

#endif /* __SCD_I2C_HELPER */
28 changes: 28 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#######################################
# Syntax Coloring Map For arduino-scd
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

SDP3XSensor KEYWORD1
SDP8XXSensor KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

init KEYWORD2
readSample KEYWORD2
getDifferentialPressure KEYWORD2
getHumidity KEYWORD2
getTemperature KEYWORD2

#######################################
# Instances (KEYWORD2)
#######################################

#######################################
# Constants (LITERAL1)
#######################################
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=arduino-sdp
version=0.0.1-pre
author=Johannes Winkelmann
maintainer=Johannes Winkelmann <[email protected]>
sentence=Support for Sensirion's Differential Pressure sensors
paragraph=Supported sensors: SDP3x, SDP8xx
category=Sensors
url=https://developer.sensirion.com
architectures=*
includes=sdpsensor.h
99 changes: 99 additions & 0 deletions sdpsensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2018, Sensirion AG <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <Arduino.h>

#include "sdpsensor.h"
#include "i2chelper.h"

int SDPSensor::init()
{
// try to read product id
const uint8_t CMD_LEN = 2;
uint8_t cmd0[CMD_LEN] = { 0x36, 0x7C };

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

Could be const. Also, the name seems a bit too generic - the datasheet of the SDP800 names it "read product identifier" so maybe read_id_cmd0, read_id_cmd1 ?

uint8_t cmd1[CMD_LEN] = { 0xE1, 0x02 };

const uint8_t DATA_LEN = 18;
uint8_t data[DATA_LEN] = { 0 };

uint8_t ret = I2CHelper::i2c_write(mI2CAddress, cmd0, CMD_LEN);
if (ret != 0) {
return 1;

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

nitpick but since we already have an int we could use negatives for errors..

}
ret = I2CHelper::i2c_write(mI2CAddress, cmd1, CMD_LEN);
if (ret != 0) {
return 2;
}
ret = I2CHelper::i2c_read(mI2CAddress, data, DATA_LEN);
if (ret != 0) {
return 3;
}

// at this point, we don't really care about the data just yet, but
// we may use that in the future. Either way, the sensor responds, and

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

, and - unfinished sentence.

return 0;
}

int SDPSensor::readSample()
{
const uint8_t CMD_LEN = 2;
uint8_t cmd[CMD_LEN] = { 0x36, 0x2F };

const uint8_t DATA_LEN = 9;
uint8_t data[DATA_LEN] = { 0 };

if (I2CHelper::i2c_write(mI2CAddress, cmd, CMD_LEN) != 0) {
return 1;
}

delay(100); // theoretically 45ms

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

wording: instead of theoretically I'd go with something like "datasheet specifies at least 45ms"
exact wording from DS:

During the 45ms that the sensor is measuring, no command can be sent to the sensor. After the 45ms the result can be read out and any command can be sent to the sensor.


if (I2CHelper::i2c_read(mI2CAddress, data, DATA_LEN) != 0) {
return 1;
}

// TODO: check CRC

int16_t dp_raw = (int16_t)data[0] << 8 | data[1];
int16_t temp_raw = (int16_t)data[3] << 8 | data[4];
int8_t dp_scale = (int16_t)data[7] << 8 | data[7];

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

two things: int16_t and | data[8]

Datasheet:

Byte7: Scale Factor differential pressure 8msb
Byte8: Scale Factor differential pressure 8lsb


mDifferentialPressure = dp_raw / (float)dp_scale;
mTemperature = temp_raw / 200.0;

This comment has been minimized.

Copy link
@abrauchli

abrauchli Jul 27, 2018

200.0f to avoid double-precision division


return 0;
}

float SDPSensor::getDifferentialPressure() const
{
return mDifferentialPressure;
}

float SDPSensor::getTemperature() const
{
return mTemperature;
}
Loading

0 comments on commit 6363337

Please sign in to comment.