Projects

The Heat Is On Thermostat: Part 2

DIY Digital Thermostat

Johann Wyss

Issue 34, May 2020

This article includes additional downloadable resources.
Please log in to access.

Log in

Completing the Arduino-based digital thermostat with a PCB and 3D printed case.

BUILD TIME: 2 HOURS (+ 3D PRINTING TIME)
SKILL LEVEL: INTERMEDIATE

THE BROAD OVERVIEW

In our prototype, we used an Arduino-compatible Nano board, 1602 LCD screen, switches, LEDs, and other components on a breadboard.

The prototype took a temperature measurement from the DS18B20 temperature sensor and indicated the output on two LEDs (Red to indicate heating, and blue to indicate cooling).

For our main build, we will use an ATmega328P as the microcontroller, which takes up much less space compared to an Arduino Nano, and is more reliable compared to running a development board for long durations. We will output to a relay board module, which provides a flexible method to control your intended device.

Finally, we provide a 3D printed enclosure, which you can modify to suit your needs.

THE PCB

We have designed a circuit board using EAGLE PCB design software, which you can download from our website to send to your favourite PCB manufacturer.

TOP SIDE
BOTTOM SIDE

Note: The PCB we are describing in this project is an older revision of the board files that we will be providing. Shown here, is the final revision (REV: D). Some small changes had been made from the previous revision but sadly, due to shipping delays related to the terrible pandemic, we were unable to get the final revision before the print deadline. Therefore, you may notice some slight differences in the board shown here and the provided EAGLE CAD files.

After getting our Revision C PCB manufactured and delivered, we encountered some issues with the circuit. The first and most significant, was a random reset condition where the microcontroller randomly reset when our hands were placed near the PCB.

We greatly reduced this phenomenon by adding a 10kΩ pullup resistor to the reset pin. This isn’t usually needed under normal conditions because the ATmega328P has an internal pullup active. However, the Application notes for AVR Microcontroller Hardware Design Considerations, suggest that in high noise situations, the internal pullup may not be sufficient and could result in sporadic resets, confirming our suspicions.

You can download this application notes here: http://ww1.microchip.com/downloads/en/AppNotes/00002519A.pdf

This image shows you the 10kΩ resistor soldered between the reset pin and Vcc. (You won’t need to do this if you are using our REV: D design).

It’s worth noting, however, that even with this reset pullup, the microcontroller will reset if we touch the top right pins of the ATmega328P. These pins are the Analog 0 to analog 5 pins which are not being used in this project, and we are unable to explain why this is happening. That being said, with the microcontroller enclosed in a case, it's impossible to touch the pins and, as such, the device works fine. We will, however, plan to investigate this and why our new lab seems to have significantly more noise than our previous.

For PCB revision D, we also added a 4-pin header, which are electrically connected to the LEDs. This will allow you to connect external LEDs to the board without needing to solder wires directly to the LED footprints. We don’t advise using the project with both the external LEDs and the LEDs soldered to the board. Doing so could cause the LED with the lower forward voltage to prematurely fail, as it could pull more current than the LED with a higher forward voltage. If you’re using the design in an enclosure, just use the external LED header and leave the LED footprint unpopulated.

As with all of our PCBs, we have included a top and bottom layer ground plane. We have, however, adopted a slightly different technique. Previously, we used a solid top and bottom ground plane which has, on occasion, made it difficult for low thermal capacity irons to solder to. In an attempt to rectify this, we increased the size of thermal relief gaps slightly, and now make one layer a hatched pattern, reducing its thermal mass, and thus, providing a lower load on the soldering iron.

The Build:

ASSEMBLY

The first step in our Thermostat project’s construction is to assemble the circuit board. We suggest you solder the smallest / lowest profile components to the board first, working your way up in size. I.e. start with the resistors, crystal, small capacitors, up until the largest components, including the TO220 regulator and screw terminals. This way, the board will lay as flat as possible on the bench as you solder.

While you solder the components to the PCB, pay close attention to the components that are polarised, including the electrolytic capacitors, LEDs and diode.

1N4004 Diode

The 1N4004 diode (D3) is for reverse polarity protection in case you apply power the wrong way around. Install this diode so the band printed on the diode matches the white line printed on the PCB.

IC Socket

Make sure you solder the IC socket into the PCB so that the notch matches the key printed on the board.

