Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using multiple sensors + low power sleep #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions TSYS01.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,76 @@
#include "TSYS01.h"
#include <Wire.h>

#define TSYS01_ADDR 0x77
#ifdef TSYS01_USE_LOW_POWER
// Use deep sleep instead of delay() to save power. Get the library from https://github.com/LowPowerLab/LowPower
#warning "TSYS01_USE_LOW_POWER: enabled"
#include <LowPower.h>
#endif

#define TSYS01_RESET 0x1E
#define TSYS01_ADC_READ 0x00
#define TSYS01_ADC_TEMP_CONV 0x48
#define TSYS01_PROM_READ 0XA0

TSYS01::TSYS01() {
TSYS01::TSYS01(uint8_t i2cAddress)
: mAddress(i2cAddress)
{}

bool TSYS01::isValid() {
return mAddress != 0;
}

void TSYS01::init() {
Wire.beginTransmission(mAddress);
if (Wire.endTransmission() != 0) {
mAddress = 0;
return;
}

// Reset the TSYS01, per datasheet
Wire.beginTransmission(TSYS01_ADDR);
Wire.beginTransmission(mAddress);
Wire.write(TSYS01_RESET);
Wire.endTransmission();

#ifdef TSYS01_USE_LOW_POWER
LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF);
#else
delay(10);
#endif

// Read calibration values
for ( uint8_t i = 0 ; i < 8 ; i++ ) {
Wire.beginTransmission(TSYS01_ADDR);
Wire.beginTransmission(mAddress);
Wire.write(TSYS01_PROM_READ+i*2);
Wire.endTransmission();

Wire.requestFrom(TSYS01_ADDR,2);
Wire.requestFrom(mAddress, uint8_t(2));
C[i] = (Wire.read() << 8) | Wire.read();
}

}

void TSYS01::read() {

Wire.beginTransmission(TSYS01_ADDR);
Wire.beginTransmission(mAddress);
Wire.write(TSYS01_ADC_TEMP_CONV);
Wire.endTransmission();

delay(10); // Max conversion time per datasheet

Wire.beginTransmission(TSYS01_ADDR);
#ifdef TSYS01_USE_LOW_POWER
LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF);
#else
delay(10); // Max conversion time per datasheet
#endif

Wire.beginTransmission(mAddress);
Wire.write(TSYS01_ADC_READ);
Wire.endTransmission();

Wire.requestFrom(TSYS01_ADDR,3);
Wire.requestFrom(mAddress, uint8_t(3));
D1 = 0;
D1 = Wire.read();
D1 = (D1 << 8) | Wire.read();
D1 = (D1 << 8) | Wire.read();

calculate();
}

Expand Down
5 changes: 4 additions & 1 deletion TSYS01.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ THE SOFTWARE.
class TSYS01 {
public:

TSYS01();

TSYS01(uint8_t i2cAddress = 0x77);

bool isValid();
void init();

/** The read from I2C takes up for 40 ms, so use sparingly is possible.
Expand All @@ -59,6 +61,7 @@ class TSYS01 {
uint32_t D1;
float TEMP;
uint32_t adc;
uint8_t mAddress;

/** Performs calculations per the sensor data sheet for conversion and
* second order compensation.
Expand Down