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.
Revision 4:2c46c3bc8032, committed 2016-07-27
- Comitter:
- joeata2wh
- Date:
- Wed Jul 27 19:42:41 2016 +0000
- Parent:
- 3:47148198f5f2
- Child:
- 5:4b66dfbc52a5
- Commit message:
- working version
Changed in this revision
--- a/Adafruit_GFX.lib Wed Mar 30 14:49:21 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/nkhorman/code/Adafruit_GFX/#7fb1d4d3525d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SHT75.lib Wed Jul 27 19:42:41 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/nimbusgb/code/SHT75/#db6b417dfa74
--- a/SSD1308_128x64_I2C.lib Wed Mar 30 14:49:21 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/wim/code/SSD1308_128x64_I2C/#df92b0c0cb92
--- a/main.cpp Wed Mar 30 14:49:21 2016 +0000 +++ b/main.cpp Wed Jul 27 19:42:41 2016 +0000 @@ -1,153 +1,57 @@ -/* Example of Reading all the ADC pins PIN and display ot OLED display using - Nucleo_F303K8. Displays voltage read from first 2 ADC lines on OLED display +/* Test ability to read SHT75 sensor + Note: Can not be on same pins as I2C due to different pull up + requirements. By Joseph Ellsworth CTO of A2WH Take a look at A2WH.com Producing Water from Air using Solar Energy - March-2016 License: https://developer.mbed.org/handbook/MIT-Licence + March-2016 License: https://developer.mbed.org/handbook/MIT-Licence Please contact us http://a2wh.com for help with custom design projects. + See Also: https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity_Sensors/Sensirion_Humidity_Sensors_SHT7x_Datasheet_V5.pdf - Used to Drive: - ebay part http://www.ebay.com/itm/152000005331?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT - 0-96-I2C-IIC-SPI-Serial-White-OLED-LCD-LED-Display-Module-128X64 - The Datasheet. https://www.adafruit.com/datasheets/SSD1306.pdf - - Unfortunately this part never turns on using the SSD1308 library. - but did find that the https://developer.mbed.org/users/nkhorman/code/Adafruit_GFX/ - library works. Unfortunately the Adafruit library doesn't include the scroll functionality. - - Using my I2C address scanner I found that it responds on Added 120 (x78) ,121 (x79) - and when the part was unsoldered nothing responded on 120 or 121. According to - to the page #19 (8.1.5) of the data sheet the I2C address should be 011110 - which seems to map correctly to dec=120 hex=79 - */ #include "mbed.h" -//#include "SSD1308.h" -#include "Adafruit_SSD1306.h" - -//#include "mbed_logo.h" +#include <stdint.h> -//Pin Defines for I2C Bus -#define D_SDA PB_7 // specific for Nucleo-F303K8 -#define D_SCL PB_6 // specific for Nucleo-F303K8 -I2C i2c(D_SDA, D_SCL); +#define sht75_clk D0 +#define sht75_data D1 +#include "sht7X.h" +SHT75 sht(sht75_clk, sht75_data); + // Host PC Communication channels Serial pc(USBTX, USBRX); // tx, rx - -AnalogIn pa0(PA_0); -AnalogIn pa1(PA_1); -PwmOut fan1(D11); -//PwmOut fan2(D12); -//PwmOut fan3(PA_2); -DigitalOut fan2x(D12); +char buff[100]; DigitalOut myled(LED1); -const float voltMeterARef = 3.3; - -// an I2C sub-class that provides a constructed default -class I2CPreInit : public I2C -{ -public: - I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) { - frequency(400000); - start(); - }; -}; - - -I2CPreInit gI2C(D_SDA,D_SCL); -Adafruit_SSD1306_I2c gOled2(gI2C,PB_5); - - -float readPrint(AnalogIn ain, char *label) -{ - float tval = ain.read(); - float volts = tval * voltMeterARef; - float perc = tval * 100.0f; - unsigned short tvalu16 = ain.read_u16 (); - pc.printf("adc %s R=%3.3f V=%3.3f U16=%u\r\n",label, tval, volts, tvalu16); - gOled2.printf("%s=%3.3fV\r\n",label,volts); - gOled2.display(); - return tval; -} - int main() { - pc.baud(9600); - // Display with the Adafruit Library - gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height()); - wait(5); - gOled2.clearDisplay(); + pc.baud(38400); + wait(1); while(1) { myled = !myled; - gOled2.clearDisplay(); - gOled2.setTextCursor(1,1); - readPrint(pa0, "PA_0"); - wait(0.1); - gOled2.setTextCursor(1,10); - readPrint(pa1, "PA_1"); - - float pwmpw = 0.3; //0.0005; //0.09; // 0.1001; - float pwmpp = 3.0; //0.00001; //0.0001; // 0.01001; - //fan2.period_ms(); - //fan2.pulsewidth_ms(200); - - //float pwmpw = 0.1; //0.0005; //0.09; // 0.1001; - //float pwmpp = 1.0; //0.00001; //0.0001; // 0.01001; - //fan2.pulsewidth(0.1); - //fan2.period(1.0 / 10.0); - //fan2.pulsewidth(2.0); - //fan2.period(20); - float pwmduty = 0.1f; - float unit_time = 10.0; - float total_period = 0.1; //0.7; //3.5; //0.8; - float num_cycle = (unit_time / total_period) + 1; + float temperature; // temperature -40 to 120 deg C + float humidity; // relative humidity 1% to 100% + float humi_f,rh_lin,rh_true; // working registers for Illustration purposes + int t; // temporary store for the temp ticks + int h; + sht.softReset(); + int stat = sht.readStatus(); + wait(3.0); + sht.readTempTicks(&t); + temperature = ((float)(t) * 0.01) - 39.61; - while (pwmduty <= 1.1f) { - float on_time = total_period * pwmduty; - float off_time = total_period - on_time; - //gOled2.clearDisplay(); - gOled2.setTextCursor(1,20); - wait(0.1); - gOled2.printf("pwm=%3.3f\r\n", pwmduty); - wait(0.1); - gOled2.display(); - wait(0.1); - gOled2.display(); - wait(0.1); - pc.printf("pwm=%3.3f\r\n", pwmduty); - //wait(3.0); - fan2x = 1; - wait(0.15); - for (int x=0; x<= num_cycle; x++) { - - fan2x = 1; - wait(on_time); - fan2x = 0; - wait(off_time); - // accelerate to get the motor turning - // then slow down to the current speed; - //fan1 = 0.0; - //fan2 = 0.0; - //fan3 = 0.0; - //wait(0.02); - //fan1 = pwmduty; - //fan2 = pwmduty; - //fan3 = pwmduty; - //fan2 = pwmduty; - //fan2.period(pwmduty/100); - //fan2.period(pwmduty); - - } - pwmduty+= 0.1f; - } - //fan1 = 0.0; - //fan2 = 0.0; - //fan3 = 0.0; + sht.readHumidityTicks(&h); + humi_f = (float)(h); + rh_lin = C3 * humi_f * humi_f + C2 * humi_f + C1; + rh_true=(((temperature/100)-25)*(T1+T2*humi_f)+rh_lin); + if(rh_true>100)rh_true=100; //cut if the value is outside + if(rh_true<1)rh_true=1; //the physical possible range + humidity = rh_true; + pc.printf("stat=%d Temp: %2.2f RH %2.2f\n\r",stat, temperature, humidity); + wait(3.0); } -} +} \ No newline at end of file