Wednesday 7 November 2012

Using a Teensy 3.0 with Adafruit bits

I2C update: Apparently there are issues with the internal pull-up resistors and the Teensy 3.0 requires external resistors connecting both the SDA and SCL lines to logic high. I added 2K pull-ups and things went from flakey to functional.


I got a Teensy 3.0 because it seemed like a good idea, especially with the high resolution ADC. Besides, it's teensy, and that's cool, always cool!

Fired it up with the iMac, downloaded the beta 6 Arduino IDE, and things ran right away.

Attached the 2x16 LCD shield on I2C and the example ran.

Attached the MCP4725 DAC breakout on I2C and the example wouldn't compile because TWBR is not defined. It seems to have something to do with the I2C clock speed. It was only mentioned because the code wanted to change it, so I commented out the change lines in the library cpp and the triangle wave code ran fine.


void Adafruit_MCP4725::setVoltage( uint16_t output, bool writeEEPROM )
{
  //uint8_t twbrback = TWBR;
  //TWBR = 12; // 400 khz
  Wire.beginTransmission(_i2caddr);
  if (writeEEPROM)
  {
    Wire.write(MCP4726_CMD_WRITEDACEEPROM);
  }
  else
  {
    Wire.write(MCP4726_CMD_WRITEDAC);
  }
  Wire.write(output / 16);                   // Upper data bits          (D11.D10.D9.D8.D7.D6.D5.D4)
  Wire.write(output % 16) << 4;              // Lower data bits          (D3.D2.D1.D0.x.x.x.x)
  Wire.endTransmission();
  //TWBR = twbrback;
}

Attached the MPL115A2 Pressure and Temperature breakout on I2C and everything compiles fine, but the answers are way wrong, and constant. I'm not sure why, but the temperature return value is 1023, all bits set, suggesting some sort of handshake problem. The same result shows up on the Uno if you disconnect the breakout board.

It looks like there are different libraries in the internal parts of the Teensy version of the ide, so maybe the variations in the wire.h library are to blame. The adafruit code has tests on the value of ARDUINO >= 100 to determine which wire.xxx calls to use and it may be using the wrong ones. Figuring what's right would require a look at the internal wire library built into the package. (Right click view package contents) I think I'll wait for the beta software to advance a little farther.

Tuesday 6 November 2012

Pressure and Temperature over I2C

I picked up the Adafruit MPL115A2 - I2C Barometric Pressure/Temperature Sensor and connected it up using I2C and ran their sample program. First thing I learned is that I really should read the data sheets. The resolution is 0.15 kPa (=1.5 hPa a unit I seldom encounter) and the absolute accuracy is 1 kPa. Thus, values that vary a lot and differ from Environment Canada shouldn't be a surprise. Here's some sample output:

Pressure (kPa): -2929.0664 kPa
Temp (*C): -12035.9 *C
Pressure (kPa): 102.2696 kPa
Temp (*C): 22.6 *C

Note that the large negative numbers are ridiculous, and I don't know where they're coming from. The reasonable numbers seem to be within spec.

So I went to the adafruit forum and looked up the product. Others were having the same problem. That made me think it was software. Following some suggestions, I started messing with the library and found the bug. I fixed it, then posted about the fix, then posted new source code (that actually got accepted) to the code repository, which may make me a geek.

Bottom line: Spend a little more and get the BMP085 Breakout that provides accuracy high enough to detect raising and lowering the board by a metre. Unfortunately it also seem to be fragile enough that I busted one soldering it into a protoboard, so be careful with heat and static.