This image shows you the differences between a machined and standard IC sockets, and also how the ATMega328P ICs have their notch identified with a printed label or the round indent to show pin 1.

Electrolytic Capacitors

Both of the 100µF electrolytic capacitors (C1 and C7) need to be mounted with their anode (usually the longer leg) into the PCB marked with the +.

THE WIRING HARNESS

Once all of the components are soldered to the PCB, it’s time to make the wiring harnesses for the switch, external LEDs, and the DC input.

Wiring

To make the wiring harness, first prepare lengths of 26AWG wire. We cut these to suitable lengths so they can reach between the PCB and the positions on the panel. We found the best way to do this was to have the PCB mounted to the bottom of the case, and the front and rear panels attached. This way, you can easily measure physically how wire much is needed. We added about 5cm more length so that the harness is easier to work with when we assemble the project.

Header connectors

We’ve designed the external components to attach to the PCB using a pair of male and female headers (an inexpensive DIY connector). We solder the male headers to the PCB and use the female header to connect to it. You simply cut the header to the required size.

Parts Required:JaycarAltronicsCore Electronics
1 x ATmega328P MicrocontrollerZZ8727Z5126CE04547
1 x 1602 LCDQP5521Z7011A**DFR0063
1 x LM7805 Linear Voltage RegulatorZV1505Z0505CE05291
1 x 10K PotentiometerRT4360R2480BCOM-09806
3 x Tactile SwitchesSP0600S1120ADA367
1 x Temperature Sensor DS18B20--DFR0198
1 x 5mm Red LEDZD0150Z0800COM-12062
1 x 5mm Blue LEDZD0185Z0869COM-12062
1 x 16MHz CrystalIncluded with ZZ8727V1289ACOM-00536
2 x 22pF Capacitors*RC5316R2814CE05206
5 x 100nF Ceramic Capacitor*RG5125R2865CE05188
2 x 100µF Electrolytic CapacitorRE6130R5123CE05258
1 x 4.7K Resistor*RR0588R7574COM-10969
4 x 10K Resistors*RR0596R7058PRT-14491 or COM-10969
3 x 330Ω Resistors*RR0560R7546PRT-14490 or COM-10969
2 x Screw Terminals 5.08mm PitchHM3130P2040PRT-08432
1 x Female Pin Header StripHM3230P5390PRT-00115
1 x Male Pin Header StripHM3211P5430POLOLU-965
1 x 28 Pin IC SocketPI6510P0571PRT-07942
1 x SPST Rocker SwitchSK0984S3210POLOLU-1406
2 x LED Sockets*HP1102H1553COM-11840
4 x M3 x 15mm Screws*HP0407H3130AFIT0273
4 x M3 Nuts*HP0426H3175FIT0273
8 x #4 6mm Screws*HP0550H1145-
1 x Case Mounted DC Input JackPS0522P0622-
1 x DC Power Jack (optional on PCB)PS0519P0626RT-00119
Wire AssortmentWH3025-PRT-11375

Parts Required:

* Quantity shown, may be sold in packs. You’ll also need a breadboard and prototyping hardware.

Note: When cutting the female header, we have the male header inserted into it. This helps to identify where to cut, and also helps to stop the female header from splitting across the required pin.

With the female header cut, solder a wire to each of the pins and insulate it with heatshrink or liquid electrical tape.

Switch Wiring

Solder your prepared wires to the switch. You may want to add short pieces of heatshrink before soldering, as you see here, then apply heat to shrink these once you have completed the soldering.

Your switch harness should look similar to this image (shown before we applied heat to the heatshrink).

The switch will be inserted into the front panel and the female header will connect to the male header (JP2), named switch.

DC Jack Wiring

You can now repeat the same procedure on the DC jack. However, in this case, polarity is important.

You should use a multimeter in continuity mode to identify which tab is connected to the centre pin, and which is connected to the outer shield.

To assist with identifying the right connections on a DC jack, we use a DC jack to screw terminal adaptor. You simply put one probe to the DC jack tab and the other on the screw of the screw terminal.

We recommend you use red or orange coloured wire for the positive and black coloured wire for the negative.

Once your DC wiring is complete, you can connect it to the PCB, making sure the red/orange wire go to the pin labelled as +12V, and black to GND.

LED Wiring

Follow the same process and wire colours for both LEDs. The red or orange wire to the longer leg (Anode), and black to the shorter leg (Cathode).

