MID400 Optocoupler - things I learned

I’d appreciate comments from folks who’ve used the MID400… have I hit the nail on the head or am I missing something?

I needed to detect an a/c 120 volt or 24 volt signal and trigger a digital pin high (3.3 volt) or low when the signal was or was not detected. After discussing it on this forum I decided to try the MID400 Optocoupler. I just got it working and I learned a few things. Thought I’d share them with the community and perhaps save folks some time in the future.

When I first wrote my code I was using a jumper wire from 3.3v to pin D0 to simulate the on/off condition. I struggled with it for a while… I couldn’t get it to detect the low / off condition. Adding a pulldown resistor between D0 and ground got that working. I later learned about the input_pulldown pinmode but left the pulldown resistor in there for good measure. I wasn’t having much success when I tried to get the MD400 working. The pulldown resistor was the problem… Lesson #1: don’t use a pulldown resistor with the MD400.

It took some tinkering to get the MD400 to work. The application notes put a 4.7k resistor between pins 6 and 8 on the chip… It didn’t make any sense to me why that resistor would be there. Lesson #2: The MD400 inverts the logic… in other words, when it senses a signal the output goes low… when there is no signal the output is high (the opposite of what one might expect). Now that I know that the 4.7 k resistor makes sense… when the signal is off the MD400 is on… with pull-up voltage provided through the resistor. When the signal is on it connects the 4.7k resistor to ground causing the signal to go low on pin 5.

Lesson #3: The MD400 calls for 5 volts on pin 8… but 3.3 volts from the particle is enough to make the chip work.

Here is how I hooked it up:
First… there is a little round marking on one of the corners of the chip. This signifies pin #1… and the pins are numbered counter-clockwise 1-4 on one side and 5-8 on the other. The big input signal (120 volts or 24 volts) comes in through 22k of resistance to pins #1 and #3. (24 volts will require less resistance… 5.6k to 6.4k) I powered the chip with 3.3v to pin #8 and ground to pin 5. The 4.7k resistor goes from pin 8 to pin 6. A jumper from pin 6 to the particle provides the on/off signal I’m looking for. (and it works).

I had to change my code… swapping “on” for “off” and “HIGH” for “LOW” because, as I said earlier, the output of the chip is inverted. The only other issue I’ve found is that the 1/4 watt resistors (I used two 10k and another smaller one in series to get 22k) coming in from the big signal are getting hot. A 1/2 watt 22k resistor would probably be a better choice for long-term reliability. Beyond that, everything is working fine and I can go ahead and develop my prototype.