Can I control a heating element through microcontroller

I want to be able to use a microcontroller to variably control the temperature of a heating element. I want to use a fan to blow air over the heating element to blow air at a desired temperature. I will use a temperature sensor to be able to read the temperature of the air and have the microcontroller use that information to provide more or less heat from the heating element to keep the air temperature consistent. My question is whether or not I can use the microcontroller to be able to read the temperature from the temperature sensor and supply variable voltage to the heating element based on the temperature reading. Since the microcontroller only supplies a voltage output of 5V is it possible to use this to supply a heating element that would need up to 120 V? I am aware that I can use a relay to be able to change to wall outlet 120 V AC to break it down to 5 V DC to power the microcontroller but am I able to reverse that process out of the microcontroller? How would I connect the heating element to the microcontroller so that the microcontroller is variably changing the amount of heat being produced from the heating element. Here is the equipment I am using:

Microcontroller: Arduino Mega 2560 R3 https://www.sparkfun.com/products/11061
Heating Element: Soldering Iron - 30W Replacement Heating Element Soldering Iron - 30W Replacement Heating Element - TOL-09509 - SparkFun Electronics
Temperature sensor: Temperature Sensor - TMP36 https://www.sparkfun.com/products/10988
Relay: SparkFun Beefcake Relay Control Kit SparkFun Beefcake Relay Control Kit (Ver. 2.0) - KIT-13815 - SparkFun Electronics

Any help would be greatly appreciated. I am not 100% on how relays work so any information to better describe how that works would be greatly appreciated as well.

Hi I am relatively new to arduino programming and so I am having trouble getting from the beginner tutorials on how to have the LCD blink and lights blink to writing the code to perform the function I am going to describe. I have begun working on a new project which will make a devise that will blow hot air into an bag. I want to connect buttons and an LCD screen to a microcontroller to allow the user to set a desired temperature for the hot air on the LCD screen. Once the temperature is set, the user will hit a start button which will heat up a heating element to the desired temperature and a fan will turn on to blow air over the heating element to create hot air which will blow into an enclosure (a bag). I want to use a temperature sensor to relay the temperature of the hot air to be able to keep the temperature of the hot air somewhat consistent. The enclosure will also have a pressure sensor attached so that once a certain pressure is reached in the bag, the fan and the heating element will turn off and a small piezo speaker will beep. The complicated part of the programming is keeping the temperature consistent. I know that once the fan starts blowing air over the heating element, the heating element will cool down. I also know that the air temperature will not perfectly match the temperature of the heating element. I am having trouble getting started with writing this code. I have been trying to take different parts of the code from other peoples projects but I am having a hard time figuring out how to combine the code and to understand which parts of their code to take. Any help to be able to write the program I am looking for would be greatly appreciated. Here is the list of the equipment I am using for this:

Microcontroller: Arduino Mega 2560 R3 https://www.sparkfun.com/products/11061
LCD Display: Basic 16x2 Character LCD - White on Black 3.3V Basic 16x2 Character LCD - White on Black 3.3V - LCD-09052 - SparkFun Electronics
Pressure sensor: SparkFun Barometric Pressure Sensor Breakout - MPL115A1
Motor to attach fan blades to: Hobby Motor - Gear ROB-11696 ROHS Hobby Motor - Gear - ROB-11696 - SparkFun Electronics
Thermocouple: Thermocouple Type-K Glass Braid Insulated Order Tracking - SparkFun Electronics
Relay: SparkFun Beefcake Relay Control Kit SparkFun Beefcake Relay Control Kit (Ver. 2.0) - KIT-13815 - SparkFun Electronics
Heating Element: Soldering Iron - 30W Replacement Heating Element Soldering Iron - 30W Replacement Heating Element - TOL-09509 - SparkFun Electronics
Buttons: Tactile Button Assortment Tactile Button Assortment - COM-10302 - SparkFun Electronics
Piezo Speaker: Piezo Speaker - PC Mount 12mm 2.048kHz Mini Speaker - PC Mount 12mm 2.048kHz - COM-07950 - SparkFun Electronics

Relay is very simple. It's basically a pushbutton, only an electromagnet does the pushing instead of your finger.

