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

1×Raspberry Pi 5
1×Relay
1×Alternatively, MOSFET Module (AOD4184, LR7843 or FR120N)
1×Heating Element
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Heating Element

The Heating Element Pinout

Raspberry Pi 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

The wiring diagram between Raspberry Pi and heating element

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:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file heating_element.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-heating-element """ import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin controls the heating element via the relay module RELAY_PIN = 12 # Set the relay pin as an output pin GPIO.setup(RELAY_PIN, GPIO.OUT) try: # Run the loop function indefinitely while True: # Turn the relay ON (HIGH) to turn on the heating element GPIO.output(RELAY_PIN, GPIO.HIGH) time.sleep(5) # Wait for 5 seconds # Turn the relay OFF (LOW) to turn off the heating element GPIO.output(RELAY_PIN, GPIO.LOW) time.sleep(5) # Wait for 5 seconds except KeyboardInterrupt: # If the user presses Ctrl+C, clean up the GPIO configuration GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 heating_element.py
  • 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

The wiring diagram between Raspberry Pi and heating element MOSFET module

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:

Raspberry Pi Screw Terminal Block Shield
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:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-heating-element """ import RPi.GPIO as GPIO import time MOSFET_PIN = 16 # GPIO16 (pin 36) connected to the PWM pin of the MOSFET module GPIO.setmode(GPIO.BCM) GPIO.setup(MOSFET_PIN, GPIO.OUT) pwm = GPIO.PWM(MOSFET_PIN, 500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.start(0) # start with the load OFF def set_load(percent): pwm.ChangeDutyCycle(percent) print("Load power: {}%".format(percent)) try: while True: # turn the load fully ON, then fully OFF print("Load: ON") set_load(100) time.sleep(2) print("Load: OFF") set_load(0) time.sleep(2) # increase the power step by step (PWM) for percent in range(10, 101, 10): set_load(percent) time.sleep(0.5) # decrease the power step by step (PWM) for percent in range(90, -1, -10): set_load(percent) time.sleep(0.5) time.sleep(1) except KeyboardInterrupt: pass finally: pwm.stop() GPIO.cleanup()
  • Plug in the 12V adapter and start the script:
python3 MosfetModule.py
  • 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:
PuTTY - Raspberry Pi
Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%

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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!