2

I would like to PID control the temperature of a water bath using a RaspberryPi4, a solid state relay (SSR) and a heater (200W, 230VAC) using python.

The IVMech library seems a good starting point. As well as Simple-PID

I went for IVMech since I saw it quoted in other projects. I can control my actuator by pulling my SSR low over one of the RPI-GPIO pins.

Currently, I'm stuck with reducing the test_pid.py test code to the bare minimum required. Please see my code sample below. Is it fine to just use the output of the PID calculation as the time to turn the heating element on?

In my case I don't want to powercycle so many times (sampletime around 1-2s) since my application should last > 1year and I'm not sure if the SSR that uses a photodiode to for galvanic isolation can be cycled indefinitely. I read that the SSR supports around 100MIO cycles. For a switching frequency of 1Hz that would correspond to a lifetime of around 3years.

import PID
import time
import numpy as np
from scipy.interpolate import BSpline, make_interp_spline #  Switched to BSpline
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
HEATER = 17
GPIO.setup(HEATER,GPIO.OUT)

P = 0.2
I = 0.0
D = 0.0
L = 0
sampletime = 2.0

def measure_Temp:
    temp = 20.0
    return temp

while True:
    pid = PID.PID(P, I, D)
    pid.SetPoint=25.0
    pid.setSampleTime(sampletime)
    END = L
    feedback = measure_Temp()
    pid.update(feedback)
    output = pid.output
    GPIO.output(HEATER, False)
    time.sleep(output)
    GPIO.output(HEATER, True)

1 Answer 1

2

You've asked an interesting question, but one that is probably off-topic for RPi SE. The "answer" that follows is more along the lines of food for thought; I won't try to review your code for a couple of reasons.

First thought is that because a Solid State Relay is your control element, and SSRs are binary devices (ON or OFF), it seems that you are operating a bang-bang controller instead of a Proportional Integral Derivative controller. The dynamics and signals are completely different than what PID controllers were developed to handle.

The formula for stability in a bang-bang controller is one simple thing: hysteresis. If your control element is an SSR, this is really the only tool in your toolbox! You cannot turn an SSR partly on, so what does the derivative term really mean - or the proportional term? Additionally, the RPi can certainly perform numerical calculations with floating point numbers galore - but it does not come equipped to deal with analog signals directly (tho' you can add a DAC/ADC if needed).

Putting on my positivity hat for a moment, I think that the RPi is much better equipped to serve as a bang-bang controller instead of a Proportional Integral Derivative controller. In that sense, the more logical approach to your problem is to set aside PID control for this particular project, and do some reading on bang-bang control. Other references are in plentiful supply.

4
  • Thank you at @Seamus I'll go for a bang-bang controller with the current setup. I could also use a DC-DC regulator for 230V-24V and then use PWM modulation on MOSFETS that are fine with a voltage range of 4-32V. Anyways, the bang-bang will do the job for my application I think. Commented Apr 18, 2022 at 8:18
  • Yeah - changing the controller would allow use of a PID solution. I don't know your application (other than a water heater), but that sounds like a lot of current to be switching which brings another set of issues. Have you considered Triacs or SCRs?
    – Seamus
    Commented Apr 18, 2022 at 8:28
  • To my knowledge the SSR that I'm using, the SSR-40DA, is based on a Triac. Commented Apr 18, 2022 at 8:58
  • @MarcoBobinger: I think you're correct. But for a heating application, I don't know why an SCR wouldn't work. But whatever you use, use it carefully - 230VAC is lethal!
    – Seamus
    Commented Apr 18, 2022 at 9:27

Your Answer

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

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