There are two pins that go to the coil. When you apply the specified coil voltage (usually something low) to the coil pins, they actuate the relay.
The other pins will be marked Com, NO, and NC (some relays only have NC or NO, others have multiple sets of pins) - when relay coil is off, Com is connected to NC ("normally closed"), when it's on, it's connected instead to NO ("normally open"). If controlling the relay without a shield or module, you need a diode across the coil with the band towards positive side, and (unless it requires 20mA or less) a transistor to drive it - if using a shield or module, these will be on the board.

Note that a relay only lets you turn the element on or off though, not a variable voltage, and you can't PWM it to get the same effect as a variable voltage.

If you actually need to dim it, you need to use a triac, which requires a few more auxiliary components, and generally more complexity; dimming AC is annoying.

For such a simple project (unless there's more to it), a simple Arduino Uno (or $3 nano/pro mini clone from ebay) is more than sufficient for the microcontroller - it's not like you need lots of pins for this.

theres lots of free stuff on the net from simple follow other peoples code and reverse engineer how it works

http://wiki.epalsite.com/index.php?title=Starter_Kit_for_Arduino

to complete free pdf manuals that will show you arduino for beginners

(carnt link it as im not sure if it was really designed to be free but a quick search for arduino for beginners should find it)

can we code bash you something that should work........yep

would you understand how to correct anything that we have coded wrong.........nope

could we code it with no mistakes..........not unless we built it to test the code

You have a plan. It looks like it should work. When you have some code started post it and some one will point you in the right direction

Typically, you simply turn the heating element on & off... Turn it on when you're below your target temp and turn in off when you're above the target. Your oven, your furnace, air conditioning, and your refrigerator all work that way... There's enough delay in the temperature change that you don't need to "dim" the power.

There is typically some hysteresis or "swing".... The heat stays on 'till you are 1 degree (or 1/10th of a degree, etc.) above the target, then it doesn't turn on again 'till you are 1 degree below the target. If you are using a mechanical relay, that will keep your relay from "chattering". With a solid state relay, you may not need much (if any) hysteresis.

Hi,
What temperature are you trying to keep, and how much air flow, a soldering iron element is designed to heat a central core that becomes the iron tip.
You are trying to pass heat energy into air.
How constant do you want the temperature, you may need PID and a SSR for proper control of the air temperature.

Tom.... :slight_smile:
PS don't double post.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Can you application tolerate the hysteresis as described by the typical oven temperature controller? If so, then go for it. You'd set the off temperature just above target and the on just below. The closer you set these to target, the more often you'll turn on and off your relay. Remember, relays have a limited number cycles, and that number gets small with larger current.

So look into a triac or solid state relay and opto-isolate the trigger to protect your micro controller.

If you application requires low hysteresis, you'll definitely have to have a solid state device. I'd just use a very, very low frequency pulse width modulated type function to turn on and off your solid state device. Experiment with the frequency and pulse width to regulate your temperature. (Ultimately, it will be your PWF that will determine your output temperature.) In a stable environment, let's say a still, you'd be able to correlate PWF to temperature. Once your volatile of temperature C has boiled off, you can switch your valve, then up your temperature (increase PW*F) to the next volatile to be collected.

If the environment is changing a lot, your talking about a serious control system and that is beyond the scope of this discussion.

winterwonderwhy:
Any help would be greatly appreciated. I am not 100% on how relays work so any information to better describe how that works would be greatly appreciated as well.

a coil is the heart of a relay and a solenoid and a motor.
when you put voltage into a coil, it creates a magnetic field.
for the motor, it helps spin the shaft.
for a relay or solenid, it just attracts a metal objectt and moves it.
the movement allows something else to happen.
for a solenoid, it opens a valve
for a relay, it makes wires move to complete a circuit.
a transistor allows you to put a tiny bit of power on one wire and that will allow a huge 'flow' through the transistor.
the relay/solenoid is used to separate two things. on the coil side you have on thing, on the other side you have something else. the magnetic field separates these so they never touch.

in your case the coil of the relay is low voltage. the wires on the other side of the relay can be pretty much anything. they make relays for 10,000 volts that you can operate from your Arduino. because there is total isolation between the signal and the output.

for the relays we use, there is typically another layer of isolation. the Arduino does not power the coil directly. it uses a transistor like a switch. the Arduino controls the transistor, the transistor controls the actual power of the relays.

you can use a 5 volt or 12 volt DC (must be DC) relay with a transistor. (for AC you have to use a different device)

an opto-isolator is nothing more than a pair of matched LED's
the Arduino turns on one LED, the other LED sends power to a transistor and that allows light to isolate the two circuits.
hence the name optical isolator.

most of the relay boards you buy for an Arduino have an on-board optio-isolator. since this creates a separation from the pwoer of the Ardino to the rest of the board, you have to provide a SEPARATE power supply for the COIL of the relays. remember the coil is separate from the output contacts. there are actually 2 places of isolation.
some relays are 5 volts, some are 12 volts. since you are using the LED, the coil voltage is not too important.
the only real decision about coil voltage is what you have from your power supply. if you are in a car, use the 12 volts that is there already.

From reading this I agree that I want to go with something similar to an oven where it will not need to dim. The program will turn on wants it drops below a certain temperature and off once it goes above a certain temperature. Thanks everyone for helping me to answer that question.

I have started going through the examples for the arduino code. I am having trouble going from those to being able to write the code to perform the task I want. I am able to get text to print on the LCD display and temerpatures to be measured by the temperature sensor and displayed on the LCD display however I am not sure how to write the code so that I can input a tempeature to have the heating element turn on/off to be able to maintain a temperature.

Earlier gpop1 said:
"can we code bash you something that should work........yep"

Please that would be amazing if I could be code bashed. That would give me a great place to start and I will be able to figure out how to modify the code. I just need a starting point code that pertains more to my intended task. The closest code I have been able to find is the DIY thermostats however those dont control a heating element that is being powered through the arduino. That is where I am really stuck is how to get the arduino to be able to control a heating element that uses a much higher voltage than the arduino output voltage.

Thank you to everyone that has responded thus far. The help is greatly appreciated.

Here are the code for you to start with

#include <LiquidCrystal.h>
int reading = 0;
int sensorPin = A0;
int relay =7;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

  pinMode(relay,OUTPUT);
}
 
