Skip to content

Commit cbe88c1

Browse files
committed
Add humidity to BME280 sensor
1 parent 906d43d commit cbe88c1

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

components/src/environment/bme280/bme280.adb

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,34 @@ package body BME280 is
128128

129129
Value.T3 := Unsigned_16 (Data (16#8C#)) +
130130
Shift_Left (Unsigned_16 (Data (16#8D#)), 8);
131+
132+
Value.H1 := Unsigned_8 (Data (16#A1#));
133+
end;
134+
135+
declare
136+
use type HAL.UInt8;
137+
Data : HAL.UInt8_Array (16#E1# .. 16#E7#);
138+
begin
139+
Read (Data, Success);
140+
141+
if not Success then
142+
return;
143+
end if;
144+
145+
Value.H2 := Unsigned_16 (Data (16#E1#)) +
146+
Shift_Left (Unsigned_16 (Data (16#E2#)), 8);
147+
148+
Value.H3 := Unsigned_8 (Data (16#E3#));
149+
150+
Value.H4 := Shift_Left (Unsigned_16 (Data (16#E4#)), 4) +
151+
Unsigned_16 (Data (16#E5#) and 16#0F#);
152+
153+
Value.H5 := Shift_Right (Unsigned_16 (Data (16#E5#)), 4) +
154+
Shift_Left (Unsigned_16 (Data (16#E6#)), 4);
155+
156+
Value.H6 := Unsigned_8 (Data (16#E7#));
131157
end;
158+
132159
end Read_Calibration;
133160

134161
----------------------
@@ -196,6 +223,46 @@ package body BME280 is
196223

197224
end Generic_Sensor;
198225

226+
--------------
227+
-- Humidity --
228+
--------------
229+
230+
function Humidity
231+
(Value : Measurement;
232+
Temperature : Deci_Celsius;
233+
Calibration : Calibration_Constants) return Relative_Humidity
234+
is
235+
Var_1 : constant Integer :=
236+
Integer (Temperature / Deci_Celsius'Small) - 76800;
237+
Var_2 : Integer := Integer (Value.Raw_Hum) * 16384;
238+
Var_3 : Integer := Integer (Calibration.H4) * 1048576;
239+
Var_4 : Integer := Integer (Calibration.H5) * Var_1;
240+
Var_5 : Integer := (Var_2 - Var_3 - Var_4 + 16384) / 32768;
241+
242+
begin
243+
-- var2 = (var1 * ((int32_t)calib_data->dig_h6)) / 1024;
244+
Var_2 := Var_1 * Integer (Calibration.H6) / 1024;
245+
-- var3 = (var1 * ((int32_t)calib_data->dig_h3)) / 2048;
246+
Var_3 := Var_1 * Integer (Calibration.H3) / 2048;
247+
-- var4 = ((var2 * (var3 + (int32_t)32768)) / 1024) + (int32_t)2097152;
248+
Var_4 := Var_2 * (Var_3 + 32768) / 1024 + 2097152;
249+
-- var2 = ((var4 * ((int32_t)calib_data->dig_h2)) + 8192) / 16384;
250+
Var_2 := (Var_4 * Integer (Calibration.H2) + 8192) / 16384;
251+
-- var3 = var5 * var2;
252+
Var_3 := Var_5 * Var_2;
253+
-- var4 = ((var3 / 32768) * (var3 / 32768)) / 128;
254+
Var_4 := (Var_3 / 32768)**2 / 128;
255+
-- var5 = var3 - ((var4 * ((int32_t)calib_data->dig_h1)) / 16);
256+
Var_5 := Var_3 - Var_4 * Integer (Calibration.H1) / 16;
257+
Var_5 := Integer'Max (0, Integer'Min (100 * 2 ** 10, Var_5 / 4096));
258+
259+
return Relative_Humidity'Small * Var_5;
260+
end Humidity;
261+
262+
-----------------
263+
-- Temperature --
264+
-----------------
265+
199266
function Temperature
200267
(Value : Measurement;
201268
Calibration : Calibration_Constants) return Deci_Celsius
@@ -207,7 +274,7 @@ package body BME280 is
207274
(Diff * Integer (Calibration.T2)) / 2 ** 11;
208275

209276
Val_2 : constant Integer :=
210-
(Diff / 2) ** 2 / 2 ** 11 * Integer (Calibration.T3) / 2 ** 14;
277+
(Diff / 2) ** 2 / 2 ** 12 * Integer (Calibration.T3) / 2 ** 14;
211278

212279
begin
213280
return Deci_Celsius'Small * (Val_1 + Val_2);

components/src/environment/bme280/bme280.ads

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ package BME280 is
4040
T1 : Interfaces.Unsigned_16;
4141
T2 : Interfaces.Unsigned_16;
4242
T3 : Interfaces.Unsigned_16;
43+
H1 : Interfaces.Unsigned_8;
44+
H2 : Interfaces.Unsigned_16;
45+
H3 : Interfaces.Unsigned_8;
46+
H4 : Interfaces.Unsigned_16 range 0 .. 4095;
47+
H5 : Interfaces.Unsigned_16 range 0 .. 4095;
48+
H6 : Interfaces.Unsigned_8;
4349
end record;
4450

4551
type Measurement is private;
@@ -51,6 +57,15 @@ package BME280 is
5157
(Value : Measurement;
5258
Calibration : Calibration_Constants) return Deci_Celsius;
5359

60+
Humidity_Small : constant := 1.0 / 2 ** 10;
61+
62+
type Relative_Humidity is delta Humidity_Small range 0.0 .. 100.0;
63+
64+
function Humidity
65+
(Value : Measurement;
66+
Temperature : Deci_Celsius;
67+
Calibration : Calibration_Constants) return Relative_Humidity;
68+
5469
type Oversampling_Kind is (Skip, X1, X2, X4, X8, X16);
5570
type IRR_Filter_Kind is (Off, X1, X2, X4, X8, X16);
5671
type Sensor_Mode is (Sleep, Forced, Normal);

examples/stm32_f4ve/bme280/main.adb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ procedure Main is
7070
Calib : BME280.Calibration_Constants;
7171
Measurement : BME280.Measurement;
7272
Temp : BME280.Deci_Celsius;
73+
Humi : BME280.Relative_Humidity;
7374

7475
begin
7576
STM32.Board.Initialize_LEDs;
@@ -91,7 +92,8 @@ begin
9192

9293
if Ok then
9394
Temp := BME280.Temperature (Measurement, Calib);
94-
Put_Line (Temp'Image);
95+
Humi := BME280.Humidity (Measurement, Temp, Calib);
96+
Put_Line (Temp'Image & Humi'Image);
9597
end if;
9698

9799
Next := Next + Ada.Real_Time.Milliseconds (500);

0 commit comments

Comments
 (0)