In this example we show how to connect a Si7021 I2C Humidity and Temperature Sensor to a MSP-EXP432P401R LaunchPad, the example will use the Energia IDE.
I used a Si7021 module in this example, which you can see below
The Si7021 I2C Humidity and Temperature Sensor is a monolithic CMOS IC integrating humidity and temperature sensor elements, an analog-to-digital converter, signal processing, calibration data, and an I2C Interface. The patented use of industry-standard, low-K polymeric dielectrics for sensing humidity enables the construction of low-power, monolithic CMOS Sensor ICs with low drift and hysteresis, and excellent long term stability.
Datasheet – https://www.silabs.com/documents/public/data-sheets/Si7021-A20.pdf
Connection
Here is a picture of the launchpad so you can see what pins we are referring to below in the table
Module Connection | MSP432 Connection |
SDA | J1-10 SDA |
SCL | J1-9 SCL |
Gnd | J3-22 Gnd |
Vcc | J1-1 3.3v |
Code
[codesyntax lang=”cpp”]
#include <Wire.h> // SI7021 I2C address is 0x40(64) #define Addr 0x40 void setup() { // Initialise I2C communication as MASTER Wire.begin(); // Initialise serial communication, set baud rate = 9600 Serial.begin(9600); // Start I2C transmission Wire.beginTransmission(Addr); // Stop I2C transmission Wire.endTransmission(); delay(300); } void loop() { unsigned int data[2]; // Start I2C transmission Wire.beginTransmission(Addr); // Send humidity measurement command, NO HOLD MASTER Wire.write(0xF5); // Stop I2C transmission Wire.endTransmission(); delay(500); // Request 2 bytes of data Wire.requestFrom(Addr, 2); // Read 2 bytes of data // humidity msb, humidity lsb if(Wire.available() == 2) { data[0] = Wire.read(); data[1] = Wire.read(); } // Convert the data float humidity = ((data[0] * 256.0) + data[1]); humidity = ((125 * humidity) / 65536.0) - 6; // Start I2C transmission Wire.beginTransmission(Addr); // Send temperature measurement command, NO HOLD MASTER Wire.write(0xF3); // Stop I2C transmission Wire.endTransmission(); delay(500); // Request 2 bytes of data Wire.requestFrom(Addr, 2); // Read 2 bytes of data // temp msb, temp lsb if(Wire.available() == 2) { data[0] = Wire.read(); data[1] = Wire.read(); } // Convert the data float temp = ((data[0] * 256.0) + data[1]); float cTemp = ((175.72 * temp) / 65536.0) - 46.85; float fTemp = cTemp * 1.8 + 32; // Output data to serial monitor Serial.print("Relative humidity : "); Serial.print(humidity); Serial.println(" % RH"); Serial.print("Temperature in Celsius : "); Serial.print(cTemp); Serial.println(" C"); Serial.print("Temperature in Fahrenheit : "); Serial.print(fTemp); Serial.println(" F"); delay(500); }
[/codesyntax]
Output
Open the serial monitor and you should see something like this
Temperature in Celsius : 27.67 C
Temperature in Fahrenheit : 81.80 F
Relative humidity : 31.35 % RH
Temperature in Celsius : 29.15 C
Temperature in Fahrenheit : 84.47 F
Relative humidity : 32.09 % RH
Temperature in Celsius : 31.21 C
Temperature in Fahrenheit : 88.17 F
Relative humidity : 32.86 % RH
Temperature in Celsius : 32.22 C
Temperature in Fahrenheit : 89.99 F
Relative humidity : 33.73 % RH
Temperature in Celsius : 32.66 C
Temperature in Fahrenheit : 90.78 F
Relative humidity : 34.14 % RH
Links
Industrial High Precision Si7021 Humidity Sensor with I2C Interface for Arduino