Seeking help with an Arduino Liquid Level Sensor project

I currently have the following code to run 8 liquid level sensors, with an LED and a Buzzer:

boolean running = 0; // when running=1, some condition is met; otherwise, another condition 

// Defined input and output pins 
int sensorPins[8] = {0, 2, 4, 6, 8, 10, 12, 18}; //These are the IO1 pins of the sensors and will hook into every second digital pin on the UNO
int outputPins[8] = {1, 3, 5, 7, 9, 11, 13, 19}; //These are the IO2 pins of the sensors

// Output Pins 
int ledPin = 14; // This is marked as A0 on the UNO 
int relayPin = 15; // This is marked as A1 on the UNO 

void setup() 
{ 
  Serial.begin(9600); // Initialize serial communication 

  // Set sensor pins as inputs and associated output pins 
  for (int i = 0; i < 8; i++) { 
    pinMode(sensorPins[i], INPUT); 
    pinMode(outputPins[i], OUTPUT); 
  } 
  
  // Set output pins for LED and relay 
  pinMode(ledPin, OUTPUT); 
  pinMode(relayPin, OUTPUT); 
} 

void loop() 
{ 
  boolean detected = false; 

  // Read each sensor 
  for (int i = 0; i < 8; i++) { 
    int sensorValue = digitalRead(sensorPins[i]); 
    Serial.print("Sensor "); 
    Serial.print(i + 1); 
    Serial.print(" reads: "); 
    Serial.println(sensorValue); 
    
    // If any sensor reads a 1, a liquid is detected 
    if (sensorValue == 1) { 
      digitalWrite(outputPins[i], HIGH); 
      detected = true; 
    } else { 
      digitalWrite(outputPins[i], LOW); 
    } 
  } 
  
  // If liquid is detected, turn on LED and relay 
  if (detected) { 
    digitalWrite(ledPin, HIGH); 
    digitalWrite(relayPin, HIGH); 
  } else { 
    digitalWrite(ledPin, LOW); 
    digitalWrite(relayPin, LOW); 
  } 
  
  delay(100); // Wait for 100 millseconds before the next loop iteration 
}

I’m looking for help to set up or design and do a diagram for me to put together an Arduino with the code and hardware with 8 x level detectors, 4 green LEDs, 4 red LEDs, 1 Common Buzzer and a kill switch for the buzzer with the LED remaining lit.

I need the alarm to sound and the LED to be on when the level drops or a bubble is detected.

1 Like

If you are looking for assistance with your design the best starting point is to post what you already have. From the code it is possible to deduce that D0 to D13 are used for the first 7 sensors and their LEDs, and that 18,19 (A4,A5) are used for the eighth. Then A0 and A1 are used for the buzzer and relay. Presumably the kill switch for the buzzer is a SPDT switch in line with A1. Note that D0 and D1 are not recommended for use as GPIO because they are connected to the on-board USB device, so I would suggest substituting 16,17 (A2,A3) for the first sensor. That may be one difference from the diagram you have - if there are other differences you could show them on the diagram. If the problem is that you can’t align the code with your particular development board you could post a picture of the board and someone may be able to interpret the pinout.

2 Likes

Hi Wayne,

Welcome to the forum!

You would need to hook up the sensors similar to the below photo, the print statements from the code can be omitted if you’re not going to have it plugged into a computer, if you are going to have it plugged into a computer, I would recommend moving the sensor on 0 and 1 to one of the available analog pins that aren’t being used. You would also need to ensure that all of the sensors are provided with 5V and ground in addition to the signal wires. (Note you would hook up the other 6 sensors the same as the two I have in the picture)

1 Like

Thanks Jeff
We will have to assume I have no hardware and looking for basically a shopping list and diagram of the Ideal way to achieve the project.
I am not sure of what relays or available LEDs and Buzzers that the arduino can drive
I have been playing with the sensors and was directed to this link (XKC-Y26-V Capacitive Contactless Liquid Level Sensor: Features, Interfacing with Arduino) for similar sensors where the controller was not used and wired direct to the arduino and as the sensor has a maximum of 12 volts I tried this method and 12 volts and I am afraid I have fried the hardware.
I hope you can help design the neatest best stable design as I have no experience with this stuff but have a need.
I dont have a big budget but would be happy to pay someone the time to help me.
Thanks for you interest

1 Like

Thanks Dan. If you can see my reply to Jeff and hopefully help me.
I can put together a system OKish from a diagram and hardware but thats about it.
Please help
W

1 Like

