PID regulation / control

Hello everyone

is there exist any library for nerves/elixir/erlang which solve a PID regulation / control?

I have following simplified schema (see picture below) where RPi is Raspberry Pi, RH-1 is humidity sensor and Bubler (which dilutes gas, in this case the gas is air).


Now I would like to regulate (control) by RPi the humidity of air in the system, e.g. the air humidity within 1h would be 50% after that 30min would be 30% etc.

best regards


PID is very simple to implement. Make a genserver and use a periodic timer…

if dt is the sampletime you get something like:

{prev_x, integral, setpoint} = state
deriv = (x - prev_x) / dt
error = setpoint - x
new_integral = integral + dt * error
set_output(new_integral * ki + error * kp + deriv * kd)
new_state = {x, new_integral, setpoint}

The values for ki, kd, kp you will need to tune for yourself, kd will maybe be zero for your application, maybe also ki.

There are many ways to formulate the PID controller, this one is quite simple but not the easiest for analysis, where you rather use td and ti for integral and derivative time, expressed in seconds rather than gains. But this is for more advanced PID control.

1 Like

As far as how to interface your raspberry pi with the humidity sensor and valve, that will depend on what hardware you want to use, but shouldn’t be too difficult. I’ve seen several people use DHT22-style 1-wire temperature and humidity sensors in their Nerves-based projects.

Nerves makes it pretty easy to interface with 1-wire devices like this. There’s a blog from quite some time ago that goes into the details: Once you have the 1w hardware interface enabled, you just read from some special files using the standard Elixir filesystem modules. Feel free to ask questions if you try something in there and it no longer works - we have changed a lot of things in the past few years.

For the ball valve, I’m not familiar with how you would interface with those, but my guess is that you’d need to drive a relay or something that would switch power from a power supply. That would be pretty easy to do using the Elixir Circuits GPIO library and some kind of relay break-out board.

1 Like

Thanks a lot. It looks good, the idea is very simple. If I’ll have a time I’ll post here a feedback of progress. I’ll see, maybe it would be better write a hex package…

Anyway I must now find my lecture notes about PID controls from college (or find some on internet) and go to study of that at least for beginning… :slight_smile:

1 Like

Also I thank you for your reply and links.

This is a part of more complex device (see my query here, the schema is now obsolete). We want to use RPi 3 model B and Arduino (as ADC converter of voltage because it gives (around 0 - 1 V)) much better results than 12-Channel I2C 0-10V). For example, we bought for measuring of temperature these thermocouples: type K Ø1,0 NL50 and for humidity DKRF417 Pressure Resistant Humidity Probe.

Certainly we’ll use or inspire from your posted blog where you use DS18B20 sensor :slight_smile:

About the ball valve, I’m looking for something like this for 24 VDC, L port.