This tutorial will explore how to build a temperature display using the AT30TS750 on the I2C and SPI Education Shield and the I2C Display v2.

Background
I2C Basics
Changing the I2C Library
IS31FL3728 Datasheet
AT30TS750 Datasheet
AT30TS750 Functionality Overview
AT30TS750 Retrieving Temperature Value


AT30TS750 Temperature and Display

Being able to make something practical is just as fun as blinking LEDs, playing with color mixing and making larson scanners. The I2C and SPI Education Shield has several data gathering chips on it, one of which is the AT30TS750 temperature sensor from Atmel. It’s not a complicated chip to work with, and I’ve linked to several tutorials above if you need to get some of the basics for how to work with the chip. Our goal will be to query the chip for the current temperature (to the nearest 0.5° C) and display that value on the I2C Display, complete with a decimal place and degree symbol.

The plan for how the code will need to run is pretty straight forward:

  1. Set up our register constants
  2. Initialize whatever needs to be initialized
  3. Query the temperature sensor for the current value
  4. Prep that value to something that can be displayed
  5. Update the display

The register lists have been documented across all the previous tutorials, so I’ll just paste those values in from their original code to save time. While thinking ahead to what we’re going to do, we’ll need to display numbers in the digit display so we’ll need to copy in the code for the number pattern array as well. Since both chips are I2C based, all we need is the I2C library to interface with them, and for this implementation, we’ll keep their initialization simple. Retrieving the temperature data is a simple I2C Read operation, but prepping the value for display may take a bit of work just because there are values to the left of the decimal place, values to the right of the decimal place, and the decimal place itself, plus a degree symbol we need to conjure up. The final act of writing the value to the display is pretty simple too, since we’ll have numbers to work with, that should correspond easily to the number pattern array.


 

Declarations

If you’ve gone through the AT30TS750 and I2C Display tutorials, then nothing in the declarations area should look new to you: it’s all registers and constants that we’ve worked with in the past. The degree symbol pattern was created using the I2C_Display_v2_Pattern_Generator, and added here as a constant for use in our sketch.

Setup

Again, the setup is straightforward. Enable the speaker and shutdown pins, then drive the shutdown pin high, configure I2C, then initialize the temperature sensor and LED driver. Since the display is plugged directly into the Education Shield, we don’t need to use the internal pull up resistors. For the chip setup, we only need 9 bits of temperature data and the LED driver can be configured with defaults. Finally, we clear the display of any patterns that might have persisted from a reset, and write the degree symbol to the DIGIT 3 space.

setDigit

In our main loop, we’ll retrieve the temperature value from the sensor, but we need to push it out to the display. We want the value in the tens position to be in DIGIT 0, the value in the ones position to be in DIGIT 1, and since we’re capturing half degree increments, the decimal value to be in DIGIT 2. DIGIT 1 also needs to have the decimal point LED enabled which is in bit position 0 for each segment group. We’ll leave the parsing of the temperature values to the main loop, and just send this function the digit we want to write to and the value we want to write to it. If the location we’re writing to is DIGIT 1, that means it’s the ones place, and in order to light up the decimal point LED, all we have to do is take the pattern and add 1 to it, enabling the 0th bit for that digit.

The number patterns are stored in the numPatterns array created in the declarations area, and each value matches with it’s position in the array index: the pattern for “0” is at numPattern[0], the pattern for “7” is at numPattern[7], etc.

Main Loop

The majority of this code is repurposed from the AT30TS750 sketches written for previous tutorials (again, linked above), so if you want greater detail, check those links out. Here’s what’s going on in a nutshell though.

We will be retrieving two bytes of data from the temperature sensor: the first byte is the most significant byte containing the whole number portion of the temperature, the second byte is the least significant byte containing the fractional portion of the temperature multiplied by 16. We’ll create a pair of variables called tempLSByte and tempMSByte, to hold those values, then read the 2 bytes of temperature data from the AT30TS750. The first byte returned is the 8 bit whole number portion, and since I’m not worried about negative temperature values (which would be returned in two’s complement), we can just use that number as is. The fractional portion of the temperature is in the second returned byte, but occupies only bits 7-4, the rest are junk data. That means we have to take the returned value and shift it over four places to obtain the actual numerical value.

With those values in hand, it’s just a matter of sending the number we want to the correct digit. The tens place is sent to DIGIT 0 with the value obtained by dividing the whole number portion by 10 and ignoring any remainder. The ones place is sent to DIGIT 1 with the value obtained being the modulus (remainder only) of the whole number portion divided by 10. The decimal value is sent to DIGIT 2 with the value obtained by taking the fractional portion and multipying by 0.625.

The 0.625 value is 1/16 multiplied by 10. Since we configured the sensor for nine bit resolution, the fractional return will only ever be 0.5 or 0.0, which are retrieved from the sensor as B10000000 or B00000000. After shifting the value over, you are left with B1000 or B0000, 8 or 0. Dividing by (1/16) gives you the actual fractional value, but we only need the portion one digit right of the decimal place, so multiply it by 10.

Finally, we include a delay so that the display isn’t constantly flickering when it’s right at the threshold between two values.

Full Sketch

Modifying the Main Loop to Create a Temperature Alarm

There is a lot of functionality still present on the I2C Display that isn’t being used. So let’s add a couple lines of code and turn it into a temperature alarm.

If the temperature is below 27°C, have the bar graph display green.
If the temperature is between 28 and 30, have the bar graph display yellow.
If the temperature is above 30, have the bar graph display red and sound an alarm on the speaker.

All the registers and pins we need to do this are already present, so it’s just a matter of putting them to use!

For more I2C Display tutorials, Click Here.

Previous Post
I2C Display v2 Tutorial 03: Larson Scanner
Next Post
Number Conversion Practice Questions…
You might also like
Menu