Connect the LEDs to the PCB making sure the red/orange goes to the pins labelled as "+", and black to the pins marked "-".

LCD Wiring

The LCD is connected to the PCB using standard female to female jumper wires. These wires simply plug into the male headers on the PCB and the LCD module. Note that both the PCB and the LCD have the pins marked on them. For this project, you do not need to connect the pins marked DB0, DB1, DB2, DB3. These pins are not used.

3D PRINTED ENCLOSURE

We designed the enclosure using Fusion 360. There are 15 pieces in total to be printed, which consists of 4 x Feet, 2 x Grommets, the top and bottom halves, and the front and rear panels.

The 3D print files and working file can be downloaded from our website.

Front and rear panels

The front and rear panels were printed on our Flashforge Creator Pro at 200-microns layer height using white and black Flashforge branded PLA. Whilst the printer is a dual extruder, we didn’t use the Flashprint slicer for this. Instead, we used the Simplify 3D slicer to create two individual sets of gcode per panel. The first was using the extruder loaded with white PLA, with the slicer set to only slice from 0.0mm to 1.6mm of the panel. We then re-sliced again using the second extruder, which was loaded with black PLA and set to start printing at 1.6mm.

All we needed to do then, was print the first (white) file, and once it was done, get the printer to print the second (black) file. The front panel took 45 minutes, and the rear panel, 50 minutes.

Top

The top was printed using white Flashforge branded PLA on our Flashforge Creator Pro. We used the 200-micron layer height and in this orientation, it took about 6 hours to print.

Bottom

The bottom was printed using the same 200-micron layer height and using the same white Flashforge PLA. This took about 5 hours to print in this orientation.

Buttons

The three buttons were printed all in one go on our Cocoon Create i3, using red Flashforge branded PLA at a 200-micron layer height. In this orientation, it took about an hour to print.

Note: Due to the buttons tall cylindrical shape with a tiny surface area, we strongly suggest either printing them on a raft or with a significant brim. This will prevent them from falling over during printing.

Grommet

The grommet is designed to hold the cable of the temperature probe firmly to the front panel. It works as both a flexible strain relief and to fill in the gaps around the cable, producing a cleaner finish. The grommet is pushed into the panel around the temperature probe cable and is securely cable tied to the cable, holding it firmly in place. The grommet halves were printed together at 200-micron layer height using Aurarum branded black TPU flexible filament. This was printed at a snail’s pace of about 25mm/s to avoid the filament jamming in the extruder. In this orientation, it took around 20 minutes for them to print.

Feet

The feet were also printed in Aurarum black TPU on the Flashforge Creator Pro, and at the same 25mm/s speed. All four printed together and took about 30 minutes to print. These feet need to be glued to the bottom base. We used hotglue, but recommend you use a two-part epoxy for a more permanent bond.

RELAY OUTPUTS

It is very important to note that an ATMega328P IC can only safely output about 40mA. A higher current draw will more than likely overheat and damage the IC. For this reason, the output of this project is just a 5V trigger signal. You will need an interface between this signal and the load you plan to operate, ensuring the transistor, FET or relay do not exceed that load limitation, and preferably use no more than 20mA.

You could output the project’s output signal to the base of an NPN MOSFET, for example. When the signal goes high, it would allow current to flow from VCC (12V) into the load and through the MOSFET to ground.

Another method is to use a relay. Some low current relays are available, such as Jaycar’s SY4092 that claims to draw only 20mA, however, this PCB mount relay obviously needs its own circuit board, flyback diode and terminals to make it practical.

We also researched other relays on the market to see what current their coil would require to actuate. Surprisingly, an SRD-5VDC relay from manufacturer Songle, would need over 70mA to actuate. This current would certainly damage the ATmega328P. Additional circuitry would be needed to drive this relay.

Since we are not entirely sure what application you will put your Thermostat project to, we have kept the output purely to a signal wire. However, we also added the obviously necessary common ground connection, as well as a 12V input wire. This way, you can use the voltage input to the circuit to drive the signal to power higher voltage devices such as 12V relays.

Thankfully for us makers, there are now handy relay modules available that have all the necessary circuitry built onto them.

Jaycar 8-Channel Relay Board XC4418
Relay Module's Schematic

These relay modules will be able to convert the 5V signal from the ATMega328P into a 12V signal, capable of driving the relay coil.

