Help - Newbie - Relay Reset / Code Query / Lifecycle Testing - 100,000 Runs

Hi All,

Right I have no previous experience with an Arduino and have been tasked to create a lifecycle test (100,000 runs) for work for some valves which spring return. I had a quick look into these and thought they would do the trick.

I have therefore purchased the following arduino and 4 x relay.

http://uk.rs-online.com/web/p/processor-microcontroller-development-kits/8750292/

http://uk.rs-online.com/web/p/motor-control-development-kits/1244687/?sra=pstk

Having followed a few videos I have copied the following code and it has worked for now. However after running for 4/5 hours the adruino or relay had stopped overnight. I therefore pressed the reset button on the arduino 4 relay shield and it continued.

My problem is that I do not know how to code the number of times it has run. It would be good to add a screen and show this but I was just going to work on the fact that the loop runs for 24 seconds and therefore in a 24hr day it would run in the region of 3600 times. Unfortunately if this stops I don't know when and why.

Is this a memory issue or do the code just reset after completing the loop?

Any help with what could be going wrong and if a counter could be added with ease, then it would be much appreciated? Or more importanatly can the arduino actually perform the task of running a code 100,000 times?

Thanks all in advance.

Matt

CODE
/4-Relays Shield Example/
//define variable
int RELAY1 = 4; **
int RELAY2 = 7;
int RELAY3 = 8;
int RELAY4 = 12;
void setup()
{ **
//set Relays as Output
** pinMode(RELAY1, OUTPUT); **
** pinMode(RELAY2, OUTPUT); **
** pinMode(RELAY3, OUTPUT); **
** pinMode(RELAY4, OUTPUT); **
}
** void loop()

{
** digitalWrite(RELAY1,HIGH); // Turns ON Relay1

** digitalWrite(RELAY2,HIGH); // Turns ON Relay2**
** digitalWrite(RELAY3,HIGH); // Turns ON Relay3**
** digitalWrite(RELAY4,HIGH); // Turns ON Relay4**
** delay(17000); // Wait 17 seconds**

** digitalWrite(RELAY1,LOW); // Turns ON Relay1**
** digitalWrite(RELAY2,LOW); // Turns ON Relay2**
** digitalWrite(RELAY3,LOW); // Turns ON Relay3**
** digitalWrite(RELAY4,LOW); // Turns ON Relay4**
** delay(7000); // Wait 7 seconds**

}

Your code is fine (would be better to indent the code (ctrl-T in the IDE) and use code tags though when posting code)

You might have a power issue - how is the whole thing wired and powered?

You mention valves as well - they usually involve a coil and as a solenoid is an inductive load so you need to include a flyback diode (a diode used to eliminate flyback, which is the sudden voltage spike seen across an inductive load when its supply current is suddenly reduced or interrupted).

-> are the relays protected (and your arduino) ?

See this example for driving a valve for example

I would put a 50 millisecond delay between valves so they are not pulling in and dropping out at the same instant.

  void loop()
{
   digitalWrite(RELAY1,HIGH);  // Turns ON Relay1
   delay(50);
   digitalWrite(RELAY2,HIGH);  // Turns ON Relay2
   delay(50);
   digitalWrite(RELAY3,HIGH);  // Turns ON Relay3
   delay(50);
   digitalWrite(RELAY4,HIGH);  // Turns ON Relay4
   delay(17000);                // Wait 17 seconds
   
   digitalWrite(RELAY1,LOW);  // Turns ON Relay1
   delay(50);
   digitalWrite(RELAY2,LOW);  // Turns ON Relay2
   delay(50);
   digitalWrite(RELAY3,LOW);  // Turns ON Relay3
   delay(50);
   digitalWrite(RELAY4,LOW);  // Turns ON Relay4
   delay(7000);                // Wait 7 seconds
 

}

and if a counter could be added with ease, then it would be much appreciated

Adding a counter is trivial.

long runCount = 0;

Every time loop() iterates, increment runCount.

You can add code to test that runCount is less than 100000 before executing the code in loop().

Hi JML,

Thank you for your response.

Power - This Arduino has a mini USB connector in which it receives its power from 230V main. The board does have its own digital power input from 5v to 23v but its not being used.

Flyback Diode - Is this needed with relays? The 230v motors are connected to the NO and common connections on the relay to complete the full circuit and therefore are driven open when the arduino provides the HIGH voltage.

Relays protected - Can you explain more what you mean. I don't believe anything is protected.

Also do you know how I could count the number of times these relays switch? If the power is being lost I assume I would loose the count on the display as the arduino doe not physically count in its memory the number of times it runs?

Thanks in advance

PaulS:
Adding a counter is trivial.

long runCount = 0;

Every time loop() iterates, increment runCount.

You can add code to test that runCount is less than 100000 before executing the code in loop().

I apologies for the trivial questions. I did say I was a beginner...

Where would the runCount be displayed and how would I see this when being run?

I'm going to ask a big favour and can you write this in a code?

Where would the runCount be displayed

Wherever you write code to display it…

void loop()
{
   if(runCount < 100000)
   {
      runCount++;

      // Put the rest of the code here
   }
}

But if you loose power, you will loose the memory of the count....

