I have a Raspberry Pi 4B, a 12 V power supply, a 12 V air pump controlled by a MOSFET, a DS18B20 for feedback and a 47 Ω, 8 W resistor that we should somehow heat. Everything up to actually heating the resistor is done and working.

Essentially, the pump pumps air through a PVC channel inside which is the resistor that should heat up the air to whatever temperature is set in the code, and the reading is done by the DS18B20.

How can I do it on the Pi? I tried searching and it has something to do with a transistor and one of the PWM pins of the Pi, but I couldn't find any schematic.

I did find something for an Arduino, but I'm not sure if it will work exactly the same, since transistors consume current and the GPIOs are heavily limited on that side.

Do you guys know some transistors that would work with this and/or a library with a schematic example? I'm new to PID controlling and transistors.

I tried posting this question on the Raspberry Pi section of the forum and I was told to come here.

  • \$\begingroup\$ Have a look into Matlab. It can generate code for python. Or use pypi.org/project/simple-pid in combination with electronicwings.com/raspberry-pi/… . Schematic is simple: Connect 12V->Resistor->FET->GND, then connect any GPIO to the FET-Gate. Control this gate as "GPIO" with PWM. Or use something like: amazon.de/… . Cant beat the price and it is ready to use, save and known to work \$\endgroup\$ Apr 6 at 17:18
  • \$\begingroup\$ On/off control might be sufficient here. I'm a bit flummoxed by your writing: "heating the MOSFET". Is the 8 W resistor there to heat up the MOSFET? I thought you were saying "the resistor that should heat up the air". That juxtaposition is giving my brain some trouble, for now. \$\endgroup\$ Apr 6 at 19:00
  • \$\begingroup\$ @periblepsis my bad, I meant the resisto, not the mosfet. I changed the question now. Do you think just on/off controlling may work here? \$\endgroup\$
    – Mito
    Apr 6 at 19:09
  • 1
    \$\begingroup\$ @Mito I can't think one way or the other. On/off control has its benefits and it is most often used in situations where air is being heated by a resistor (electric heater controlled by a thermostat.) PID has certain reasons to exist and it can perform better with proper design and placement of the temperature sensor and especially if the sensor is a fast one that responds directly and quickly to changes. But in air, this is unlikely to exist. So on/off is probably "good enough" unless you provide some of your reasons about what you want to achieve that requires more. \$\endgroup\$ Apr 6 at 19:13

1 Answer 1


How can I do it on the Pi?

You need some way to read the temperature and then change a PWM or analog channel to control the heater, this also needs to be done several times a second. I would suggest the control loop run at least 5Hz.

Running this code on a Pi is going to be hard, because control loops need to be run in a deterministic way. This can be difficult on the pi because of the operating system, which could make it hard to run the control loop at exactly 5Hz, if the processes aren't setup correctly then their could be error because of uneven sampling.

I would probably use an arudino and find some PID code, port the temperature in and have the . Here is the equations, they aren't that hard to put in code. The only thing you need after that is a timer to start the function (every X number of milliseconds) to read the ADC's, run the PID code then port the output to the PWM or DAC and update them with the new value from the PIC.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.