Raspberry Pi - Heating Element
This tutorial instructs you how to use Raspberry Pi to control a heating element. Additionally, we will use this knowledge to create a heating system in a subsequent tutorial.
You can drive the heating element through a relay or through a MOSFET module. The relay gives you two states only: heating ON or heating OFF. The MOSFET module gives you those two states as well, and it also lets you set the heat level, from a little warm to full power. It switches silently, and it does not wear out when it switches very often. This tutorial shows you both methods.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Heating Element
The Heating Element Pinout

The heating element typically has two pins:
- The Positive (+) pin (red) should be connected to the 12V of a DC power supply
- The Negative (-) pin (black) should be connected to the GND of a DC power supply
How to Control Heating Element using Raspberry Pi
If a 12V heating element is supplied with 12V power, it will produce heat. To manage the heating element, a relay must be used between the Raspberry Pi and the heating element. The Raspberry Pi can then control the heating element through the relay.
If you are unfamiliar with relays (pinout, functioning, programming, etc.), you can find out more in the Raspberry Pi - Relay tutorial.
For the pinout and the inside of the MOSFET module, read the Raspberry Pi - MOSFET Module tutorial first.
Relay vs MOSFET Module for a Heating Element
| Relay | MOSFET Module | |
|---|---|---|
| Heat control | full heat or no heat | full heat, no heat, or any level between (PWM) |
| Noise | clicks when it switches | completely quiet |
| Wear | the contacts wear out, faster if you switch often | nothing moves, so switching often is no problem |
| Speed | slow, milliseconds | very fast |
| Heater type | AC or DC heater | DC heater only |
| Power supply | any voltage within the relay rating | about 6V or higher |
| Isolation | yes, coil and contacts are separate | yes, through a PC817 optocoupler |
| Use it for | AC heaters and simple ON/OFF heating | 12V DC heaters, heat level control, a thermostat that switches many times |
A heating system often switches the heater ON and OFF again and again to keep a temperature. For a 12V DC heating element, the MOSFET module handles this better, because it never wears out and it can also run the heater at part power. For a mains (AC) heater, a relay is the only choice of the two.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code
The code below will turn the heating element ON every five seconds and OFF every five seconds.
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file heating_element.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the temperature of the heating element.
WARNING
Take caution. It can cause harm to you and your residence. This is a serious matter, and we want you to be secure. If you are not absolutely certain of what you are doing, do yourself a favor and don't meddle with anything. Seek help from someone who is knowledgeable! We are not accountable for your safety.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
Raspberry Pi Code - Control Heating Element with MOSFET Module
In this part, the MOSFET module takes the place of the relay. With PWM, the Raspberry Pi decides how much power goes to the heating element, so it can heat a little or a lot.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

| From | To |
|---|---|
| Raspberry Pi GPIO16 (pin 36) | MOSFET module PWM pin |
| Raspberry Pi GND (pin 34) | MOSFET module GND pin |
| 12V adapter positive | MOSFET module + terminal |
| Heating element red wire (+) | MOSFET module + terminal (shared with the adapter) |
| Heating element black wire (−) | MOSFET module LOAD terminal |
| 12V adapter negative | MOSFET module − terminal |
A heating element has no coil, so you do not need a flyback diode here.
Detailed Instructions
- Set up the Raspberry Pi, the SSH connection and the RPi.GPIO library the same way as in the relay Quick Steps above.
- Connect everything as shown in the wiring diagram, but keep the 12V adapter unplugged for now.
- Create a Python script file MosfetModule.py and add the following code:
- Plug in the 12V adapter and start the script:
- The script gives the heater full power for 2 seconds and no power for 2 seconds. After that, the power goes up from 10% to 100% and back down to 0%. A heater warms up and cools down slowly, so you will not feel each step. Leave it running for a while and you will notice it is warmer at high power than at low power.
- The terminal shows the power level at each step:
Tip: the MOSFET module switches DC loads only, and it needs a load supply of about 6V or more. If your heater runs on AC, use the relay method.