|
| 1 | +/* |
| 2 | + Take humidity and temperature readings with the SHTC3 using I2C |
| 3 | + By: Owen Lyke |
| 4 | + SparkFun Electronics |
| 5 | + Date: August 24 2018 |
| 6 | + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). |
| 7 | + Example1_BasicReadings |
| 8 | + To connect the sensor to an Arduino: |
| 9 | + This library supports the sensor using the I2C protocol |
| 10 | + On Qwiic enabled boards simply connnect the sensor with a Qwiic cable and it is set to go |
| 11 | + On non-qwiic boards you will need to connect 4 wires between the sensor and the host board |
| 12 | + (Arduino pin) = (Display pin) |
| 13 | + SCL = SCL on display carrier |
| 14 | + SDA = SDA |
| 15 | + GND = GND |
| 16 | + 3.3V = 3.3V |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "SparkFun_SHTC3.h" // Click here to get the library: http://librarymanager/All#SparkFun_SHTC3 |
| 20 | + |
| 21 | +#define SERIAL_PORT Serial // #define-ing the SERIAL_PORT allows you to easily change your target port - for example if you are using a SAMD21 board you can change "Serial" to "SerialUSB" |
| 22 | + |
| 23 | +SHTC3 mySHTC3; // Declare an instance of the SHTC3 class |
| 24 | + |
| 25 | +void setup() { |
| 26 | + SERIAL_PORT.begin(115200); // Begin Serial |
| 27 | + while(SERIAL_PORT == false){}; // Wait for the serial connection to start up |
| 28 | + SERIAL_PORT.println("SHTC3 Example 1 - Basic Readings"); // Title |
| 29 | + SERIAL_PORT.println(); |
| 30 | + |
| 31 | + SERIAL_PORT.print("Beginning sensor. Result = "); // Most SHTC3 functions return a variable of the type "SHTC3_Status_TypeDef" to indicate the status of their execution |
| 32 | + errorDecoder(mySHTC3.begin()); // To start the sensor you must call "begin()", the defualt settings use Wire (default Arduino I2C port) at 400 kHz clock speed |
| 33 | + SERIAL_PORT.println(); |
| 34 | + |
| 35 | + if(mySHTC3.passIDcrc) // Whenever data is received the associated checksum is calculated and verified so you can be sure the data is true |
| 36 | + { // The checksum pass indicators are: passIDcrc, passRHcrc, and passTcrc for the ID, RH, and T readings respectively |
| 37 | + SERIAL_PORT.print("ID Passed Checksum. "); |
| 38 | + SERIAL_PORT.print("Device ID: 0b"); |
| 39 | + SERIAL_PORT.print(mySHTC3.ID, BIN); // The 16-bit device ID can be accessed as a member variable of the object |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + SERIAL_PORT.print("ID Checksum Failed. "); |
| 44 | + } |
| 45 | + SERIAL_PORT.println("\n\n"); |
| 46 | + SERIAL_PORT.println("Waiting for 5 seconds so you can read this info ^^^"); |
| 47 | + |
| 48 | + delay(5000); // Give time to read the welcome message and device ID. |
| 49 | +} |
| 50 | + |
| 51 | +void loop() { |
| 52 | + SHTC3_Status_TypeDef result = mySHTC3.update(); // Call "update()" to command a measurement, wait for measurement to complete, and update the RH and T members of the object |
| 53 | + printInfo(); // This function is used to print a nice little line of info to the serial port |
| 54 | + delay(190); // Delay for the data rate you want - note that measurements take ~10 ms so the fastest data rate is 100 Hz (when no delay is used) |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +/////////////////////// |
| 60 | +// Utility Functions // |
| 61 | +/////////////////////// |
| 62 | +void printInfo() |
| 63 | +{ |
| 64 | + if(mySHTC3.lastStatus == SHTC3_Status_Nominal) // You can also assess the status of the last command by checking the ".lastStatus" member of the object |
| 65 | + { |
| 66 | + SERIAL_PORT.print("RH = "); |
| 67 | + SERIAL_PORT.print(mySHTC3.toPercent()); // "toPercent" returns the percent humidity as a floating point number |
| 68 | + SERIAL_PORT.print("% (checksum: "); |
| 69 | + if(mySHTC3.passRHcrc) // Like "passIDcrc" this is true when the RH value is valid from the sensor (but not necessarily up-to-date in terms of time) |
| 70 | + { |
| 71 | + SERIAL_PORT.print("pass"); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + SERIAL_PORT.print("fail"); |
| 76 | + } |
| 77 | + SERIAL_PORT.print("), T = "); |
| 78 | + SERIAL_PORT.print(mySHTC3.toDegF()); // "toDegF" and "toDegC" return the temperature as a flaoting point number in deg F and deg C respectively |
| 79 | + SERIAL_PORT.print(" deg F (checksum: "); |
| 80 | + if(mySHTC3.passTcrc) // Like "passIDcrc" this is true when the T value is valid from the sensor (but not necessarily up-to-date in terms of time) |
| 81 | + { |
| 82 | + SERIAL_PORT.print("pass"); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + SERIAL_PORT.print("fail"); |
| 87 | + } |
| 88 | + SERIAL_PORT.println(")"); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + SERIAL_PORT.print("Update failed, error: "); |
| 93 | + errorDecoder(mySHTC3.lastStatus); |
| 94 | + SERIAL_PORT.println(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void errorDecoder(SHTC3_Status_TypeDef message) // The errorDecoder function prints "SHTC3_Status_TypeDef" resultsin a human-friendly way |
| 99 | +{ |
| 100 | + switch(message) |
| 101 | + { |
| 102 | + case SHTC3_Status_Nominal : SERIAL_PORT.print("Nominal"); break; |
| 103 | + case SHTC3_Status_Error : SERIAL_PORT.print("Error"); break; |
| 104 | + case SHTC3_Status_CRC_Fail : SERIAL_PORT.print("CRC Fail"); break; |
| 105 | + default : SERIAL_PORT.print("Unknown return code"); break; |
| 106 | + } |
| 107 | +} |
0 commit comments