The code you posted assumes an Arduino Uno or Nano - is that what you are using or would a different MCU be possible? The UNO/Nano is suitable and has the advantage of 5V GPIO, which makes your choice for relays somewhat easier.

The code assumes that the level sensors are simple on/off sensors: that’s what the digital GPIO can handle. As you are using LEDs I assumed that’s what you intended - if that’s not so then your options change quite significantly.

I also assumed that the buzzer kill switch is a physical switch inserted in the buzzer driver line, and not a switch wired to the MCU.

If all that matches then you simply connect everything to the Uno/Nano according to the numbers in the code above.

Below are two options for Arduino. The relay is an example - there are a lot of other options.

Arduino Uno R3
Nano V3.0 Board
Non-contact Capacitive Liquid Level Sensor
5mm LEDs 100pcs Pack
600 Pack of 1/4 Watt 1% Resistors
Relay Module V2 (Arduino Compatible)
Duinotech Arduino Compatible Active Buzzer Module
Hook-Up Wire Pack

1 Like

Thanks so much Jeff
I am so sorry to say I am pretty green with all things arduino
Wiring I am a little better
I have nanos, unos and mega 2860 (or something like that)
esp32s
a raspberry pi.
I will need to check tomorrow to see what versions I have

I am happy with either the physical toggle or rocker switch to kill the buzzer or if its better a button that kills it in the mcu until it goes out of alarm and then resets. (this would be great if its easy.)
Dan wrote the code for me so that can be whatever you think is best
Thanks Again
Wayne

1 Like

The code is written for a UNO or Nano, so they would be best. A Mega2560 would also match the code, but is overkill for this job. The ESP32S would likely work OK, but it depends on the exact model and the dev board - there might not be enough GPIOs - and the code would need to be adjusted. The Pi would also work just fine, but is overkill and would also need some reworking of the code.

1 Like

Hey Jeff
What part of the world are you in

Are you able to help me set this up with a diagram please
I am not sure what to do with diodes and such.
Is it possible to have the code silence the buzzer and when the sensor goes out of alarm the kill or silence mode reverts back to buzzer ready for when it next goes in to alarm due to a level drop.

I am not sure if this is allowed on this site
This is a low budget commercial job that I really need help with and would gladly pay you for your time to work with me privately to get it right very quickly PLEASE!.

1 Like

The code is setting the pin HIGH if liquid is detected. I assume you want that to turn the LED on, so you need to connect the LED via a 470Ohm resistor to ground. See, for instance:
Arduino LED - Complete Tutorial - The Robotics Back-End
Note that the example uses a 220Ohm resistor - as you could have all LEDs on at once, as well as other things happening, it is good practice to keep the current drain per LED low, so 470Ohm would be better.

Both the buzzer and the relay are ‘active high’, like the LEDs, so the other side of each device is connected to ground. The relay does not require a resistor, but you might choose to insert one for the buzzer depending on the sound level you need. Note that when looking for a suitable buzzer you want one described as ‘active’. That means it only needs to be switched HIGH to work - most buzzers you will see advertised are ‘passive’ - they need to be controlled with a tone. The Arduino can do this, but it complicates the code.

Yes. You have two spare pins either of which can be used as inputs. A simple momentary switch connected to one of these pins, configured as an input, could be used to set a flag variable that determines whether or not the buzzer should sound using a simple test at the line where the buzzer pin is activated. Turning the buzzer off after it has been activated is not a problem with the existing code, because the sensors are checked every 100mS and the relay and buzzer are changed if required. So the additional code for your silence button can also reset the value of the flag variable (if set) so that the buzzer will sound the next time liquid is setected. See, for example:
Arduino Push Button - Complete Tutorial - The Robotics Back-End

1 Like

Hi Mate
Where are you in the workld please
Can I pay u to be on a call or zoom from time to time - Ongoing consultant

1 Like

Thanks Dan
That is working great.
I have changed it a little and am sending the signal in on say 2 and on the 3 pin I noticed it is 5 volts out so I am driving a relay that will power the LED.
This works(should it?)

Can you modify the code so that when there is an alarm on any of the 8 pins then it sounds a joint alarm and then a push button will silence the alarm while in trigger mode and resets back to waiting for an alarm.
So close now
I will order some more sensors now
Thanks again
W

I am not sure for safety if it needs any diodes or anything else
Thanks
W

Hi Wayne,

Its worth mentioning that for a commercial project it may be worth getting some engineering advice, here in Newcastle we have Saphi - but there may be similar near you.

Liam