Overview
This program introduces how to measure temperature and humidity by connecting a humidity/temperature sensor (SHT31) to the GR-COTTON board.
![cotton-sp-hdc1000-serial](/sites/default/files/inline-images/cotton-sp-hdc1000-serial.png)
Preparation
You will need a GR-COTTON board, a USB cable (Micro B type), and an SHT31 humidity/temperature sensor.
You will need to install a pin socket on the GR-COTTON board and a pin header in the SHT31 sensor.
The SHT31 sensor and the pin socket can be purchased from the Akizuki Denshi website.
![cotton-sp-prepare-hdc1000](/sites/default/files/inline-images/cotton-sp-prepare-hdc1000.jpg)
Connect the SHT31 sensor module to GR-COTTON as shown in the photo to the right.
Make sure the white jumper on the reverse side of GR-COTTON is moved to the “3V3 USB” side. If it is set to the BATT side, remove and reinsert it into the USB side.
![cotton-sp-prepare-hdc10002](/sites/default/files/inline-images/cotton-sp-prepare-hdc10002.jpg)
![cotton-sp-prepare2](/sites/default/files/inline-images/cotton-sp-prepare2_8.jpg)
Displaying Temperature and Humidity on the Serial Monitor
The sample below programs the serial monitor to display temperature and humidity values read every second by the humidity/temperature sensor.
#include <arduino.h>
#include <Wire.h>
#define SHT31_ADDRESS 0x40
#define SHT31_RDY_PIN 2
#define SHT31_TEMPERATURE_POINTER 0x00
#define SHT31_HUMIDITY_POINTER 0x01
#define SHT31_CONFIGURATION_POINTER 0x02
#define SHT31_SERIAL_ID1_POINTER 0xfb
#define SHT31_SERIAL_ID2_POINTER 0xfc
#define SHT31_SERIAL_ID3_POINTER 0xfd
#define SHT31_MANUFACTURER_ID_POINTER 0xfe
#define SHT31_CONFIGURE_MSB 0x10 /* Get both temperature and humidity */
#define SHT31_CONFIGURE_LSB 0x00 /* 14 bit resolution */
void getTemperatureAndHumidity(float *temperature, float *humidity) {
unsigned int tData, hData;
Wire.beginTransmission(SHT31_ADDRESS);
Wire.write(SHT31_TEMPERATURE_POINTER);
Wire.endTransmission();
while (digitalRead(SHT31_RDY_PIN) == HIGH);
Wire.requestFrom(SHT31_ADDRESS, 4);
while (Wire.available() < 4);
tData = Wire.read() << 8;
tData |= Wire.read();
hData = Wire.read() << 8;
hData |= Wire.read();
*temperature = tData / 65536.0 * 165.0 - 40.0;
*humidity = hData / 65536.0 * 100.0;
}
//*********************************************************
void setup()
{
setPowerManagementMode(PM_STOP_MODE); // use stop delay
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
pinMode(SHT31_RDY_PIN, INPUT);
//Put the SHT31 IC into the correct operating mode
Wire.beginTransmission(SHT31_ADDRESS);
Wire.write(SHT31_CONFIGURATION_POINTER);
Wire.write(SHT31_CONFIGURE_MSB);
Wire.write(SHT31_CONFIGURE_LSB);
Wire.endTransmission();
}
//------------------------------------------------------
void loop()
{
float temperature, humidity;
getTemperatureAndHumidity(&temperature, &humidity);
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.print(" degree, Humidity = ");
Serial.print(humidity);
Serial.println("%");
Serial.flush();
delay(1000);
}
Temperature and Humidity Display for Picalico Reader
Each tap on the touch sensor yields a visible light transmission of temperature and humidity.
Picalico Reader is a smartphone app capable of reading these visible light signals.
#include <arduino.h>
#include <Wire.h>
#include <PicalicoClass.h>
Picalico pica(LOW); // set LED to active high
#define SHT31_ADDRESS 0x40
#define SHT31_RDY_PIN 2
#define SHT31_TEMPERATURE_POINTER 0x00
#define SHT31_HUMIDITY_POINTER 0x01
#define SHT31_CONFIGURATION_POINTER 0x02
#define SHT31_SERIAL_ID1_POINTER 0xfb
#define SHT31_SERIAL_ID2_POINTER 0xfc
#define SHT31_SERIAL_ID3_POINTER 0xfd
#define SHT31_MANUFACTURER_ID_POINTER 0xfe
#define SHT31_CONFIGURE_MSB 0x10 /* Get both temperature and humidity */
#define SHT31_CONFIGURE_LSB 0x00 /* 14 bit resolution */
void getTemperatureAndHumidity(float *temperature, float *humidity) {
unsigned int tData, hData;
Wire.beginTransmission(SHT31_ADDRESS);
Wire.write(SHT31_TEMPERATURE_POINTER);
Wire.endTransmission();
while (digitalRead(SHT31_RDY_PIN) == HIGH);
Wire.requestFrom(SHT31_ADDRESS, 4);
while (Wire.available() < 4);
tData = Wire.read() << 8;
tData |= Wire.read();
hData = Wire.read() << 8;
hData |= Wire.read();
*temperature = tData / 65536.0 * 165.0 - 40.0;
*humidity = hData / 65536.0 * 100.0;
}
//*********************************************************
void setup()
{
setPowerManagementMode(PM_SNOOZE_MODE, 0, 800); // use snooze
//Initialize Serial and I2C communications
Wire.begin();
//Put the SHT31 IC into the correct operating mode
Wire.beginTransmission(SHT31_ADDRESS);
Wire.write(SHT31_CONFIGURATION_POINTER);
Wire.write(SHT31_CONFIGURE_MSB);
Wire.write(SHT31_CONFIGURE_LSB);
Wire.endTransmission();
}
//------------------------------------------------------
void loop()
{
float temperature, humidity;
static bool g_mode = 0;
analogRead(A6); // into snooze mode, wait for touch.
pica.attach(22, 23, 24);
getTemperatureAndHumidity(&temperature, &humidity);
if(g_mode == 0){
pica.writeTemperature((int)temperature); // write temperature into picalico
} else {
pica.writePercent((int)humidity); // write temperature into picalico
}
delay(10000); // operate 10sec
g_mode = !g_mode;
pica.detach();
}
![cotton-sp-picalico](/sites/default/files/inline-images/cotton-sp-picalico.jpg)