Electromagnetic noise?

I'm working on my own version of the CoffeeTronics (http://www.arduino.cc/playground/Main/CoffeeTronics) project and I'm up against a general electronics problem. I'm not very knowledgeable when it comes to hardware and electronics so hopefully someone on here can help me out. I'm testing things out at the moment and what I've got is 120VAC power that's connected to a primary on/off switch. There's a second switch after that one that connects to an IAC5 opto-isolator. The IAC5 takes 120VAC input and produces 5 VDC logical output. The output side of the IAC5 has a power pin which is connected to the 5V pin on my Arduino, a ground pin which is connected to one of Arduino's grounds, and an output pin that goes to a digital input (with a pull-up resistor thrown in there). That's used to turn on and off an LED.

Now for the problem... When the main power is turned off and the Arduino is running, the IAC5's output is low and the LED is turned off. When the power is turned on but the second switch is turned off (meaning the IAC5 still shouldn't be getting any juice), the output goes high. When the second switch is turned on, the output remains high. That didn't make any sense to me so I completely disconnected the IAC5. Even after doing that, I'm getting the same behaviour! Flipping the power switch is changing the output even though it's not connected!! I'm assuming this is electromagnetic interference at work but I'm clueless on how to fix it. Does anyone have any ideas?

Thanks, Mark

Sorry...but trying to follow your schematic description in words is pretty tough. Any chance you can draw a schematic?

Oops, sorry about that! Here are two versions of the schematic:

Stock Modified

I'm including the original just in case it seems like I might have done horrible things without knowing it... Oh and I don't know to draw an opto-isolator or any part of the Arduino so they're just rectangles on mine! :) The most relevant wires for this part of it are the red and black which I've highlighted. The main power switch is labelled IG and the switch that I'm trying to detect is IV. IA and IC are always turned off. There's another lamp labelled SR that normally only turns on when the boiler (R) is running but I've just got it so it turns on when the power switch is on. Note that each switch also has a small indicator light that goes on when the switch is closed. Both of them work correctly even after my rewiring.

My loop code is just this:

if(digitalRead(OPTO) == LOW)
{
    digitalWrite(LED, LOW);
}
else
{
    digitalWrite(LED, HIGH);
}

Thanks, Mark

When the main power is turned off and the Arduino is running, the IAC5's output is low and the LED is turned off.

I think this is the first problem. The IAC5 output should be high with no AC input because the output is open-collector and you say you have a pull-up resistor on it.

And by IAC5 I assume you mean this?

Yup, that's the one. So I guess I need a pull-down resistor instead of pull-up? BTW, when I said I had a pull-up resistor, I should have said that I've got what I think is a pull-up resistor. I don't know for sure whether or not I'm doing it right. I know how to do it in the case of a switch controlling an LED or something simple but I'm confused by the opto-isolator... I had just put a 10K resistor between the power source and the IAC5. So now that you mentioned that problem, I tried moving the resistor to the ground wire but it's still doing the same thing. Am I doing the resistor incorrectly?

Thanks, Mark

No, you should have a pull-UP resistor. 10k between the IAC5 output and 5V is appropriate (or you can enable the digital I/O pin's built-in pullup resistor).

I'm thinking you have a miswiring somewhere. With a 10k resistor between 5V and the IAC5 output, you should measure that output as nearly 5V when there's no AC power. Try it with the output disconnected from the Arduino for starters.

Turning on the built-in resistor did the trick!!! I never knew about that feature. Sweet! So I guess that means I wasn't doing my pull-up resistor correctly. Just for further education, what do you mean by "10k between the IAC5 output and 5V"? Is that different than what I had drawn in the schematic? Physically speaking, I had Arduino 5V-->wire-->10k resistor-->wire-->IAC5 power. The output and ground were straight connections without any resistors or anything.

Thanks, Mark

Physically speaking, I had Arduino 5V-->wire-->10k resistor-->wire-->IAC5 power. The output and ground were straight connections without any resistors or anything.

Ummm...yeah, that was wrong :-) I didn't notice that on your original drawing. That's the wrong place for the resistor.

I guess you don't need it now given that you figure out how to enable the internal pullup, but for the record, one side of the resistor should be connected to Arduino 5V and the other side to the IAC5 output. That's essentially what the built-in pullup is doing.

Oops! :-[ Yes, that makes sense. I've got a long way to go with this stuff.... hehe But anyway, many thanks for your help!!! Maybe I'll be able to drink some espresso again sometime soon.

Thanks again, Mark