I2C Wiring
Use this wiring if you want to connect via I2C interface
The HTS221's I2C address is 0x5F.
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
The final results should resemble the illustration above, showing an Adafruit Metro development board.
- Connect Vin to the power supply, 3V or 5V is fine. Use the same voltage that the microcontroller logic is based off of
- Connect GND to common power/data ground
- Connect the SCL pin to Digital #13 but any pin can be used later
- Connect the SDA pin to Digital #12 but any pin can be used later
- Make a second connection to the SDA pin, through a 1K resistor from Digital #11. Any pin can be used later but it must be connected to SDA through a 1K resistor
- Connect the CS pin Digital #10 but any pin can be used later
Later on, once we get it working, we can adjust the library to use hardware SPI if you desire, or change the pins to others.
Library Installation
You can install the Adafruit HTS221 Library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit HTS221 and select the Adafruit HTS221 library:
Then follow the same process for the Adafruit BusIO library.
Finally follow the same process for the Adafruit Unified Sensor library:
Load Example
Open up File -> Examples -> Adafruit HTS221 -> adafruit_hts221_test and upload to your Arduino wired up to the sensor.
Depending on whether you are using I2C or SPI, change the pin names and comment or uncomment the following lines.
if (!hts.begin_I2C()) { // if (!hts.begin_SPI(HTS_CS)) { // if (!hts.begin_SPI(HTS_CS, HTS_SCK, HTS_MISO, HTS_MOSI)) {
Once you upload the code and open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, you will see the current configuration printed, followed by the humidity and temperature measurements. You should see something similar to this:
The moisture from your breath should make the readings change, so give it a try by breathing on the sensor (the tiny black square in the center of the breakout) and seeing what happens!
// Basic demo for reading Humidity and Temperature #include <Wire.h> #include <Adafruit_HTS221.h> #include <Adafruit_Sensor.h> // For SPI mode, we need a CS pin #define HTS_CS 10 // For software-SPI mode we need SCK/MOSI/MISO pins #define HTS_SCK 13 #define HTS_MISO 12 #define HTS_MOSI 11 Adafruit_HTS221 hts; void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("Adafruit HTS221 test!"); // Try to initialize! if (!hts.begin_I2C()) { // if (!hts.begin_SPI(HTS_CS)) { // if (!hts.begin_SPI(HTS_CS, HTS_SCK, HTS_MISO, HTS_MOSI)) { Serial.println("Failed to find HTS221 chip"); while (1) { delay(10); } } Serial.println("HTS221 Found!"); // hts.setDataRate(HTS221_RATE_1_HZ); Serial.print("Data rate set to: "); switch (hts.getDataRate()) { case HTS221_RATE_ONE_SHOT: Serial.println("One Shot"); break; case HTS221_RATE_1_HZ: Serial.println("1 Hz"); break; case HTS221_RATE_7_HZ: Serial.println("7 Hz"); break; case HTS221_RATE_12_5_HZ: Serial.println("12.5 Hz"); break; } } void loop() { sensors_event_t temp; sensors_event_t humidity; hts.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C"); Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH"); delay(500); }
Text editor powered by tinymce.