void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  reading = analogRead(sensorPin);
  	float volts = reading/204.8;       // scale for 1024/5 
	float celsius= 100.0 * volts - 50; //converting from 10 mv per degree wit 500 mV offset
                                           //to degrees ((voltage - 500mV) times 100)

  lcd.setCursor(0, 0);
  lcd.print("Temperature: ");

  lcd.setCursor(0,1);
  lcd.print(celsius);
  lcd.print((char)223);
  lcd.print("C");
  if (celcius <35.0)
  {
    digitalWrite(7,HIGH);
  }
  else if (celcius >36.0)
  
  {
	  digitalWrite(7,LOW);
  }
  delay(500);
  lcd.clear();
}

But it would be good to be able to set the temperature with a potentiometer. The LCD could display the temp it is set to.

Have the display show the target temperature AND the current temperature.

The potentiometer is connected on leg to gnd, one leg to Vref (or Vcc) and the wiper to an analog input. Use the exact same algorithm to display the target temperature.

Here's the cool thing, when you do the analogRead of the temperature sensor, compare that raw value to the raw value from the potentiometer.

digitalWrite(Relay, (analogRead(Temperature) < analogRead(Potentiometer) );

(analogRead(Temperature) < analogRead(Potentiometer) will be either true or false (HIGH or LOW)

I have had some problems with the compiler compiling code like this, so maybe it is better to put it into several distinct steps.

:slight_smile:

You're having fun!

OSD

Thank you BillHo for the code to start with. This enables me to read the temperature on the LCD screen and turns the heating element based on the temperature being below 36 degree Celsius. I want to be able to enter the desired temperature into the LCD screen instead of having the temperature set on the code to 36 degree. How would I go about writing that code?

Try a variable such as setDegrees

  if (celcius < (setDegrees - 1))[color=#222222][/color]
  {[color=#222222][/color]
    digitalWrite(7,HIGH);[color=#222222][/color]
  }[color=#222222][/color]
  else if (celcius > (setDegrees + 1))[color=#222222][/color]
  [color=#222222][/color]
  {[color=#222222][/color]
	  digitalWrite(7,LOW);[color=#222222][/color]
  }

And to set the temperature:

setDegrees = analogRead(adjustPin);//You will need a analog pin.
setDegrees = map(setDegrees, 0, 1023,0,100);//The temperature it is set to

And then display setDegrees on the LCD.

I found a ebook on Safari Books Online, 30 Arduino Projects for the Evil Genius: Second Edition. It has the exact project you are working on! I attached the page that the code is on. It is a office word file.

Book.zip (975 KB)