As you can see in the relay module’s schematic shown here, the signal wire goes into an optoisolator, which galvanically isolates the input circuitry from the rest of the circuit. The output of the optoisolator is then used to drive the base of a transistor, which allows current from the 12V input into the coil of the relay, allowing it to actuate.

APPLICATION EXAMPLE

We will show you an application example using a 12V fan for cooling, a 12V globe as a heater, connected to a relay module.

Note: These relay modules are usually sold with one, four or eight relays onboard. You only need two relays for this project - one for heat and the other for cool. In our case, we only had access to an eight relay module. You can use two single relay modules or a four relay module if you have access to those.

In this example, connect the circuit to the relay module as described here.

IMPORTANT NOTE: We do not recommend you use the 12V output from the thermostat to power the heating and cooling devices. These should be independently powered and left floating from the rest of the system to reduce transient voltages that may interfere with the circuit.

The silkscreen label on the relay’s output terminals indicates the normally open or normally closed connections. The centre pin label that looks like a wiper is the common pin. Looking at the diagram, you will notice that this common pin wiper is pointing towards the first pin. This is because in a resting state the contacts are connecting the com pin to first pin. Thus, the first pin is called the normally closed pin.

When we trigger the relay, the contacts/wiper moves from the normally closed position causing that pin to go open circuit and connect to the other bottom pin instead. This bottom pin is called the normally open connection.

You can use any power source here (see warning below), provided that it is compatible with the ratings listed on the casing of the relay and the relay’s module board, etc. It’s important though to switch the positive or live wire, and not the negative or neutral wire. Doing so, could mean the heater or cooling device would have potentially lethal voltage in them when not active and could lead to electrical shock.

WARNING: While these relay modules can usually switch mains voltages with their 240VAC/10A contact ratings, you must be a licensed electrician to work with mains voltages or any voltages above 50VAC in Australia and New Zealand. Simply put, mains wiring can KILL, so it’s best to keep this project to low voltages only, such as 5V, 12V or 24V.

TESTING

To test our project, we designed and 3D printed a test jig to allow us to create a controlled environment. We will add the .stl for this file but be warned, it’s a 11 hour print.

The test jig we built consisted of an 80mm 12V DC fan salvaged from an old PC. This fan will simulate the cooling process.

The heating will be done using a 12V 35W halogen bulb, soldered to a small piece of perfboard.

When the temperature is lower than desired, the thermostat will trigger the heating signal, which will actuate the heating relay to illuminate the bulb.

This bulb heats the chamber, which also has the temperature probe in it (cable tied to the very top of the chamber).

When the temperature reaches the upper threshold, the microcontroller stops sending the heat signal and the relay contacts open, stopping current to the bulb. If the temperature inside the chamber exceeds the upper threshold, the microcontroller triggers the cool signal, which turns on the fan. The fan pulls air through the vents in the heating side of the jig, pulling air over the hot bulb and through holes in the top of the heating chamber. The air will continue to exhaust the warm air until the temperature falls below the lower threshold.

OUR FINDINGS

Our test confirmed that the device was indeed regulating the temperature. Strangely though, there was some undesired oscillations when the temperature was on the cusp of a threshold, indicating that our hysteresis coding wasn’t quite up to scratch. Due to time constraints, we didn’t have time to resolve this issue, but plan to after we go to print. Keep an eye on our website for updated code.

WHERE TO FROM HERE?

With the exception of still needing to make a few small modifications to the code (which we will upload to the website when completed), the thermostat is a finished product with little need to make any changes.

If we were to revisit this design, we would make a few improvements:

  • We would improve the output by using a transistor to protect the output pins of the microcontroller in the event of a short-circuit, etc. This would also allow our thermostat to directly trigger a relay, removing the need for a relay module.
  • We would also look at using a PID (Proportional–Integral–Derivative) algorithm, which converts the simple on/off control style of thermostats such as this, into a much more dynamic pulse width modulated (PWM) controlled device. Such devices are significantly better at keeping the output at a precise level. However, the PWM output can be a limitation as to what devices you can connect to them. For example, an incandescent or halogen lightbulb is simply a resistive load (not purely resistive but generally speaking a resistive load), and will work fine with a PWM signal driving it as would a simple DC fan, however, many systems are not tolerant to being controlled directly from such high speed on and off control.

Note: Small computer fans may be small brushless direct current (BLDC) or similar, requiring care to control in PWM, if at all possible.

Part 1

Johann Wyss

Johann Wyss

Staff Technical Writer