Wednesday, March 08, 2023

Teensy 4.1 with TMP117 Temperature to USB MIDI

Overview

The TMP117 is a high precision temperature sensor with I2C output and can measure between -55˚C to 150˚C. 


Hardware







The following connections should be made between the Teensy 4.1 and the TMP117:
  • Teensy 4.1 3V to TMP117 VCC - orange in the above photo
  • Teensy 4.1 ground to TMP117 GND - black in the above photo
  • Teensy 4.1 pin 19 / A5 / SCL / PWM to TMP117 SCL - yellow in the above photo
  • Teensy 4.1 pin 18 / A4 / SDA / PWM to TMP117 SDA - yellow in the above photo




In order to set the correct I2C address for the TMP117 device for this code example, make sure the first bit of the four address bits is set and the rest are cleared. 

Software

The Arduino code uses the Wire library to communicate with the TMP117 via the I2C bus. The TMP117-Arduino library by Nils Minor is required and can be installed via the Arduino IDE. If the temperature changes, the new value is sent as a scaled, simple USB MIDI control change message. 

#include "TMP117.h"

double temp;
int data;
int data_prev;
const int chan = 1;
const int cc = 1;

TMP117 tmp(0x48);

void setup() {
Wire.begin();
Serial.begin(115200);
tmp.init(NULL);
}

void loop() {
temp = tmp.getTemperature();
data = constrain(map(temp, 0.0, 48.0, 0.0, 127.0), 0.0, 127.0);
Serial.println(temp);

if (data != data_prev) {
usbMIDI.sendControlChange(cc, data, chan);
delay(10);
}
}



The temperature can be viewed in Max via this example patch: 






0 comments: