is this safe?

Can I set a pin as an input but at the same time digitalWrite it HIGH with resistor in series to collector on a npn?
Then DigitalRead for LOW?
Thanks rawbush

When you digitalWrite a high value to a digital input pin it connects an internal resistor of 20k-50k Ohms to VCC .

If you connect the collector of just about any small-signal NPN silicon transistor to that ATmega input pin and connect the emitter of the transistor to ground, then:

If the transistor is not conducting (base low or open), digitalRead will show a '1' value.

If the transistor is saturated (base connected to a voltage greater than about 0.7 Volts through some appropriate resistor), digitalRead will show a '0' value.

Regards,

Dave

yes the npn (half of optoisolator) is there to send digital input to pin to read. The change is the pull down LOW? I would add a pic of schmatic but have no way to draw it on the computer yet. Also is it better practice to add a resistor in series to the internal anyway?

If the input to the opto-isolator causes it to be "on" the transistor will be saturated and digitalRead will show logic zero.

If the input to the opto-isolator causes it to be "off" the transistor will not conduct and digitalRead will show logic one.

The data sheet for your opto-isolator will tell you what it takes to make the opto-isolator be "on."

Also is it better practice to add a resistor in series to the internal anyway?

No. Just connect the collector to the ATmega pin. Really. With the emitter of the transistor connected to ground, you will never need a series resistor between the collector of the opto-isolator transistor and the ATmega input pin. I mean, not only do you not need a series resistor, in fact you really (really) don't want one.

The transistor is not "driving" the ATmega pin. It is either letting the internal pull-up resistor cause a logic '1' or it is allowing current to flow through the internal pull-up resistor to cause a logic '0' on the input pin.

Regards,

Dave

I would add a pic of schmatic but have no way to draw it on the computer yet.

Great device called a pen or pencil. You draw the schematic on something called paper and then photograph it and post the photo on line.

wow the sarcasim here is great. I thank all of you for your comments, even the ones that point me no were. I will try to explaine what I am looking for a bit better next time. Also will work on a way to get the schamatic into a pic format so I can share that as well. Thanks again rawbush

When somebody can do something like this with MS Paint:

http://www.ubersite.com/m/84167

Saying you don't have a way to draw a schematic on a computer is nothing more than an excuse...

:wink:

I have started using a program called circuitmaker and I have drawn up a really nice schamtic, the problem is that I cant finger out how to export it as a jpeg,gif or any other pic file. I am working on this issue now as I am sure once you guys see it, it will become very clear what I am trying to do. Peace rawbush
Wow those are awsome pics with paint..

Something you can do is hit print screen and paste the print screen image into MS paint or something like that, then save it as a jpeg or whatever image format you pick.

In response to your original questions, I think you'd have to set the pin as output to digitalWrite() to it, then set it as input for digitalRead(). Also, you'll want a resistor in series with the base of the transistor to protect it from current damage from the microcontroller.