-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_test_th10.ino
78 lines (59 loc) · 2.29 KB
/
i2c_test_th10.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* HopeRF TH10 i2c Temperature and Humidity Reader
* Uses code from:
* I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
* by Nicholas Zambetti <http://www.zambetti.com>
* and James Tichenor <http://www.jamestichenor.net>
*
* This example code is in the public domain.
*/
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
pinMode(SDA, INPUT);
pinMode(SCL, INPUT);
Serial.begin(9600); // start serial communication at 9600bps
Serial.write("i2c test TH10 v1.0\n");
}
long reading = 0;
void loop() {
Serial.println("Start Loop");
int address = 0x44; // transmit to device (0x00 or 0x88)
// step 1: instruct sensor to start measurement
Wire.beginTransmission(address);
Wire.write(byte(0x24)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x16));
Wire.endTransmission(); // stop transmitting
Serial.println("Transmission Sent");
// step 2: wait for readings to happen
delay(1000); // datasheet suggests at least 65 milliseconds
int bytes_receive = 6;
// step 3: request reading from sensor
Wire.requestFrom(0x44, bytes_receive); // request 6 bytes from device
Serial.println("Read data");
int bytes_received = Wire.available();
/*
Serial.print("b: ");
Serial.print(bytes_received);
Serial.print("/");
Serial.print(bytes_receive);
Serial.println();
*/
// step 5: receive reading from sensor
if (bytes_receive <= bytes_received) { // if two bytes were received
// Temperature
reading = Wire.read(); // receive high byte (overwrites previous )
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(-45+(127*(reading/((2^16)-1)))); // print the reading
// Swallow CRC
reading = Wire.read();
// Humidity
reading = Wire.read(); // receive high byte (overwrites previous )
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(100*(reading/((2^16)-1))); // print the reading
}
Serial.println("End Read");
delay(250); // wait a bit since people have to read the output :)
}