Arduino mega crash

Hello,

I had a strange problem.
I set up a test banch with an arduino mega, reading 16 sensors and operating 4 relais by transistors.
I use serial monitor of arduino programmer to comunicate with arduino (on a notebook with ubuntu).

Running the software, sometimes it runs straight to the end, sometimes it crashes in 2 different ways:
-Crash and reset, (like pushing reset button)
-Crash and no more serial comunication, replugging USB is needed

I have no idea how to solve the problem, anyone can suggest me where to search? Arduino hardware? My software? My external hardware?

Thanks for any help
Best Regards
Roberto

Arduino hardware? My software? My external hardware?

All of the above, plus your power supply and grounding scheme.

Read this before posting a programming question

Ok...

Yes, I read "Read this before posting a programming question ...", I tried 2 month to solve the problem myself, cheching other posts and so on. I disturbed only as very last resource.
Ok, no help.
Sure, I did not wrote in right section in royal grade manner style, my fault! My sin! Sorry!
Please, do not waste time answering, I'll cancel my user.

Do you want me to put your toys back in your pram?

Seriously, what have you told us?
Where is your code?
Where is your schematic?
Did you read reply #1?

betozz:
Sure, I did not wrote in right section in royal grade manner style, my fault! My sin! Sorry!
Please, do not waste time answering, I'll cancel my user.

Cancel your user? Good idea. How are we supposed to help? You may as well ring the garage and say your car won't go without giving any more details at all. As you did above.

I tried 2 month to solve the problem myself ...

I can believe that.

betozz:
Yes, I read "Read this before posting a programming question ..."

You may have read it, but you haven't followed the advice in it. That article explains how to ask for help in the way that gets best results. You've ignored that, and you haven't given any of the information that would be needed to understand what your problem is.

Yes, belive that, 2 months trying to solve the problem.
And yes, your right, I did not post in the correct way as indicated, again, sorry.
Really sorry, I was so frustraded but you are here just to help, my answer was not fair.
I was neither able to cancel my user, can you belive it?

Bye
Roberto

No-one said you posted in the wrong place, just that you didn't post enough information to help you.
People here are helpful and experienced, but they're not psychic.

I'm very sorry for your frustration, and we may be here to help, but only if you are willing to put in some effort to help yourself.

I wrote "anyone can suggest me where to search? Arduino hardware? My software? My external hardware?", becaure I had no idea how to proceed, and as a first aproach, I thought providing everithing was too much (I connected more than 100 components without drawing). Waiting an answer like "can you provide ...".
This only to explain you what was in my mind when I posted, I don't want to be right, I admit it was the WRONG WAY to post a problem, and absolutely my answer of 13 june was sincerely not correct and fair

I connected more than 100 components without drawing

That's...not sensible.
Without a schematic, we have little chance of helping.

It's clear that English is not your first language - have you tried some of the sections in the International section?

betozz:
Waiting an answer like "can you provide ...".

Can you provide your code?

when i hear relay, my first question is: do u use diodes to protect your electronics from induction?

That was pretty much my first thought too, but the diodes protect against back EMF.
Which is why a schematic would be a useful thing to post.

betozz:
I wrote "anyone can suggest me where to search? Arduino hardware? My software? My external hardware?",

It could be any of those. That is why providing your code, describing your Arduino hardware, and placing links to the external hardware would be a good idea. And some sort of circuit.

betozz:
I tried 2 month to solve the problem myself, cheching other posts and so on.

You have wasted another 7 days, so I can understand how it is taking so long. On the very first day you posted we suggested you submit your code, and describe your hardware. You still haven't done so. Is this some kind of "wasting time" project?

Hello,

thanks for replay!

I tried to isolate the problem, reflecting on your comments, I was able to reproduce the crash with a simple part of the code and a simple part of hardware.
I tried to draw a schematic with gEDA, my first one (I'm a mechanic), let me know if it's not clear, following the used components:
transistor BC548BG
diode 1N4007
relay G5RL 1A E HR 12VDC
https://www.distrelec.it/ishopWebFront/search/luceneSearch.do?dispatch=find&keywordPhrase=408110

I have 3 blocks of relay, diode and transistor, connected to the same 12VDC.
each realy swithches a solenoid valve 220VAC 6.3W.

I tried to remove +12V supply to arduino, so arduino is supplied by USB but still ground of 12VDC connected: exactly same results.

I found as follow:
only one output switched on/off: no crash on over 300 cycles
3 ouputs on/off: crash after less than 15 cycles
2 ouput always on, 1 output on/off (as schetch, but other combination tested): crash after less than 15 cycles

It seems to me that the crashes are related to switch off, but I'm not sure some crash occurred in other situation.

I put Serial.print between 2 delays because, with this command just after a digitalWrite, I note SOMETIMES a crash DURING serial transmit (partial texy displayed).

int n = 0;
void setup() {
    Serial.begin(9600);

}

void loop() {
  
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
delay(4000);
n = n + 1;
//digitalWrite(10, LOW);
digitalWrite(11, LOW);
//digitalWrite(12, LOW);
delay(2000);
Serial.print(n);Serial.println();
delay(2000);
}

thanks again
Roberto

R.ps (18.9 KB)

It seems to me that the crashes are related to switch off

Sounds like classic back-EMF - have you got diodes fitted to anything that has a coil?

digitalWrite(10, HIGH);

Pins are inputs by default, so all you're doing here is switching on the built-in pullup resistor.

I have a diode on relay coil as in schematic, no diode in solenoid valve coil as it works in alternate current. Do you think that shutting off solenoid valve current with relay can generate something that can pass to arduino cyrcuit?

Pins are inputs by default, so all you're doing here is switching on the built-in pullup resistor.

Are you meaning I have to declare pin as output? Normally I do that, I forgot in this test code.

Provlem solved,

Sounds like classic back-EMF - have you got diodes fitted to anything that has a coil?

AWOL, you was right, it was a coil problem. It was some kind of disturb passing from the relay contact to relay coil, very strange to me, so relay coil diode was not enough.
My solenoid valves was 220AC current, I changed the solenoids in 24VDC and I fittet diodes to prevent spikes, no more crash.
Of course it's possible protect AC coil, but I already have diodes and DC coils.

Thanks for your help!