This is the second part of a tutorial that will teach you how to build a portable heating device with Arduino. In this post, we will explore how to control a heating resistor with Arduino. This allows to keep your setup at the desired temperature.
- Introduction
- Step 1. Choosing the Sensor
- Step 2. Powering the Heater
- Step 3. Calculating the Base Resistance
- Step 4. Connecting the Components
- Step 5. The Code
- Conclusion
Introduction
In the first part of this tutorial, How to Build a Heater with Arduino – Part 1, we’ve discussed how to create and calibrate the component that will generate the heat. The problem so far is that there is no control over the temperature. If you’ve done your calculation correctly and respected the wattage of your resistors, there is no risk of overheating. However, there’s nothing that prevents your resistors from getting hotter than what you initially intended. This is particularly true if there is no ventilation, or if the heater is in direct contact with a surface that does not dissipate heat very well. The correct (and safe) approach to solve this problem is to introduce a sensor to measure the temperature. If it’s too cold, we’re turning the heating on; if it’s too hot, we’re turning it off. Please, keep in mind that what seems a trivial problem is in actuality incredibly challenging. Designing a responsive device that control an actuator (the heater) based on the reading of a sensor (the temperature) can be a major engineering challenge. This tutorial will provide a very simple solution, and point out how the design can be improved with a little bit of extra Maths.
Choosing the Sensor
Is important to understand that when it comes to temperature, there are a large number of choices. It’s important to understand that different sensors are based on different technologies, hence come with differed limitations and advantages. The main two classes of temperature sensors that are mostly used are integrated circuits and thermistors. Distinguishing them is very easy: the former have three pins, the latter only two.
Like the name suggests, thermistors are components which resistance value is very sensitive to temperature changes. Then a voltage difference is applied to its ends, its resistance will vary in response to changes in temperature. Thermistors are somehow a pain to use, since they require additional resistors and a deeper understanding of electronics. The relationship between resistance and temperature is in fact calculated using the Steinhart-Hart equation, which can be scary at first.
For the above-mentioned reasons, this tutorial will focus on the more Arduino-friendly integrated circuit sensors. For this tutorial, I have used the very common LM35. Two pins are reserved for the reference voltage (ground and 5V); the voltage on the other one will vary between these two, depending on the temperature.
If you want to expand your knowledge on sensors, Arduino sensor temperature comparison has ready-to-use recipes for the most used sensor types.
Powering the Heater
As seen in the previous part of this tutorial, the heater we have designed is likely to require much more current than the one that Arduino can provide. For this reason, it has to be powered separately. A safe and reliable option is to use a traditional phone charger. They usually range from 5V to 9V, and have a limit on the amount of current they can provide. This makes them perfect for this application, as they operate in the a safe range. If you need a serious heating device, you can power you heater directly from the main, although this is going to be very dangerous.
The problem we have now is to control the flow of current to the heater, using Arduino. This is the perfect time to use a transistor. Transistors are, de-facto, switches. They allow or prevent the flow of current in one direction, based on a specific input. We can use Arduino to turn the switch on and off based on the reading from the sensor. The one I am using for this project is a TIP120, which can operate high voltages. Make sure you have one that can be controller by the voltage provided by an Arduino.
Calculating the Base Resistance
The previous part of this tutorial referred to a battery-operated heater. The new schematics will assume that power comes from a phone charger instead. This changes the input voltage from to . If we want to dissipate of power, we need a total resistance of , safely distributed on 8 resistors of each (all the calculations are explained in the Part 1). Our new setup requires, to operate at full power, a current of . Make sure that the charger you’re using can provide this amount of current, since most of them are capped at .
It’s common practice to wire a resistor to the base of the transistor. This is the prevent an excessive flow of current that might damage both the transistor and the Arduino. Most designs use resistors between and . If you have the datasheet of your transistor, you can calculate how much resistance you actually need to be safe. The formula is:
Where:
- : the base resistance to connect to the transistor base;
- : the voltage provided to the base (for Arduino: );
- : the voltage of the transistor when fully on (see below);
- : the load current the transistor has to withstand (as calculated previosuly: );
- : the gain of the transistor, when subjected to a current of amps (see below).
To calculate the we need to refer to the datasheet of the transistor in use. We can read from this graph (TIP120 datahseet) that the TIP120 has a of 3000 at a current of . Similarly, it has a around :
By plugging all the numbers, we can calculate that , meaning we can use a 13KΩ resistor.
🪛 Recommended Components
Connecting the Components
The following diagram shows how all the components have been wired up. The transistor used, TIP120, is a NPN; it means that its emitter pin must be grounded.
Remember that in order for this design to work, you have to keep the temperature sensor close to the heating elements.
The Code
The final part is to write the logic that operates our heater. During every iteration, Arduino samples the temperature from the sensor and switches the transistor accordingly.
#define PIN_SENSOR A1 #define PIN_SWITCH 9 float target = 30; void setup() { pinMode(PIN_SENSOR, INPUT); pinMode(PIN_SWITCH, OUTPUT); Serial.begin(9600); } float getTemperature() { float data = analogRead(PIN_SENSOR); return (5.0 * data * 100.0) / 1024.0; // Celsius } void loop() { // Temperature read float c = getTemperature(); Serial.print("Temperature: "); Serial.println(c); // Regulation if (c > target) { digitalWrite(PIN_SWITCH, LOW); Serial.print("\tHeater OFF"); } else { digitalWrite(PIN_SWITCH, HIGH); Serial.print("\tHeater ON"); } delay(5000); }
The equation to read the temperature from the LM35 comes from the official documentation on Arduino Playground (LM35 Higher Resolution).
Conclusion
The code provided in this tutorial is simple, possibly too simple for this application. The sensor used is unreliable, making the system excessively sensitive to temperature small oscillations and noise. A better approach would be to take repeated samples over a longer period of time. Averaging them reduces the effect of noise on the final measure. If you want an optimal solution, however, you can use a Kalman filter. It’s a powerful tool that allows to attenuate and to remove noise from sensors. To read more about it, check the tutorial A Gentle Introduction to Kalman Filters.
Secondly, even the way the heater is controller can be improved. There are several techniques to control an actuator based on a sensor reading. A common approach is to use a PID controller. This will be the topic of a future tutorial.
🪛 Recommended Components
Building alternative game controllers intimidating. If you are new to Electronics, Makey Makey and BBC micro:bit are perfect for beginners!
Leave a Reply