Pressure regulation with PWM controlled proportional valves and I2C sensor

Hi, I am working on pressure regulation project with an Arduino Leonardo. The idea is to have a tank maintained at a constant pressure (small vacuum in this case ~-20 mbar). To achieve this, the tank is connected to a pressure source and a vacuum source. Both sources are controlled by proportional valves. These valves are controlled with PWM signals. Moreover, the vacuum inside the tank is measured with a vacuum sensor that communicates by I2C with the Arduino.

To avoid noises, I set the timer 4 manually so it can provide a PWM signal at ~15kHz. Using registers OCR4A and OCR4D I can control both valves with the pins 5 and 6. I also use a PID from the livrary PID_v1.

The idea now is to have an interrupt every every 10ms (generated with the timer 0) during which I read the latest sensor value and then I compute the valves’ command with the PID library.

Unfortunately, I am encountering issues with my PWM that gets stuck with a 0% duty cycle when I execute both tasks : sensor reading + PID computing. In the actual configuration, I don’t even process the PID’s output value, so the PWM’s duty cycle should not vary.

I have tried to put some time margin between sensor reading and PID computation and also to lower the frequency down to 1Hz but I still have the same issue…

Please find my main sketch below.
Would you have any advice for me? What could cause this PWM interruption?

Best regards,
Loic

MeniscusPressureRegulator.ino (6.77 KB)

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: