Created
February 11, 2022 20:37
-
-
Save leonrinkel/17a85beb323a4654e36bc078abcb9879 to your computer and use it in GitHub Desktop.
Arduino <-> Analog Devices ADT7410 Temperature Sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <Wire.h> | |
#define DEVICE_ADDRESS 0x48u | |
#define TEMPERATURE_REGISTER_ADDRESS 0x00u | |
#define STATUS_REGISTER_ADDRESS 0x02u | |
#define CONFIG_REGISTER_ADDRESS 0x03u | |
#define POLL_DELAY 240 /* ms */ | |
typedef union { | |
uint8_t value; | |
struct { | |
uint8_t faultQueue:2; | |
uint8_t ctPinPolarity:1; | |
uint8_t intPinPolarity:1; | |
uint8_t intCtMode:1; | |
uint8_t operationMode:2; | |
uint8_t resolution:1; | |
} fields; | |
} reg_config_t; | |
typedef union { | |
uint8_t value; | |
struct { | |
uint8_t unused:4; | |
uint8_t tLow:1; | |
uint8_t tHigh:1; | |
uint8_t tCrit:1; | |
uint8_t nrdy:1; | |
} fields; | |
} reg_status_t; | |
void readRegister( | |
uint8_t* value, | |
uint8_t deviceAddress, | |
uint8_t registerAddress, | |
uint8_t quantity | |
) { | |
Wire.beginTransmission(deviceAddress); | |
Wire.write(registerAddress); | |
Wire.endTransmission(/* stop: */ (uint8_t) false); | |
Wire.requestFrom( | |
deviceAddress, | |
quantity, | |
/* stop: */ (uint8_t) true | |
); | |
for (; quantity >= 1; quantity--) | |
*(value + quantity - 1) = Wire.read(); | |
} | |
void writeRegister( | |
uint8_t deviceAddress, | |
uint8_t registerAddress, | |
uint8_t value | |
) { | |
Wire.beginTransmission(deviceAddress); | |
Wire.write(registerAddress); | |
Wire.write(value); | |
Wire.endTransmission(/* stop: */ (uint8_t) true); | |
} | |
void setup() { | |
Serial.begin(9600); | |
Wire.begin(); | |
reg_config_t config; | |
readRegister( | |
&config.value, | |
DEVICE_ADDRESS, | |
CONFIG_REGISTER_ADDRESS, | |
/* quantity: */ 1u | |
); | |
if (!config.fields.resolution) { | |
// set resolution to 16-bit | |
config.fields.resolution = 1; | |
writeRegister( | |
DEVICE_ADDRESS, | |
CONFIG_REGISTER_ADDRESS, | |
config.value | |
); | |
} | |
} | |
void loop() { | |
reg_status_t status; | |
readRegister( | |
&status.value, | |
DEVICE_ADDRESS, | |
STATUS_REGISTER_ADDRESS, | |
/* quantity: */ 1u | |
); | |
if (status.fields.nrdy) { | |
delay(POLL_DELAY); | |
return; | |
} | |
int16_t adcCode; | |
readRegister( | |
(uint8_t*) &adcCode, | |
DEVICE_ADDRESS, | |
TEMPERATURE_REGISTER_ADDRESS, | |
/* quantity: */ 2u | |
); | |
float temperature = adcCode / 128.0f; | |
Serial.print("Temperature: "); | |
Serial.print(temperature); | |
Serial.println(" °C"); | |
delay(POLL_DELAY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment