1

I am trying to make a simple water overflow protection system for my home. I have created it using BC547 and NodeMCU (ESP8266). I have a simple circuit like this:

enter image description here

The problem here is I have 2 wires going into the tank. Which suppose to complete the circuit as soon as water touch both the wires. But the problem here is its by default complete the circuit without even touching, this is happening only in my rooftop water tank. I tried this circuit locally on my desk first and it is working fine. I feel the problem can be humidity inside the tank? Can this be a reason for false trigger?

Here is how I have my code. #define RELAY_1 D1 #define TANK_CONNECTION D4

void setup() {
  Serial.begin(115200);
  delay(10);


  Serial.println("Started");
  secondsTick.attach(1, ISRwatchdog);

  //Set relay pins to outputs
  pinMode(RELAY_1, OUTPUT);
  pinMode(TANK_CONNECTION, INPUT);

  digitalWrite(RELAY_1, relay_on);
  delay(100);

}

void loop() {
  tankFull(); 
  delay(100);
}

void tankFull(){

  Serial.println(digitalRead(TANK_CONNECTION));
   if(!digitalRead(TANK_CONNECTION) && relay_on == 0){
      Serial.println(motorOnCount);
      motorOnCount++;
      motorOffCount = 0;
      if(motorOnCount >= 10){
        Serial.println("Motor Turn OFF");
        digitalWrite(RELAY_1, HIGH);
         if (client.connected()) {
            client.publish(motor_status, "off", true);
            client.publish(tank_full, "on", true);

          }
        motorOnCount=0;
        relay_on = 1;
      }
      delay(100);
   } else{
      motorOffCount++;
       if (client.connected()) {
          client.publish(tank_full, "off", true);
       }
      //Serial.println(motorOffCount);
      if(motorOffCount>=100){
        motorOffCount=0;
        motorOnCount = 0;

      }
   }
}

Obviously this is not the full code. I am just trying to share the pattern of its working for basic understanding of the setup I have.

Any one have idea about why its always shows circuit complete even without touching the wires. I have tried with 5v as well and 3.3v as well which are present on NodeMcu Board. But none of them worked properly.

  • 2
    try pinMode(TANK_CONNECTION, INPUT_PULLUP); since you need a pullup resistor with that circuit – chrisl Apr 10 at 8:24
  • OK. And value of resistor will also make a difference here? – user3201500 Apr 10 at 8:25
  • With the INPUT_PULLUP you enable the internal pullup resistor of the MCU. Mostly this is something in the range of 10kOhm, but this is not critical here. The value of the resistor at the base of the transistor also shouldn't be critical. The transistor has only have a considerable less impedance when turned on, than the internal pullup resistor. 1kOhm seems ok. You can try smaller values, when it doesn't activate correctly after you actived the internal pullup resistor – chrisl Apr 10 at 8:30
  • Please try the INPUT_PULLUP. If that works, I will formulate a proper answer. – chrisl Apr 10 at 8:31
  • 1
    I'd suspect it won't be much EMI proof. And if you have long wires the INPUT_PULLUP might not be enough. – KIIV Apr 10 at 18:18
2

Certainly the issue is from EMI but not from the collector side as from the transistor base even the wires are shorter on that side as I understand from the comments.

It's a common mistake to let the transistor base floating, see here a different application but with the same cause and result, the transistor is turning on apparently with no reason. With the base floating the EMI current has no other path than through the transistor base where is amplified by beta ( 110 to 800 for BC547).

Put a 10Kohm resistor between the transistor base and emitter to let a path for EMI currents to the ground. Try different values, smaller if you still have false triggering, higher if the sensor is not turning on the transistor.

Of course, use INPUT_PULLUP but the internal resistor is to high 30 to 100 kohm, better use an external resistor arround 10k or lower form the transistor collector to 3V3 to ensure a good noise margin on the collector side to.

schematic

simulate this circuit – Schematic created using CircuitLab

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.