The STCN75 is a high-precision CMOS temperature sensor with an on-chip converter that turns its readings into digital data compatible with the I2C standard. It offers 0.5°C resolution and ±2°C accuracy from −25°C to 100°C, making it an appealing component to try. With Protón+, we're returning to practical microcontroller projects with an instrument that is useful in all kinds of settings.
Ever since people began measuring time with simple clocks, they have wanted to quantify the weather around them: temperature, humidity, UV radiation, atmospheric pressure and other basic measurements. Combined appropriately, such readings provide the information meteorology relies on.
The goal: measure temperature at a distance
This design is a digital thermometer that can be connected over a simple two-wire line—such as a telephone pair—over distances greater than one kilometre. It lets you monitor temperatures remotely and, using a computer application, switch heating or cooling equipment without being physically present.
Parts used in the project
Choosing a digital sensor
Basic temperature meters often use special resistors whose values change with temperature, such as NTCs and PTCs. Discrete diodes, correctly biased, can also exploit the linearity of one of their properties to turn current flow into a temperature reference. More sophisticated, familiar options include the LM35 and LM335, which provide microcontrollers with analog voltages corresponding to the temperature being measured.
All these approaches share a challenge: keeping the supply voltage to the analog circuitry stable and accurate. Changes in the supply can introduce errors of several degrees or cause readings to fluctuate so much that they become difficult to interpret.
STCN75 connection diagram
To avoid designing complex regulated and compensated circuits, this project uses an entirely digital IC. Its SDA and SCL connections carry data over an I2C bus to the microcontroller, and it supplies useful information as a 16-bit word. This avoids analog measurement circuitry that could introduce errors, helps address noise and supply variations, and makes more complex operation possible by setting the STCN75's internal registers.
STCN75 block diagram
Configuring those registers lets the chip work as a thermostat, set temperature alarms or enter a low-power mode when it is not needed. You can also define a temperature window: if the reading falls below its minimum or rises above its maximum, the OS/INT output can activate an alarm or other warning system. The STCN75 datasheet explains how to configure each register; consult it to understand the settings used here.
At power-up, the device can be read without first writing to any register, which simplifies microcontroller programming. Read register address zero, where the 16-bit temperature word is stored, and send the result to an LCD connected to the microcontroller. The controller can repeat this read cycle continuously to track the temperature wherever the STCN75 is installed. Its SO8 package also suits compact boards. You already know how to solder SMD, as we covered earlier.
Package-size comparison and IC ready for soldering to the board
The microcontroller
The selected device is the PIC 16F628A, a microcontroller we've discussed before. You can find a trainer and even a programmer—often called a burner—to continue experimenting as you move on to other projects. I use a GTP-USB+ myself and have had excellent results, but the device mentioned above will also serve you well as you get started.
The initial circuit
The most prominent part of the schematic is the connection between the 16F628A and the LCD. Below that is the relay for switching a heating or cooling system, along with the temperature sensor and resistors used as pull-ups on both I2C bus lines. Above the microcontroller is a 20 MHz external crystal that provides the system clock.
The 16F628A's internal 4 MHz oscillator could have been used. However, a later installment will use this circuit for an RS485 connection at 34,800 bps, which calls for a high-frequency crystal. The free version of the software used to program the PIC also supports only 4 or 20 MHz crystals.
Schematic of the initial circuit
On the left, pin 4 connects to the components that provide the microcontroller's reset during initialization. If you like, add a push-button from pin 4 to GND so you can reset the system without disconnecting it. Farther left is the ICSP connector (In-Circuit Serial Programmer), which lets you program the PIC repeatedly without removing it from the board. This saves time and avoids wear on the microcontroller and its socket, both of which can be damaged by repeated insertion and removal.
The circuit also includes a 5-volt power supply, which readers have already learned to build from the magazine. The transformer is rated at 2 × 7.5 volts to keep the 7805 regulator running cool. The LCD backlight connects to the 5-volt supply like an LED, but through a 47-ohm resistor that is not shown in the circuit.
Firmware for the PIC
There is rarely agreement on the best software for writing a PIC program. Some prefer MPLAB, others C and others BASIC; all are valid choices. What matters is that the program loaded into the microcontroller does the job its developer intended and provides the required features.
Protón+ Lite installation screen
We'll use Protón+ LITE, whose demonstration version has certain restrictions described on its download site. We're working to obtain the full version so we can begin developing projects with USB connectivity and computer interfaces for displaying instruments, dataloggers and other interactions between a design and a PC. For this first project, however, the available version is sufficient: it can compile the firmware and create the .HEX file needed to program the PIC. USB connectivity, our longer-term goal, requires the full version. What do you think?
Structuring a program for the PIC
Protón+ is approachable for newcomers, but like any programming language it requires a structured sequence of lines to compile correctly. Begin by specifying the device and the crystal frequency.
Program header and microcontroller selection
Anything preceded by an apostrophe is treated as a comment. It is ignored both during compilation and when counting the lines allowed by Protón+ LITE. So although the example reaches line 11, only two lines count toward the limit.
After choosing a supported microcontroller and the 20 MHz crystal frequency, define the peripherals to be used. Set CMCON to the datasheet value that disconnects the internal comparators, then specify fully digital operation with ALL_DIGITAL = TRUE. Next define the LCD connections: DTPIN identifies the microcontroller pin connected to the LCD's DT terminal, and RS and EN identify their respective pins. The LCD definition also specifies a 4-bit connection through INTERFACE, the number of display rows through LINES, and the display type: 0 for alphanumeric or 1 for graphics.
Initial firmware declarations
Next tell the microcontroller which pins will serve as SDA and SCL, the two I2C bus lines. Assign the label LEER the value used to address the STCN75. That value tells the controller which device it is calling; other I2C devices can share the bus and communicate when requested. The STCN75 address is the binary number %10010001. The SYMBOL instruction assigns that address to LEER.
Before defining the microcontroller's tasks, declare the names and sizes of the variables that the firmware will use to store data. The program's HELP file explains these details. For example, a WORD variable can be split into two BYTE variables, LOW and HIGH, and given new names for use in the firmware.
A short DELAYMS of 100 milliseconds helps stabilize the system. Then clear the display with CLS and initialize selected variables to known values rather than leaving them at random.
The firmware's main loop
This is where the interaction with the outside world takes place, through the temperature sensor and relay RL1.
Program's main loop
The main loop begins at the INICIO label and reads the ambient temperature. Depending on the programmed minimum and maximum, it activates or deactivates relay RL1, which in this example switches a heating system. First open the bus with BSTART. Then use BUSIN to communicate with the I2C device at address LEER—the temperature sensor here—and load register 0 into variable B with BUSIN LEER, 0, [B]. The microcontroller then closes the I2C connection with BSTOP.
Earlier, B was declared as a WORD, or a double BYTE. Protón+ has now split it into C = B.HIGHBYTE and D = B.LOWBYTE. The STCN75 datasheet and the comments in the program explain the resulting values. In brief, C contains the integer part of the temperature, positive or negative, while D indicates whether the reading is an integer number of degrees or has an additional five tenths, matching the sensor's resolution.
The next instructions display the reading on the LCD. The first row can show the word TEMPERATURA, centered as well as possible. On the second row, leave five spaces and then display variable A, which is blank above zero and contains a minus sign below zero. Follow it with C in decimal form, a comma, and F to represent the fractional part. ASCII code 158 is the degree symbol; finish with C to indicate Celsius.
Finally, compare the reading against the reference values that control the relay. If C is below 20, set the relevant output high to activate the relay; if it is above 26, set the output low. This creates a control window: the heating switches on below 20 degrees and switches off above 26.
You can change the thresholds to control a car or bedroom air conditioner, or perhaps keep a terrarium at a stable temperature by switching a heater. The program ends with the END instruction.
Thermometer in operation
Final notes
We have not included the program listing, even though it is short. Instead, the images let you write it yourself and think through what each part does—no copy and paste. In the next installment, we'll add an RS485 bus so the thermometer can be monitored remotely from a computer, following its operation from hundreds of metres away.
There are many I2C thermometer ICs on the market, some potentially more accurate than the one used here. The LM92 from National Semiconductor is one example. The STCN75 serves as a practical illustration of I2C and the flexibility it offers when used with microcontrollers.
PCB layout used for the sensor
Reader project: Here is a project by our reader and friend Yesbond, who kindly made a video showing his work with a TMP100 temperature sensor. Like the STCN75 in this article, it communicates over I2C. Congratulations, Yesbond—and thank you for sharing your work!