Real World code issues

I am having an issue with my breadboard setup not responding the same as the simulator for a small part of the code.
Below is a link to the simulator.
For the moment I will just refer to the sequence of operation for the part that is not working. I have also mentioned sensors for test purposes I am using potentiometers.

Operation works as follows-
If the level sensor reads <90% full scale and the pressure sensor reads <50% full scale the push button can be used to energize the relay. The tank will begin to fill and the level sensor will increase once it reaches 90% the relay will turn off, if at any time the pressure sensor is =>50% the relay will be de-energized.

As you can see in the below simulator it all works as intended. When I physically test the exact same setup on the bench the setting for the pressure cut off and the level seem to be acting against one another or together. For example the relay is dropping out at when the level is ~2.4v (490) and the pressure is 0v if i change the level value is seems to have little to no effect, if I change the pressure value this seems to then allow for a different cut out point. but it gets to a point where the values overlap. I know this is not the best explanation but it is also very hard to describe. at first I thought it may be a problem with the board, so i swapped to another but it did exactly the same thing. I am leaning towards an issue with my IF, ELSE blocks and how I have set them up. Any suggestions?

#include <TM1637.h>

// Initialize the display module
const int CLK = 8;
const int DIO = 9;
TM1637 tm(CLK, DIO);

const int inPin1 = A0;  // Analog input from level sensor
const int inPin2 = A1;  // Analog input from Override POT
const int relayPin = 4; // Output controlling relay
const int overPPin = A2; // Analog input from pressure sensor
const int overridePin = 5; //Digital input for override switch 
const int fillPin = 2; //Pushbutton for fill cmd
const int outPin = 10; //0-5v pwm output to external device.
int overrideState = 0; //override state on start
//int static_variable = 500;
//int onVal = 100; //Relay on @ .7v (0-1023)
int offVal = 920; //Relay off @ (0-1023)
int overPTH = 510; // Overpressure cutoff (0-1023)
int actualValadv;

void setup() {
  // Set up analog input pins
  pinMode(inPin1, INPUT);
  pinMode(fillPin, INPUT_PULLUP);
  pinMode(relayPin, OUTPUT);
  pinMode(overridePin, INPUT_PULLUP);
  pinMode(outPin, OUTPUT); // Set up digital output pin for 0-5V DC output
  //analogReference(EXTERNAL);
  Serial.begin(9600);
  tm.set(3);// Set the brightness of the display (0 to 7)
  
}

void loop() {

  // Check if manual override switch is on
  overrideState = digitalRead(overridePin); // check the state of the override switch
  if (overrideState == LOW) { // if switch is on 
    int overrideVal = analogRead(inPin2);// Read the voltage from the override and output it directly
    float inPin2 = (overrideVal * 5) / 1023.0;
    analogWrite(outPin, (inPin2 / 5) * 255);

  } 
    
    else {
      // Output the DP reading as a voltage on the digital output pin using PWM
  int sensorValue = 0;  // Initialize sensor value
  float outputVoltage = 0.0;  // Initialize output voltage

  // Take 16 samples and calculate the average
  for (int i = 0; i < 16; i++) {
    sensorValue += analogRead(inPin1);
    delay(5);  // Delay between samples
  }
  sensorValue /= 16;

  // Convert sensor value to voltage (0-5V range)
  outputVoltage = (sensorValue * 5) / 1023.0;

  // Output voltage to PWM output pin 10
  analogWrite(outPin, outputVoltage * 255.0 / 5.0);

  // Print voltage to serial monitor
  //Serial.print("Input voltage = ");
  //Serial.print(sensorValue * 5 / 1023.0);
  //Serial.print("V, Output voltage = ");
  //Serial.print(outputVoltage);
  //Serial.println("V");

  //delay(100);  // Delay between readings
      
      int displayValue = (sensorValue);
      // Convert the display value to an array of digits
      tm.display(0, displayValue / 1000);
      tm.display(1, (displayValue / 100) % 10);
      tm.display(2,(displayValue / 10) % 10);
      tm.display(3,displayValue % 10);
     
     
     {int digitalVal = digitalRead(fillPin);
      //Avereges the level sensor reading
            float relayCtrl = 0.0;
      for (int i = 0; i < 16; i++) { // Take 16 readings and average them
      relayCtrl += analogRead(inPin1);
      delayMicroseconds(50); // Wait between readings to reduce noise
      }
      relayCtrl /= 16.0;
      
      if (digitalVal == LOW) {
      digitalWrite(relayPin, HIGH); // turn relay on
 
      } else if (relayCtrl >= offVal) { // if value is greater than or equal to threshold2
        digitalWrite(relayPin, LOW); // turn relay off
      }
        float pVal = 0.0;
      for (int i = 0; i < 16; i++) { // Take 16 readings and average them
      pVal += analogRead(overPPin);
      delayMicroseconds(50); // Wait between readings to reduce noise
      }
      pVal /= 16.0;
         if (pVal >= overPTH) //if pressure increases over threshold 
        digitalWrite(relayPin, LOW); //turn relay off
        }
  
    }

  

  
}

