-
Notifications
You must be signed in to change notification settings - Fork 27
/
sharp_a1du5p2cp006b_demo.ino
103 lines (82 loc) · 3.03 KB
/
sharp_a1du5p2cp006b_demo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/////////////////////////////////////////////////////////////////////////////
// Sharp A1DU5P2CP006B Humidity Sensor Module
//
// Board Connection:
// Arduino A1DU5P2CP006B
// 3.3V Pin 1 - VDD
// SCL Pin 2 - SCL Use 3.3V logic level converter
// SDA Pin 3 - SDA Use 3.3V logic level converter
// GND Pin 4 - GND
//
/////////////////////////////////////////////////////////////////////////////
#include <Wire.h>
// Sensor I2C slave address = 67 decimal.
#define SLAVE_ADDRESS 0x43
// Sensor I2C register addresses as defined in ENS210 datasheet.
#define REGISTER_ADDRESS_SYS_CTRL 0x10
#define REGISTER_ADDRESS_SENS_RUN 0x21
#define REGISTER_ADDRESS_SENS_START 0x22
#define REGISTER_ADDRESS_T_VAL 0x30
#define REGISTER_ADDRESS_H_VAL 0x33
/////////////////////////////////////////////////////////////////////////////
// Helper function to write a value to an 8-bit register.
void writeRegister(uint8_t regAddress, uint8_t value)
{
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(regAddress);
Wire.write(value);
Wire.endTransmission();
delay(200);
}
// Helper function to read a 3 byte measurement value from register.
uint32_t readMeasurement(uint8_t regAddress)
{
// Specify the register address to read from.
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(regAddress);
Wire.endTransmission();
// Request to read 3 bytes.
Wire.requestFrom(SLAVE_ADDRESS, 3);
uint8_t b0 = Wire.read();
uint8_t b1 = Wire.read();
uint8_t b2 = Wire.read();
uint32_t measurement = (uint32_t)b0 + (uint32_t)b1*256 + (uint32_t)b2*65536;
// Only take the last 16-bits.
measurement = measurement & 0xffff;
return measurement;
}
/////////////////////////////////////////////////////////////////////////////
// Arduino setup function.
void setup() {
// Start the hardware serial port for the serial monitor.
Serial.begin(9600);
Serial.println("");
Serial.println("A1DU5P2CP006B Demo");
Serial.println("==================");
// Start the Wire library.
Wire.begin();
// Reset the sensor and put it into standby mode.
writeRegister(REGISTER_ADDRESS_SYS_CTRL, 0x81);
// Set the run mode of the sensor to be Single Shot for both humidity and temperature.
writeRegister(REGISTER_ADDRESS_SENS_RUN, 0x00);
}
// Arduino main loop.
void loop() {
// Start a measurement for both humidity and temperature.
writeRegister(REGISTER_ADDRESS_SENS_START, 0x03);
// Read temperature value.
uint32_t rawTemperature = readMeasurement(REGISTER_ADDRESS_T_VAL);
Serial.print("Temperature = ");
float tempKelvin = rawTemperature/64.0;
float tempC = tempKelvin - 273.15;
Serial.print((tempC + 0.05), 1);
Serial.println(" degrees Celsius");
// Read relative humidity value.
uint32_t rawHumidity = readMeasurement(REGISTER_ADDRESS_H_VAL);
Serial.print("Humidity = ");
Serial.print((rawHumidity/512.0 + 0.05), 1);
Serial.println("%");
Serial.println("");
delay(500);
}
// END PROGRAM