Way to correct input

Hello

I have a question which way is better for implementing pinMode INPUT.
I assume the switch is on a +/- 70 meter long cable for the door bell.

in the attachment I put 2 diagrams.

Best regards and thank you for reading my question.

pytanie1.jpg

pytanie2.jpg

Here is a guide to post pic's on the forum. Simple guide for inserting images in a post
OP's pic's
pytanie1.jpg

pytanie2.jpg

Because of the distance involved you will likely have to use the first one. the circuitry and higher voltage will help it resist noise.

first one(12V) is wrong:
a) 70 meters of cable is an inductor, adding Capacitor and switching it will cause osculations, possibly huge voltage spikes.
b)11mA trough BC547 base is too high.

here is preferred circuit:

r1 and r2 create 40mA loop.
voltage across r2 is ~0.9V

r3 limits base current to 2 mA, enough to switch 200-300mA with bc547.

If possible use twisted pair cable and avoid running it parallel with mains power cables.

Good Catch!

Well, there are a couple of things here.

The second circuit with a direct connection to the ESP would be most unwise as all sorts of voltages may appear on the long lines and the ESP would be easily damaged. Using a transistor for isolation is wise. An opto-coupler would be even better.

12 V is unnecessary - more voltage does not necessarily make things better. As switches bounce, it will be necessary for your code to ignore erratic input from both contact bounce and induced interference. Using code is vastly more effective than adding capacitors. :sunglasses:

It is a bad idea to connect your 12 V - or 5 V - power supply line out to the button, an accidental short to ground would cause obvious trouble. One wire should be ground, the other to the input conditioning circuit. A 1k pull-up to 5 V, presuming 5 V is the available supply, and a 10k between that and the transistor base.

Mind you, the transistor in this situation would not care if you did feed it 11 mA to the base. :roll_eyes:

I've used this circuit for digital inputs in harsh environments.

D5, 5V TVS diode. Clips any damaging + or - voltage coming in.

C5 is the first line of defense for high frequency pickup and some ESD.

R2 limits the current to D5 in the event of the input being connected to a (relatively) high voltage source.

R3 & C6 limit the current to a 3.3 input device and stops any transients that made it this far.

There are literally 100's of solutions to this problem Your transistor problem will work as well but with some added capacitors for high frequency pickup.

Note here "track" would be from your switch.

Regarding twisted pair for your 70 meters. Definitely would help, good suggestion.

Sasquatchv:
first one(12V) is wrong:
a) 70 meters of cable is an inductor, adding Capacitor and switching it will cause osculations, possibly huge voltage spikes.

Its a transmission line, not an inductor. Add 100 ohm resistor in series at each end and it won't misbehave.

b)11mA trough BC547 base is too high.

No, its fine for a switching circuit.

here is preferred circuit:

r1 and r2 create 40mA loop.
voltage across r2 is ~0.9V

40mA is a lot for a simple switch signal loop like this, 10mA is plenty I think.

r3 limits base current to 2 mA, enough to switch 200-300mA with bc547.

Strictly speaking that's enough to switch 20 to 40 mA. Transistor current gain only relevant to the active region, switching only uses cutoff and saturation regions, for saturation the gain is in the range 10 to 20 for most devices.

If possible use twisted pair cable and avoid running it parallel with mains power cables.

Solid advice, seconded.

A ~100nF capacitor across the cable is the best protection against noise spikes, which might otherwise start damaging the transistor. The low value of the 22 ohm resistor is going to protect well too though, although I'd raise the whole circuit impedance up somewhat to reduce current levels. 10mA is a reasonable value.
Opto couplers are great for long cable runs - much more likely to withstand a lightning strike for instance.