You could possibly write every 10 or 100 times your count in EEPROM (eeprom has a write lifetime of 100,000 times, so either write in different places or don't write every count)

An alternative could be to connect a mechanical stepper motor counter that could count up and driven from arduino

Hi All,

Unfortunately the Arduino stopped running again overnight.

I reset pressed the reset button on the Arduino board as opposed to the relay shield this time and it started to run again.

Does anyone know what this could be? I cant see this being a power issue twice in two nights. Plus our servers do not show any power loss.

Paul S, I've tried your code and it mentions 'runcount' was not declared in this scope.

I really need to be able to capture the count somehow and if this stops it would still be logged. Any ideas?

Thanks All

Matt

Anyone?

Matt_1796:
Paul S, I've tried your code and it mentions 'runcount' was not declared in this scope.

Put a line like this: long runcount; in the area above void setup() - where your int RELAY statements are. This will make runcount a 'global' variable so it's accessible everywhere in the program.

Explanation of scope

Matt_1796:
Flyback Diode - Is this needed with relays? The 230v motors are connected to the NO and common connections on the relay to complete the full circuit and therefore are driven open when the arduino provides the HIGH voltage.

Relays protected - Can you explain more what you mean. I don't believe anything is protected.

Some cheap relays are not protecting against high surge when you drive motors. I had not seen the products you purchased it's fine for your use.

the relays shield is consuming 140mA with all releays on, about 35 mA each. The best practices Recommends to not draw more than 20mA from a pin (with peak at 40 Ok) and not more than 150mA from a port. If you are powering from standard pins At 35mA and 140mA total Having the 4 on at the same time for 17 seconds this might actually be way too high for your arduino clone and it fails.

I'm not familiar with the "DFRobot DFR0305 Romeo BLE - Arduino Robot DC Motor Controller Board" but it's clearly not the UNO type of pin specification - how did you connect the "Arduino 4 relay shield" / what is wired where and how does power get into the relays?

--> to test and be safe - I would suggest you power from a wall outlet - 9V 2A for example instead of the the USB which can only supply 500 mA and is capped at 5V and might drop below 5V if you put high demand

Also the suggestions above to keep track of the count will be lost in case of the arduino (well technically it's not really an Arduino, seems compatible) rebooting or becoming stuck if you don't save that in a safe place as you go (SD card, EEPROM) or print it through serial but in that case make sure the PC does not loose power either

As a quick and dirty test you could print the count to the Serial monitor and keep it open on a PC. If the PC has reset in the morning you have bigger problems that just the Arduino. It might also be instructive to print the value of millis() along with the count or, slightly better, the number of hours, minutes and seconds since starting.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please include your power supplies.

It will help, also a picture of your project so we can see your component layout.

Thanks.. Tom... :slight_smile:

J-M-L:
Some cheap relays are not protecting against high surge when you drive motors. I had not seen the products you purchased it's fine for your use.

the relays shield is consuming 140mA with all releays on, about 35 mA each. The best practices Recommends to not draw more than 20mA from a pin (with peak at 40 Ok) and not more than 150mA from a port. If you are powering from standard pins At 35mA and 140mA total Having the 4 on at the same time for 17 seconds this might actually be way too high for your arduino clone and it fails.

I'm not familiar with the "DFRobot DFR0305 Romeo BLE - Arduino Robot DC Motor Controller Board" but it's clearly not the UNO type of pin specification - how did you connect the "Arduino 4 relay shield" / what is wired where and how does power get into the relays?

--> to test and be safe - I would suggest you power from a wall outlet - 9V 2A for example instead of the the USB which can only supply 500 mA and is capped at 5V and might drop below 5V if you put high demand

Also the suggestions above to keep track of the count will be lost in case of the arduino (well technically it's not really an Arduino, seems compatible) rebooting or becoming stuck if you don't save that in a safe place as you go (SD card, EEPROM) or print it through serial but in that case make sure the PC does not loose power either

Hi JML,

The DFR0305 Romeo BLE does have the same input as an Arduino as the shield fits straight onto the top and taking power from the Romeo.

I will look at fitting an external power supply. I believe the Romeo can take an external power supply range of 5 to 23 V so I will try to use an old transformer.

I am only connecting 3 motors at the moment and they are 5W, 6W & 6.5W. This would mean im on two of the valves im drawing more than 26mA but as a total no more than 75/80. Should this be a problem.

Very interested in your idea regarding the printing to a laptop via serial. I have an old one sitting in my test room which I can use. Is this easy? Do you have the code or how to wire it?

Thanks in advance

If you power from the wall plug you might have a switch on the board to change cf the doc

Motor Power terminal
The setting for the system & motor power switch:

On: supply power to the motor driver and system power regulator. The input range is from 5~23 volts. It’s suitable for most of robot platform.
Off: Isolate the system power supply from the motor power. In this case, it requires to supply system voltage from Micro USB port,5v power source to 5v & GND pins directly or 5~23v power source to VIN & GND pins.

Printing to the console is just having a USB cable plugged into your module and using the Serial class to issue print commands

Note that your module is a bit special, it has two Serial ports And you use Serial1.[i]methodCall[/i]() instead of Serial.[i]methodCall[/i]() in code to communicate with devices connected to serial interface (Pin 0/1) but you do use Serial.[i]methodCall[/i]() for USB debugging on pc serial monitor.

(Use leonardo as type card for uploading)

unsigned long n = 0;

void setup() {
  Serial.begin(115200);      // open the USB serial port at 115200 bps
  while (!Serial); // wait for serial port to connect if needed (probably not needed with your board)

   // other setup stuff here
   //....

}

void loop() {
   if (n < 100000ul) { // do this 100,000 times
     // print n
     Serial.print("Iteration # "); Serial.println(n);

      // here you would drive the relays for one iteration 
      // ....


      // before going  to the next iteration and remember it by increasing n
      n++;
   } // end if
}

But alternatively as your arduino Supports Bluetooth HID it could be seen as a remote keyboard for your computer (if your computer has Bluetooth too) and if you open excel or any text editing app, and put the cursor at the top and send keys like you were typing on a keyboard it will fill in your document