1
\$\begingroup\$

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 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 cycle the ppwer so many times (sample time 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)
 
JRE
75k10 gold badges115 silver badges197 bronze badges
asked Apr 16, 2022 at 12:11
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Sleeping for a duration proportional to the output isn't the best way. The controller is "blind" while sleeping and you may overshoot.

To achieve what you want, simply sleep for 2 seconds after every measure / control cycle. Turn on the heater if the output is greater than 0, turn it off otherwise.

If you require a particularly high regulation accuracy, I recommend reading up more on how to achieve that, it is not a trivial problem but it has been well-researched.

answered Apr 16, 2022 at 12:57
\$\endgroup\$
3
  • \$\begingroup\$ You are right, sleeping for the time period of the output value isn't a good idea. How do you suggest to use the PID output as input for my actuator? I can use the output as time-period to power cycle the heater or as dutycycle for a PWM. \$\endgroup\$ Commented Apr 16, 2022 at 13:04
  • \$\begingroup\$ @MarcoBobinger the simplest is just a binary on/off based on the sign of the output. \$\endgroup\$ Commented Apr 16, 2022 at 13:28
  • \$\begingroup\$ If that's not sufficient, you can indeed PWM the heater, but that goes against your goal to minimise switching. \$\endgroup\$ Commented Apr 16, 2022 at 13:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.