How do you power the relay and the pump?
Have you installed flyback diodes over the pump and relay coil?

1 Like

Seems the level sensor is reading the fluid level in a container, what is the pressure sensor reading ? Pressure where?

I don't think your approach to get average values for pressure and fill level
with for loops that contain delays is very productive.

Let loop() loop, do not use any delays.

Get a reading when the sample rate is reached, and process it to an average of the last 16 values.
Check the button(s).
Decide whether any action is necessary, and perform it.

In general:

  • Don't comment what a statement does, but why and only if necessary.
  • Don't insert superfluous braces or brackets, if not needed for clarity.
  • Give your variables good names, so you don't need comments explaining their usage.
  • Use the auto formatting feature of the IDE.
1 Like

there is nothing connected to the relay yet, it is set up on the bench exactly the same as it appears in the simulator except for the power supply.

I am new to this, the comments are more for my reference as well as making it clear to the person I am making this for (my farther) when I am explain the function to him. I understand that this may not be the accepted norm but for this project I kind of need that "plain language".
Thanks for the tip on removing the delay's ill give that a go.

the bench set up is exactly the same as the simulator, I have edited my original post to make this clearer. Once I have it all working correctly on the breadboards ill swap out the POT's with the sensors and put it all onto PCB's. There is a bunch of other stuff that will be needed Cap's across the sensor signals, a Lowpass filter on the PWM output ect. that stuff has been previously tested and all works as intended.

What does your project do?

Function 1
Uses a differential pressure sensor (level sensor) to measure the liquid level in a pressurized vessel, the sensor is very sensitive and the head of liquid is between 20mm and 350mm so i average the signal to get a stable reading. This value is then outputted as a 0-5v signal to an external device that displays the level.

Function 2
The output can be overridden and controlled by a POT for calibration purposes on the device that displays the level.

Function 3
A pump can be turned on to fill the vessel the pump will automatically turn off when its reaches a set level. As there is a danger the vessel can be over pressurized the pump will automatically turn off if the pressure sensor exceeds a set pressure again this pressure is less than 1PSI so i need to average the signal to stabilize it.

Thanks, good luck.

This does not answer any of my questions. You obviously don't understand the relevance of the mentioned items if it comes to real-life circuits. Simulation may verify code structure but never the hardware construction.

The VCC relay is fed from the 5v PS supplying the breadboard +bus. Yes there is a diode across the +&-. The pump will be supplied from a separate system this relay will actually drive a larger relay with higher rated contacts.

Your simulation apparently, does not include filling the container. With a sensitive pressure sensor, if you fill from the top, the sensor will see the impact force of the liquid as a pressure change.

Does this affect the issue I am currently facing?

Only after you do real-world testing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.