Thursday, February 4, 2016

I2C, Energia, Stellaris Launchpad and HT21D

A friend asked me to help him connect the HT21D temperature and humidity sensor to a TI board.
He's using the TM4C123 board while I'm using the older LM4F Launchpad.

The first thing to know is that neither of the boards have pullups adequate for I2C. You need to add a 4.7k resistor from SDA to VCC and another one from SCK to VCC. I've tested it with 1k resistors and it works with those as well.

The second thing is that the Stellaris boards have several I2C modules, so you need to select one. Even worse is the fact that the Energia library by default chooses the Boosterpack:


if(i2cModule == NOT_ACTIVE) {
  i2cModule = BOOST_PACK_WIRE;
}

So you first need to go into this folder "\hardware\lm4f\libraries\Adafruit_HTU21DF_Library-master\" and edit two files as explained below.

Adafruit_HTU21DF.cpp
From:
boolean Adafruit_HTU21DF::begin(void) {
  Wire.begin();

  reset();
To:
boolean Adafruit_HTU21DF::begin(int moduleNo) {
  Wire.setModule(moduleNo);

  reset();

Adafruit_HTU21DF.h
From:
  boolean begin(void);
To:
  boolean begin(int moduleNo);

In the example file
From:
void setup() {
  Serial.begin(9600);
  Serial.println("HTU21D-F test");
  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}
To:
void setup() {
  Serial.begin(9600);
  Serial.println("HTU21D-F test");
  if (!htu.begin(0)) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}
Instead of begin(0) you can choose whichever I2C module you'd like, the LM4F has 4 of them.
The Wire::begin() function does not need to be called since the setModule() function will call it. But if you want to use the device as a slave, you need to call begin(slaveAddress).


No comments:

Post a Comment