The problem started when I connected a switch between the ElectroMagnet and the relay module. Theoretically it has no connection to the Arduino but when I press it (it's in normally closed mode) and the ElectroMagnet is turned off the other switch, the one connected to the Arduino is toggled and switches on whatever it should turn on. When I turn off the Magnet using the Arduino's digital out everything works fine.I believe that I have a debounce issue caused by reverse voltage coming from the ElectroMagnet whenever I manually turn it off.I can try enlarging the debounce delay (currently 50 milliseconds) and perhaps it will solve the issue but I would like to get to the route cause and try to prevent the reverse voltage coming back to the Arduino.
I connected a Schottky Diode (1N5819) in series to the electro magnet: between Vcc-in of the magnet and Vcc-out of the relay module and it didn't help.I connected a Schottky Diode (1N5819) in parallel to the electro magnet: between Vcc-in and GND and it didn't help.
I don't see any common gnd between the power supplies and Arduino.
what is the problem ?I don't understand it the way you described it in the quote above.Yeah, that's a good point but since he has the Opto-coupled relays, if he wired it correctly , he should not need a common ground.
By the way, as I mentioned in my previous message, I did try to connect the diode in parallel to the Magnet's coil but it didn't help. But you are right, I don't have much experience with diodes and that's why I'm seeking other people advise here.
First of all , you are breaking the first rule of power distribution.You are powering your microcontroller from the same power supply that powers the inductive coil (the electromagnet). This is a big no no. The arduino should NOT be powered by the 12V supply for TWO reasons:1-It is the electromagnet power supply2-12V is too high for an input to the Vin of the Mega. It causes the onboard 5V regulator to overheat.The Mega should be powered by the 5v supply using the 5V pin on the Mega.The 12V supply should ONLY power the maget.You should have separate GNDs between the two supplies because you have an opto isolated relay board.I am not really sure how everything is wired because your image file is such poor resolution.Post a larger file size (1 Mbyte) .or post a wiring list (preferred ) that references pin labels . (like N.O., N.C. COM, etc)
There is no ground wire from E-magnet to 12V supply.
These opto-isolated relay modules take a common-anode supply from Arduino 5V, andeach input is pulled low by the Arduino to operate the opto-isolator. If you connect the grounds you will bypass the opto-isolation completely, so don't do that!