Created
June 2, 2015 05:45
-
-
Save monpetit/d5c558c9ee8a522c8902 to your computer and use it in GitHub Desktop.
This file contains 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 <Wire.h> | |
#define I2C_SLAVE_ADDRESS 0x4 // the 7-bit address | |
#define MEASURE_TEMPERATURE 0xA0 | |
#define SEND_TEMPERATURE 0xB0 | |
#define TEMP_CONST 10 // (1 Celcius == 10 mV) | |
#define TEMP_RESOLUTION 1024 // 2^10 (10 bits) | |
#define UNIT_CHANGE 1000 // (1V = 1000 mV) | |
#if defined(ARDUINO_ARCH_ESP8266) || defined(__CC3200R1M1RGC__) | |
#define SUPPLY_VOLTAGE 3.3 // VCC votage for ESP8266 | |
#else | |
#define SUPPLY_VOLTAGE 5.0 // VCC votage for ATmega328p | |
#endif | |
float temp; | |
const float temp_magic_no = (((SUPPLY_VOLTAGE * UNIT_CHANGE) / TEMP_RESOLUTION) / TEMP_CONST); | |
int temperature; | |
volatile uint8_t buffer[2]; | |
void setup() | |
{ | |
Wire.begin(); // join i2c bus (address optional for master) | |
Serial.begin(115200); | |
Serial.println("START..."); | |
} | |
void loop() | |
{ | |
Wire.beginTransmission(I2C_SLAVE_ADDRESS); // transmit to device #4 | |
Wire.write(MEASURE_TEMPERATURE); // sends five bytes | |
Wire.endTransmission(); // stop transmitting | |
delay(100); | |
Wire.beginTransmission(I2C_SLAVE_ADDRESS); // transmit to device #4 | |
Wire.write(SEND_TEMPERATURE); | |
Wire.endTransmission(); | |
Wire.requestFrom(I2C_SLAVE_ADDRESS, 2); | |
buffer[0] = Wire.read(); | |
buffer[1] = Wire.read(); | |
temperature = (buffer[0] << 8) | buffer[1]; | |
Serial.print("raw adc value = "); | |
Serial.println(temperature); | |
temp = temperature * temp_magic_no; | |
Serial.print("current temperature = "); | |
Serial.println(temp); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment