Testing SHT75 humidity sensor on STM F303K8 board.
Fork of Nucleo-F303K8-SSD1306_OLED by
Sample code to test SHT75 humidity sensor using STM F303K8 board. Uses a 3.3V from board to power sensor. 10K resistor Pull-up on data. Must not be on same pins as I2C. Uses D0 for Clk and D1 for Data.
I had to modify sample code supplied by https://developer.mbed.org/users/nimbusgb/code/SHT75/ because the sensor failed to read without the softReset() and readStatus() at beginning of measurement loop. I think this is caused by the 72Mhtz speed of the F303K8 but did not attempt to fully diagnose.
The readStatus() method from library seems to malfunction and always return a -1 which never changes even when sensor is unplugged.
See https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity_Sensors/Sensirion_Humidity_Sensors_SHT7x_Datasheet_V5.pdf section 2.1 for wiring.
main.cpp@5:4b66dfbc52a5, 2016-07-27 (annotated)
- Committer:
- joeata2wh
- Date:
- Wed Jul 27 19:54:51 2016 +0000
- Revision:
- 5:4b66dfbc52a5
- Parent:
- 4:2c46c3bc8032
updated comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 5:4b66dfbc52a5 | 1 | /* Test ability to read SHT75 sensor from F303K8 board. |
joeata2wh | 5:4b66dfbc52a5 | 2 | |
joeata2wh | 4:2c46c3bc8032 | 3 | Note: Can not be on same pins as I2C due to different pull up |
joeata2wh | 4:2c46c3bc8032 | 4 | requirements. |
joeata2wh | 2:dc3e84d595c3 | 5 | |
joeata2wh | 3:47148198f5f2 | 6 | By Joseph Ellsworth CTO of A2WH |
joeata2wh | 3:47148198f5f2 | 7 | Take a look at A2WH.com Producing Water from Air using Solar Energy |
joeata2wh | 4:2c46c3bc8032 | 8 | March-2016 License: https://developer.mbed.org/handbook/MIT-Licence |
joeata2wh | 3:47148198f5f2 | 9 | Please contact us http://a2wh.com for help with custom design projects. |
joeata2wh | 5:4b66dfbc52a5 | 10 | |
joeata2wh | 5:4b66dfbc52a5 | 11 | Sample code to test SHT75 humidity sensor using STM F303K8 board. |
joeata2wh | 5:4b66dfbc52a5 | 12 | Uses a 3.3V from board to power sensor. 10K resistor Pull-up on data. |
joeata2wh | 5:4b66dfbc52a5 | 13 | Must not be on same pins as I2C. Uses D0 for Clk and D1 for Data. |
joeata2wh | 3:47148198f5f2 | 14 | |
joeata2wh | 5:4b66dfbc52a5 | 15 | I had to modify sample code supplied by https://developer.mbed.org/users/nimbusgb/code/SHT75/ |
joeata2wh | 5:4b66dfbc52a5 | 16 | because the sensor failed to read without the softReset() and readStatus() at beginning |
joeata2wh | 5:4b66dfbc52a5 | 17 | of measurement loop. I think this is caused by the 72Mhtz speed of the F303K8 but did |
joeata2wh | 5:4b66dfbc52a5 | 18 | not attempt to fully diagnose. |
joeata2wh | 5:4b66dfbc52a5 | 19 | |
joeata2wh | 5:4b66dfbc52a5 | 20 | The readStatus() method from library seems to malfunction and always return a -1 |
joeata2wh | 5:4b66dfbc52a5 | 21 | which never changes even when sensor is unplugged. |
joeata2wh | 5:4b66dfbc52a5 | 22 | |
joeata2wh | 3:47148198f5f2 | 23 | |
joeata2wh | 1:90d213185462 | 24 | */ |
joeata2wh | 0:fa185766e039 | 25 | |
joeata2wh | 0:fa185766e039 | 26 | #include "mbed.h" |
joeata2wh | 4:2c46c3bc8032 | 27 | #include <stdint.h> |
joeata2wh | 2:dc3e84d595c3 | 28 | |
joeata2wh | 4:2c46c3bc8032 | 29 | #define sht75_clk D0 |
joeata2wh | 4:2c46c3bc8032 | 30 | #define sht75_data D1 |
joeata2wh | 4:2c46c3bc8032 | 31 | #include "sht7X.h" |
joeata2wh | 4:2c46c3bc8032 | 32 | SHT75 sht(sht75_clk, sht75_data); |
joeata2wh | 4:2c46c3bc8032 | 33 | |
joeata2wh | 2:dc3e84d595c3 | 34 | |
joeata2wh | 2:dc3e84d595c3 | 35 | // Host PC Communication channels |
joeata2wh | 2:dc3e84d595c3 | 36 | Serial pc(USBTX, USBRX); // tx, rx |
joeata2wh | 4:2c46c3bc8032 | 37 | char buff[100]; |
joeata2wh | 2:dc3e84d595c3 | 38 | |
joeata2wh | 0:fa185766e039 | 39 | DigitalOut myled(LED1); |
joeata2wh | 2:dc3e84d595c3 | 40 | |
joeata2wh | 2:dc3e84d595c3 | 41 | int main() |
joeata2wh | 2:dc3e84d595c3 | 42 | { |
joeata2wh | 4:2c46c3bc8032 | 43 | pc.baud(38400); |
joeata2wh | 4:2c46c3bc8032 | 44 | wait(1); |
joeata2wh | 0:fa185766e039 | 45 | while(1) { |
joeata2wh | 2:dc3e84d595c3 | 46 | myled = !myled; |
joeata2wh | 4:2c46c3bc8032 | 47 | float temperature; // temperature -40 to 120 deg C |
joeata2wh | 4:2c46c3bc8032 | 48 | float humidity; // relative humidity 1% to 100% |
joeata2wh | 4:2c46c3bc8032 | 49 | float humi_f,rh_lin,rh_true; // working registers for Illustration purposes |
joeata2wh | 4:2c46c3bc8032 | 50 | int t; // temporary store for the temp ticks |
joeata2wh | 4:2c46c3bc8032 | 51 | int h; |
joeata2wh | 4:2c46c3bc8032 | 52 | sht.softReset(); |
joeata2wh | 4:2c46c3bc8032 | 53 | int stat = sht.readStatus(); |
joeata2wh | 4:2c46c3bc8032 | 54 | wait(3.0); |
joeata2wh | 4:2c46c3bc8032 | 55 | sht.readTempTicks(&t); |
joeata2wh | 4:2c46c3bc8032 | 56 | temperature = ((float)(t) * 0.01) - 39.61; |
joeata2wh | 3:47148198f5f2 | 57 | |
joeata2wh | 4:2c46c3bc8032 | 58 | sht.readHumidityTicks(&h); |
joeata2wh | 4:2c46c3bc8032 | 59 | humi_f = (float)(h); |
joeata2wh | 4:2c46c3bc8032 | 60 | rh_lin = C3 * humi_f * humi_f + C2 * humi_f + C1; |
joeata2wh | 4:2c46c3bc8032 | 61 | rh_true=(((temperature/100)-25)*(T1+T2*humi_f)+rh_lin); |
joeata2wh | 4:2c46c3bc8032 | 62 | if(rh_true>100)rh_true=100; //cut if the value is outside |
joeata2wh | 4:2c46c3bc8032 | 63 | if(rh_true<1)rh_true=1; //the physical possible range |
joeata2wh | 4:2c46c3bc8032 | 64 | humidity = rh_true; |
joeata2wh | 4:2c46c3bc8032 | 65 | pc.printf("stat=%d Temp: %2.2f RH %2.2f\n\r",stat, temperature, humidity); |
joeata2wh | 4:2c46c3bc8032 | 66 | |
joeata2wh | 2:dc3e84d595c3 | 67 | wait(3.0); |
joeata2wh | 0:fa185766e039 | 68 | } |
joeata2wh | 4:2c46c3bc8032 | 69 | } |