Digital Read Freezes Arduino

Hello guys,

I am trying to ready a pin in arduino. It is a part of bigger project but i am having problem here.
when ever i try to digital read a pin which is operated via a 220volt Ac relay. Some times it reads the pin while sometime my arduino freezes.

The Tx light on my arduino freezes on while i dont get any reading in serial.

here is the circuit diagram and code.

int genout = 5; // Generator Out Check
void setup() {
Serial.begin(9600);
pinMode(genout, INPUT); // Generator Out Check
}

void loop() {

int geno = digitalRead(genout);
Serial.println(geno);
}

The TX light is operated by the second chip on the Arduino, not by the main microcontroller. I'm guessing that that's causing trouble, because the code you posted shouldn't crash.

Is it a genuine Arduino or a Chinese clone? What kind of USB-to-Serial chip does it have? An ATmega16U2? Or an FT232? A CH340G?

Pieter

This is just a guess... Try adding a 1ms delay (or longer) in your loop. Your loop is running "full speed" which is faster than you can send-out serial data and I don't know what happens when you do that.

Normally delay() is a "bad thing" but there is a limit to how much data you can push-out of the serial/USB port.

There are ways of accomplishing the same thing without delay() such as reading in a fast-loop, but only sending-out the status once per second, or 10 times per second, or something like that.

DVDdoug:
This is just a guess... Try adding a 1ms delay (or longer) in your loop. Your loop is running "full speed" which is faster than you can send-out serial data and I don't know what happens when you do that.

Serial.println() should in theory block if the output buffer is full:

So that shouldn't be a problem.
I've ran the same code myself on multiple occasions, and I've never had any problems.

Is there any chance that the wiring from the mains operated relay to the Arduino input might picking up electrical noise (interference) from the 220VAC wiring? That could cause just about anything to happen.

Agreed try running your program without the mains attached and see if it still crashes.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you post a picture of your project, so we can see your component layout?

Thanks.. Tom.. :slight_smile:

alexmonro:
Is there any chance that the wiring from the mains operated relay to the Arduino input might picking up electrical noise (interference) from the 220VAC wiring? That could cause just about anything to happen.

without the mains. it is not crashing!!!
but how do i stop the interference now??

the mains wires are not touching the arduino connections at all. still!!

Opto isolator, properly configured of course.

bluejets:
Opto isolator, properly configured of course.

No you already have isolation with the relay contacts so using an opto would not help. This interference is airborne interference and so must be tackled as such.

Hi,

Can you post a picture of your project, so we can see your component layout?

Make sure you 240Vac wiring is away from the other wiring.
Make sure you have no 240Vac wires layed next to other control wiring.

Can I suggest you include “Blink without delay” example in your code to blink the onboard LED, to act as a heartbeat to your code.

Thanks… Tom… :slight_smile:

Grumpy_Mike:
No you already have isolation with the relay contacts so using an opto would not help. This interference is airborne interference and so must be tackled as such.

Pir then.