Raspberry Pi Pico - Control Heating Element

This guide will show you how to control a heating element with the Raspberry Pi Pico. In the next tutorial, you will learn to build a heating system.

There are two ways to switch the heater from the Pico: a relay or a MOSFET module. The relay gives only two states, heating or not heating. The MOSFET module gives those two states and also lets you choose how much heat the element makes, using PWM. It is silent, and switching it often does not wear it out. We will go through both ways here.

Raspberry Pi Pico heating element

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
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×Breadboard
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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

Pinout

Raspberry Pi Pico Heating Element Pinout

A heating element usually has two pins.

  • Connect the positive (+) pin (red) to the 12V DC power supply.
  • Connect the negative (-) pin (black) to the ground (GND) of the DC power supply.

How to Control Heating Element

When a 12V heating part is linked to a 12V power source, it generates heat. We utilize a relay to join the Raspberry Pi Pico and the Heating Element. The Raspberry Pi Pico can control the Heating Element by using the relay. If you're new to relay or MicroPython programming for the Raspberry Pi Pico, I recommend checking out the below tutorial:

For the MOSFET way, it helps to read the Raspberry Pi Pico - MOSFET Module tutorial first.

Relay or MOSFET Module for a Heating Element?

Relay MOSFET Module
Heat control full heat or no heat no heat, full heat, or any level between (PWM)
Sound clicks on every switch quiet
Speed of switching slow (milliseconds) very fast
Wear contacts wear out, more so with frequent switching no moving parts to wear
Power type works with AC or DC DC only
Heater supply any voltage the relay is rated for at least about 6V
Isolation from the Pico yes, between coil and contacts yes, through a PC817 optocoupler
Use it for AC heaters and simple on/off heating 12V DC heaters, heat level control, keeping a set temperature

If you plan to keep a temperature steady, the heater will switch on and off many times. The MOSFET module handles this well and can also lower the power. A relay is still the right part for a heater that runs on AC mains.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Heating Element

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

This code switches the heating element ON for five seconds and then turns it OFF for five also, doing this continuously.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-controls-heating-element """ from machine import Pin import time RELAY_PIN = 1 # The Raspberry Pi Pico pin GP1 connected to the Heating Element via relay pin # Initialize the relay pin as an output relay = Pin(RELAY_PIN, Pin.OUT) # Infinite loop while True: relay.value(1) # Turn on Heating Element for 5 seconds time.sleep(5) relay.value(0) # Turn off Heating Element for 5 seconds time.sleep(5)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the heating element according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check the temperature of the heating element.

WARNING

Please be cautious as it can lead to burns or fire at home. It's important for your safety. If you are unsure, do not try to handle it yourself. Ask someone experienced for help. We are not liable for your safety.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Code Explanation

The explanation can be found in the comments section above this Raspberry Pi Pico code.

How to Control Heating Element with MOSFET Module

In this part, a MOSFET module replaces the relay. With PWM on GP18, the Pico sets the heater power from 0% to 100%.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico heating element MOSFET module

This image is created using Fritzing. Click to enlarge image

From To
Pico GP18 module PWM pin
Pico GND module GND pin
12V adapter + module + terminal
Heating element + (red) module + terminal (shared with the adapter +)
Heating element − (black) module LOAD terminal
12V adapter − module − terminal

A heating element has no coil, so no flyback diode is needed.

Raspberry Pi Pico Code - Heat Level

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-controls-heating-element """ from machine import Pin, PWM import time MOSFET_PIN = 18 # Pico GP18 connected to the PWM pin of the MOSFET module pwm = PWM(Pin(MOSFET_PIN), freq=500) # 500 Hz PWM, slow enough for the module's optocoupler pwm.duty_u16(0) # start with the load OFF def set_load(percent): pwm.duty_u16(percent * 65535 // 100) # convert 0-100% to 0-65535 print("Load power: {}%".format(percent)) 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)

The script turns the heater fully on for 2 seconds and off for 2 seconds. After that, it raises the power from 10% to 100% and brings it back down. A heater warms up and cools down slowly, so you will not feel each step. Let it run for a while and you will notice it gets warmer at high power.

Detailed Instructions

  • Prepare Thonny and the Pico in the same way as in the relay Quick Steps above.
  • Connect the heating element and the MOSFET module by following the diagram. Leave the 12V adapter unplugged.
  • Paste the code into Thonny and save it on the Pico as main.py.
  • Plug in the 12V adapter, then click the green Run button.
  • Check the power value in the Shell of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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%
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

WARNING

The heating element gets hot and can burn you. Do not touch it while it runs, and keep it away from anything that can catch fire.

Tip: use the MOSFET module only with a DC heater and a supply of about 6V or more. For an AC heater, keep the relay.

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!