PI controller with controlvalve

Working on a project here, so hope somebody can help me:)

I've got a 10 Liters tank, using a HC-SR05 ultrasonic sensor to measure level, and a controller valve that operates with with 0-10vDC. The valve is placed at the outlet of the tank. The inlet is variable.

The way I calculated the liter measurement:
Y = -0,392157x + 11,1765
X = centimeter value
Y = litervalue

11,17 is what the tank can hold maximum, but then the ultrasonic would get wet. So i'm using 10 as max.

This is what my program looks like:

// Define cho and trig pin
const int echoPin = 2;
const int trigPin = 3;

//define variables
long duration; // duration of soundwave traveltime
float distance;

//Scaling CM to liters
float liter; //also works as a current value

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);


void loop() {

digitalWrite(trigPin, LOW);

digitalWrite(trigPin, HIGH);

digitalWrite(trigPin, LOW);

//Reads echoPin, and returns the soundwaves traveltime in microseconds
duration = pulseIn(echoPin, HIGH);

//calculation of distance
distance = duration * 0.034 / 2;

//Converting CM to liters. 2.65 cm = 1 liter.
liter = -0.3921 * distance + 11.17;

Serial.print("Distanse: ");
Serial.print( "Liter: ");


My question is, how to i put this together with an PI regulator on my arduino uno?
My controlvalve is operating on 0-10vDC, 0=closed, 10= fully open.
I have a pwm to voltage converter, so that is taken care of.


PI control seems like overkill. What are you trying to achieve?

Why is it overkill? I'm trying to control the level inside the tank.

PID is nice if you need fine control but there's extra effort tuning the coefficients.

Unless you need the level to be precisely held, I would incline to trying if level rising, open valve more; if falling, close valve a bit.