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.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Heating Element
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

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.

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.


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!

Video Tutorial