3. Software

<p>This sample program reads the temperature from the MLX90614ESF&#8217;s RAM using SMBus. The program works with our <a href="/category/8/robot-controllers">Orangutan robot controllers</a> (except for the Orangutan X2) and uses the <a href="/docs/0J20">Pololu AVR C/C++ Library</a> to print the measured temperature to the LCD. For specific information on how to program your Orangutan, read its User&#8217;s Guide, which can be found on the resources tab of the product&#8217;s website.</p> <pre name="code" class="c">&#x000A;#include &lt;avr/io.h&gt;&#x000A;#include &lt;pololu/orangutan.h&gt;&#x000A;&#x000A;void i2c_start() {&#x000A; TWCR = (1 &lt;&lt; TWINT) | (1 &lt;&lt; TWSTA) | (1 &lt;&lt; TWEN); // send start condition&#x000A; while (!(TWCR &amp; (1 &lt;&lt; TWINT)));&#x000A;}&#x000A;&#x000A;void i2c_write_byte(char byte) {&#x000A; TWDR = byte; &#x000A; TWCR = (1 &lt;&lt; TWINT) | (1 &lt;&lt; TWEN); // start address transmission&#x000A; while (!(TWCR &amp; (1 &lt;&lt; TWINT)));&#x000A;}&#x000A;&#x000A;char i2c_read_byte() {&#x000A; TWCR = (1 &lt;&lt; TWINT) | (1 &lt;&lt; TWEA) | (1 &lt;&lt; TWEN); // start data reception, transmit ACK&#x000A; while (!(TWCR &amp; (1 &lt;&lt; TWINT)));&#x000A; return TWDR;&#x000A;}&#x000A;&#x000A;void i2c_receive_pec() {&#x000A; TWCR = (1 &lt;&lt; TWINT) | (1 &lt;&lt; TWEN); // start PEC reception, transmit NACK&#x000A; while (!(TWCR &amp; (1 &lt;&lt; TWINT)));&#x000A;}&#x000A;&#x000A;void i2c_stop() {&#x000A; TWCR = (1 &lt;&lt; TWINT) | (1 &lt;&lt; TWSTO) | (1 &lt;&lt; TWEN); // send stop condition&#x000A;}&#x000A;&#x000A;//Returns 100 times the temperature read by the sensor giving a 0.01 degree resolution.&#x000A;long i2c_read_temperature_f() {&#x000A; long low_byte, high_byte;&#x000A;&#x000A; DDRC = 0; // all inputs&#x000A; PORTC = (1 &lt;&lt; PORTC4) | (1 &lt;&lt; PORTC5); // enable pull-ups on SDA and SCL, respectively&#x000A;&#x000A; TWSR = 0; // clear bit-rate prescale bits&#x000A; TWBR = 192; // produces an SCL frequency of 50 kHz with a 20 MHz CPU clock speed.&#x000A;&#x000A; i2c_start();&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x08 (Start condition transmitted).&#x000A;&#x000A; i2c_write_byte(0);// 0 is the universal write address for slaves.&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x18 (SLA+W transmitted ACK received).&#x000A;&#x000A; i2c_write_byte(0x07); // read TObj1 (0x07) from RAM&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x28 (Data transmitted ACK received).&#x000A;&#x000A; i2c_start();&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x10 (Repeated start has been transmitted).&#x000A;&#x000A; i2c_write_byte(1); // 1 is the universal read address for slaves.&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x40 (SLA+R transmitted ACK received).&#x000A;&#x000A; low_byte = i2c_read_byte();&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x50 (Data received ACK received).&#x000A;&#x000A; high_byte = i2c_read_byte();&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x50 (Data received ACK received).&#x000A;&#x000A; i2c_receive_pec(); // read packet error code (PEC)&#x000A; // The expected value of TWSR &amp; 0xF8 is now 0x58 (Data received NOT ACK received).&#x000A;&#x000A; i2c_stop();&#x000A;&#x000A; // Tk is temperature in Kelvin, Tf is temperature in degrees Fahrenheit, To is the raw&#x000A; // value of the object temperature as returned by the sensor&#x000A; // 100 Tk = To × 2 (from the datasheet section 8.7.2--To has the units 0.02K)&#x000A; // Tf = Tk × 9/5 - 459.67 (conversion from Kelvin to Farenheit)&#x000A;&#x000A; // 100 × Tf = 100 × Tk × 9/5 - 45967&#x000A; // 100 × Tf = To × 2 × 9/5 - 45967&#x000A; // 100 × Tf = To × 18/5 - 45967&#x000A; return (256*high_byte+low_byte) * 18/5 - 45967; // return temperature in units of 0.01°F&#x000A;}&#x000A;&#x000A;&#x000A;int main()&#x000A;{&#x000A; long object_temperature_f;&#x000A; clear();&#x000A; print("press");&#x000A; lcd_goto_xy(0, 1);&#x000A; print("button");&#x000A;&#x000A; wait_for_button(ALL_BUTTONS);&#x000A; clear();&#x000A; while (1) // loop forever&#x000A; {&#x000A; object_temperature_f = i2c_read_temperature_f();&#x000A; &#x000A; lcd_goto_xy(0, 0);&#x000A; // print the temperature in degrees Fahrenheit on the LCD (this code formats it nicely)&#x000A; if (object_temperature_f &lt; 0)&#x000A; {&#x000A; print("-");&#x000A; object_temperature_f = -object_temperature_f;&#x000A; }&#x000A; long integer_temperature = object_temperature_f / 100;&#x000A; print_long(integer_temperature);&#x000A; print(".");&#x000A; long decimal = object_temperature_f - integer_temperature * 100;&#x000A; if (decimal &lt; 10)&#x000A; print("0");&#x000A; print_long(decimal);&#x000A; print_character(223); // degree symbol&#x000A; print("F ");&#x000A; delay_ms(75);&#x000A; }&#x000A;&#x000A; return 0;&#x000A;}</pre>

Related Products

MLX90614ESF-AAA Infrared Temperature Sensor 90° FOV
Orangutan SV-168 Robot Controller
Orangutan LV-168 Robot Controller
Baby Orangutan B-48 Robot Controller
Orangutan SV-328 Robot Controller
Baby Orangutan B-328 Robot Controller
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors