Hi guys, I wanted your help on my project with Liquid pumps. I was trying to follow this project "https://arduinogetstarted.com/tutorials/arduino-controls-pump" while I added SerialEvents to be able to control the pump through the Serial Monitor. Unfortunately I kept on encountering a problem where the Serial would either completely freeze and be unresponsive or it would reset itself and go back to void setup. Please note that I am using the Raspberry Pi3 to run the Arduino IDE which is then connected to the Arduino Uno through a USB cable.
const int RELAY_PIN1 = 7;
//int inputValue = 0; for Communicating with the RPI
int pumpcount = 0;
String inputString = "";
boolean stringComplete = false;
void setup()
{
pinMode(RELAY_PIN1, OUTPUT);
digitalWrite(RELAY_PIN1, LOW);
Serial.begin (9600);
/*
while (Serial.available()<=0)
{
Serial.println("Waiting For Command:");
delay(500);
}
*/
}
void loop()
{
if (stringComplete)
{
if(inputString.startsWith("pump"))
{
pumpOn(2000);
}
else
{
Serial.println("Invalid Command");
}
//Reset String
stringComplete = false;
inputString = "";
}
delay(10);
}
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n')
{
stringComplete = true;
}
}
}
void pumpOn(int secs)
{
Serial.println(pumpcount);
pumpcount +=1;
digitalWrite(RELAY_PIN1, HIGH);
Serial.print(" :Pump is ON");
delay(secs);
digitalWrite(RELAY_PIN1, LOW);
Serial.println("Pump is OFF");
delay(secs);
}
-----Sample Serial Monitor Output------
0
:Pump is ONPump is OFF
1
:Pump is ONPump is OFF
2
:Pump is ONPump is OFF
3
:Pump is ONPump is OFF
4
:¨0
:Pump is ONPump0
:Pump is ONPump is OFF
1
:Pump is ONPump is OFF
2
:Pump is ÂŻĂş0
:Pump is ONPumĂż0
:Pump is ONPump is OFF
1
:Pump is ONPump is OFF
2
:Pump is O
-----Sample Serial Monitor Output------
Was hoping you guys could help me figure out the problem.
Your fritzing makes things hard to understand. Better to draw a schematic. Then you can include the power to the arduino. You need to understand the relay specs to know if this is an issue and particularly if there is a fly back diode
A library? I dont think so. The power to the arduino is provided by the Raspberry pi. The Relay specs are
*1 Channel 5V 10A Relay Module with Optocoupler
Features:
•One normally closed contact and one normally open contact
•Triode drive, increasing relay coil
•High impedance controller pin
•Pull-down circuit for avoidance of malfunction
•Power supply indicator lamp
•Control indicator lamp
Specification:
•Channel: 1 Channel
•Relay Voltage: 5V
•Dimensions: 56.5mm x 17mm x 20mm
•Weight: 16.5g
I also inquired to the seller for the specs of the Relay module for clearer understanding.
OP? do you mean Operating Pin? Unfortunately I do not have the schematics of the relay module but I have inquired the seller. Here is an image of the relay module. Hopefully it helps
Oi! that actually looks like an opto-isolated relay module.
Post an image of your project. Add some serial prints to your code to see where the issue may lie. The breadboard may not be able to supply the power. The Uno may still not be able to power that opto-isolated relay module.
I hope its clear enough. Here are the labels for the wires.
Arduino:
Silver Wire: This is connected to IN pin in the Relay module
Red Wire: Connected 5V pin in Arduino then connected to the small breadboard which is also connected to the VCC pin in the Relay Module
Black Wire: Connected to GND pin in Arduino then connected to small breadboard which is also connected to GND pin in Relay Module.
Relay Module:
Normally Open pin is connected to the positive terminal of 12V power Supply.
Common Pin is connected to the positive terminal of the pump.
Both 12V power supply and pump negative pins are connected through the bread board. Everything powers up but somehow fails after a few runs of the pumpOn() command.
Do you then suggest me to purchase a 5v power supply to try and power the Relay module separately? I also want to note that whenever I set digitalWrite pin 7 to LOW somehow closes the circuit even when I have the load on the Normally Open pin. The circuit opens only when I set digitalWrite pin 7 to HIGH. I was just wondering why it is the opposite?
Seems you have this relay module where it seems impossible to actually enable the opto isolation feature. (unfortunately there's a common ground, common VCC, no jumper)
Arduino is not a power supply for the relay load. I have another 12V power supply for the load while the Arduino powers the relay through the 5v Arduino pin.
Would have to treat this as a non-opto isolated relay module, so it will be more susceptible to EMI contact arcing. Specifically, I'll bet the issue occurs when the relay is de-energized. In any case, you'll need to use a flyback diode connected across the 12VDC pump. The cathode connects to the pump 12VDC terminal, anode connects to the pump's switched terminal.
You might try using a separate power supply for the Arduino side of the relay. Unfortunately, you will need to connect the grounds so, as already mentioned, the opto-isolation is compromised. Consider a relay module where signal and power can be truly separated.
The other thing that often causes lockups (though likely not resets) is use of String objects, which I see in your code.
Snubbing on the pump side has become a common suggestion in recent weeks.
@wildbill@dlloyd thanks guys! I will try to add a separate 5v power Supply to the relay module. With regards to snubbing the pumps connection, by snubbing you mean adding a diode correct? I thought the optocoupler allowed the two circuits to be separate? sorry for all the questions I am fairly new to this.
Your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem
They didn't provide a way to actually isolate the supply (no jumper or extra connection terminal). This means one or more traces would need to be cut on the printed circuit board in order to enable opto isolation. I wouldn't worry about this for now. I would just rewire your pump connections so that the relay contacts switch the load side of the pump to GND, then add a flyback diode across the pump as described earlier. The UNO's 5V output should be OK to handle 1 relay module (about 80mA).
Doing this might be enough to resolve the problem.
Have a look at this thread. It's a very similar problem and @Grumpy_Mike has some things to say about snubbing circuits, in that case a little more than just the diode. Whether it's necessary in your case is beyond me.