Arduino Mega, 1 or 22 resistors

Hi,

Iam creating a water level indicator reporting to mqtt
Its using 22 water switches => https://goo.gl/images/4YBFEQ

Now my question is: Can i use 1 10k resistor or do i need 22 resistors?

my un-finished code

#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>

byte floatPin[] = {30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; // 22pins

const byte floatswitches = 8; 
int level = 0; 

#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celcius=0;

IPAddress server(192,168,0,115);  
EthernetClient ethClient;
PubSubClient   client(ethClient);
byte mac[]    = {  0xBE, 0xEF, 0x3E, 0x7E, 0x4A, 0x1F  };// DHCP (be:ef:3e:7e:4a:1f)...success: 192.168.0.106

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println("START");
  Serial.println("Ethernet.begin");
  Ethernet.begin(mac);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP()); 

  client.setServer(server, 1883);
  client.setCallback(callback);
  delay(1500);
  
  sensors.setResolution(9); 

  for(int i = 0; i < floatswitches; i++)
  { 
    pinMode(floatPin[i], INPUT); 
    Serial.println(floatPin[i]);
  } 
  
}


void loop(void)
{ 

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  //Serial.println("MQTT CONNECTED");  

  client.publish("arduino/watertank/status", "ok");
  client.publish("arduino/watertank/ip", String(Ethernet.localIP()).c_str());

  level = 0; 
  for(int i = 0; i < floatswitches; i++) 
  { 
    if(digitalRead(floatPin[i]) == HIGH) 
    { 
      //Serial.print(floatPin[i]);
      //Serial.println(" HIGH"); 
      level = level + 1;
    }
    else
    {
      //Serial.print(floatPin[i]);
      //Serial.println(" LOW"); 
      
    }
    
  } 
  Serial.print("Water level: "); 
  switch(level)
  { 
    case 20: Serial.println("2000L"); break; 
    case 19: Serial.println("1900L"); break; 
    case 18: Serial.println("1800L"); break; 
    case 17: Serial.println("1700L"); break; 
    case 16: Serial.println("1600L"); break; 
    case 15: Serial.println("1500L"); break; 
    case 14: Serial.println("1400L"); break; 
    case 13: Serial.println("1300L"); break; 
    case 12: Serial.println("1200L"); break; 
    case 11: Serial.println("1100L"); break; 
    case 10: Serial.println("1000L"); break; 
    case 9:  Serial.println("900L"); break; 
    case 8:  Serial.println("800L"); break; 
    case 7:  Serial.println("700L"); break; 
    case 6:  Serial.println("600L"); break; 
    case 5:  Serial.println("500L"); break; 
    case 4:  Serial.println("400L"); break; 
    case 3:  Serial.println("300L"); break; 
    case 2:  Serial.println("200L"); break; 
    case 1:  Serial.println("100L"); break; 
    case 0:  Serial.println("0L"); break; 
  } 

  sensors.requestTemperatures(); 
  Celcius=sensors.getTempCByIndex(0);
  Celcius = Celcius - 1.1;
  //Serial.print(" C  ");
  //Serial.println(Celcius);
  client.publish("arduino/watertank/watertemp", String(Celcius).c_str());

  delay(1000); // 30sec
  //delay(300000); // 5min
}


void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoWatertank")) 
    {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 1 second");
      delay(1000);
    }
  }
}

Thx in advance

22 or none (if you use the internal pullup)

Mark

No resistors needed if you wire the switches between pin and ground. And use the internal pull up resistors of the micro.

pinMode(floatPin[i], INPUT_PULLUP);

Logic is reversed now. An open switch is a HIGH on the pin, and a closed one a LOW.

Did you think of how to mount 22 of those VERTICAL float switches? Don't you need horizontal ones?

There are also float switches like that with multiple reed switches in the stem, so you only need one float sensor for multiple levels. The reed switches are internally connected with a resistor ladder. The ladder can be read with one analogue input (no Mega needed). Leo..

22 float switches ? What is the application? Apart from what has already been said:

  1. Long connections to an Arduino can be prone to interference, for example if you are monitoring remote tanks.
  2. There could be alternative options such as ultra sonic or pressure sensors if you are measuring depth.


Example: sertup

As you can see, ill mount 11x 90° float switches inside 1 tank
These will be mounted on ALUMINIUM or INOX ruler
Depending on how many switches are “HIGH” ill know how many L the tank has

Admittedly, I have no direct experience of this. Just some school day physics. But I'd certainly be considering alternatives like the ones already mentioned or maybe something like this, which appears to be a pressure sensor (equivalent to 0 to 5 meter depth of water) from which you can derive the water level:

https://de.aliexpress.com/item/Submersible-Water-Level-Transmitter-Level-Transducer-Sensor-6m-Cable-Electronic-Water-Level-Sensors/32749498400.html

You may need also to measure the atmospheric pressure to get an accurate reference, say with a second sensor.

Much better idea - use three 74HC165 parallel-to-serial shift registers chained together to read your 22 switches - each switch wired to ground and a pull-up to 5 V. The three registers are chained to just three Arduino pins and read by three successive shiftIn operations into three bytes.

For "bulk" pullup resistors, they make bussed resistors in a SIP package. The 9 pin version provides 8 pull up resistors in a single package?

Has anyone tried ultrasonic or other type of distance measurement for this application?

I had a mind to mention the SIP resistor packs, but thought it could wait until the OP acknowledged the shift register approach, which means you do not need a Mega 2560 (unless the complexity of the Ethernet demands it :roll_eyes: ).

janvier123: As you can see, ill mount 11x 90° float switches inside 1 tank These will be mounted on ALUMINIUM or INOX ruler

Note that these float switches must be mounted into the sides of the tank. The wiring is not waterproof, so must be outside the tank.

Paul__B: wiring is not waterproof, so must be outside the tank.

i have waterproof connectors, ones you need to heat up and a kind of goo comes out We use it at work all the time

Paul__B: Much better idea - use three 74HC165 parallel-to-serial shift registers chained together to read your 22 switches - each switch wired to ground and a pull-up to 5 V. The three registers are chained to just three Arduino pins and read by three successive shiftIn operations into three bytes.

I have an dedicated arduino mega :) so i dont need to worry about the pins

janvier123: I have waterproof connectors, ones you need to heat up and a kind of goo comes out

You may well have waterproof connectors, but how are you going to waterproof the connections into the switch which are not waterproof?

Epoxy? Silicone?

Hi, What is the distance from the tank to the Mega?

Tom.. :)

About 1.5m (2 Meter max), iam planning to have it in the middle with a water proof box