Controlling dc motor through relay doesn't work as expected

I'm doing a little project to empty the water out of a bucket when the water is going to overflow.

When the high level is reached, the pump is started until the water goes under the low level.

The schema I use is as follow

Pin 2 and 3 correspond to high level and low level pin, are configured as INPUT_PULLUP and are grounded through a 10k resistor representing the resistance of the water.

The code I'm using is as follow

#define lowPin 2
#define highPin 3
#define greenLed 4
#define redLed 5
#define relayPin 6

void setup() {
  pinMode(lowPin, INPUT_PULLUP);
  pinMode(highPin, INPUT_PULLUP);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(relayPin, OUTPUT);
  if (digitalRead(lowPin) and digitalRead(highPin)) {
    digitalWrite(redLed, LOW);
    digitalWrite(relayPin, HIGH);
    digitalWrite(greenLed, HIGH);    
  } else
  digitalWrite(redLed, HIGH);
  digitalWrite(relayPin, LOW);
  digitalWrite(greenLed, LOW);    

void loop() {
  while (digitalRead(lowPin) or digitalRead(highPin)){}
  digitalWrite(redLed, HIGH);
  digitalWrite(relayPin, LOW);
  digitalWrite(greenLed, LOW);
  while (!(digitalRead(lowPin) and digitalRead(highPin))){}
  digitalWrite(redLed, LOW);
  digitalWrite(relayPin, HIGH);
  digitalWrite(greenLed, HIGH);

Now here is the issue : when the DC motor representing the pump is not plugged in, everything just works fine. The program waits for both pins to be grounded through the 10k (this correspond to full bucket). Then, then pump is switched on until both pins are high.

But with the motor, disconnecting one wire from the 10k resistor stops the motor immediately (instead of waiting for both the wires to be "ungrounded").
I'm completely puzzled as my motor circuit is completely isolated from the Arduino...
I also noticed that removing the 10k resistor (and connecting/disconnecting sensor pins directly to ground solves the problem), but it wiill be an issue within real circumstances
Any help would be greatly appreciated...
I also put a link to a video to demonstrate this issue.

Relay contacts tend to bounce and produce sparks with RF effects. Better use a MOSFET for turning the motor on.

Put a freewheel diode over the motor.

Thank you for your answer :slight_smile:
I actually tried both solutions. If forgot to put the freewheel diode on fritzing, but it's been there all along. And the problem is the same with the MOSFET. I's actually worse in this case (directly un/grounding the sensor pins doen's longer work).
The project is working fine with the relay, it's only when the motor is used that I have the problem.

I suspect that you put the diode over the battery, not over the motor?

the 10K value for GND is too high to consider the port to be LOW.
The value of the internal resistor, (pull-up resistor), of Atmega can have values from 20k to 50K.
Atmega328 Datashee pag 314 I / O Pull-up Pin Resistor 20 50 k

With a 10K resistor for GND the value in the port can have values from 0.8V to 1.7V, and these values can be interpleted as HIGH or LOW.
In your tests, put a small resistor (eg 510 Ohms) in place of the 10K one and see how your program behaves.

RV mineirin

1 Like

@ruilviana Thank you very much. I've thought about that too. I've tried with a 560R, and it kinds of made things a bit better, but not always. And if I bypass the resistor completely, it doesn't work at all :grimacing:
My setup is now with a MOSFET, a flywheel diode on the motor and I tried 560, 180, 100 and 0 ohm... With no success

Yep, good point for the flywheel, my mistake when I took the pictures, and fixed it after :slight_smile:

So, are you sure your simulation using resistors accurately represents the bucket with electrodes in it? I'd be very surprised if the conductivity of ordinary tap water would be enough to pull down those two inputs with internal pullups.

Anyway, If everything works perfectly EXCEPT when the motor is connected (i.e. you literally just pull out one of the motor wires) then it suggests the motor is creating electrical noise that gets picked up in the rest of the wiring. I recommend connecting a ceramic capacitor across the motor; 100nF or larger.

@SteveThackery You maybe right about the tap water, this why I wanted to experiment first. I'm affirmative : circuit works perfectly fine with motor unplugged, whatever the resistance (at least until 10k). I'll give a try to your suggestion, which is promising. I'm not used to capacitors though, which kind of it should I use (electrolytic, ceramic, tantale ?). Also "across the motor", does it mean like the flywheel diode on both pins or something else ? :thinking:

Ceramic, as I mentioned. They have many shortcomings, but they are brilliant for absorbing spikes and other noise.

Connect one leg of the capacitor to one terminal of the motor, the other leg to the other terminal. Polarity is unimportant.

I just meant that I think the resistance might be a lot more than that with ordinary water. Water only conducts because of the impurities in it.

The capacitor will stress the driver transistors. Shouldn't a resistor be added in series with the capacitor?

I thought the motor was operated by the relay?

Edit: Ah, he's switched to a MOSFET. So put a 4.7 or 10 ohm resistor in series with the capacitor.

Thank you. I guess both of you were right, the resistance was too high and the the smallest parasites were enough to make the whole thing unstable. I can't give credits to both of you, sorry :wink:
I finally made it with two additional B547, and now it's ok :+1:

1 Like

You can give hearts :wink:

1 Like

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