Trying to use an arduino as an endstop on a 3D printer

I have some random stuff connected to an arduino nano, which is connected to an endstop connector on a RAMPS (equivalent). There are three lines from the RAMPS, - +5, ground and "signal" which I believe is +5 that should either be connected to ground or not, sometimes through a simple mechanical switch. There must be current limiting, because the normal thing is that there are unpowered endstops that are simple switches that connect signal to ground or not. Under the RAMPS is an arduino, but I do not think that this is a matter of hooking one Arduino to another - because the RAMPS expects to communicate with powered and unpowered endstops.

I want to make my Arduino Nano look like an endstop to the RAMPS board. I want to be able to change one of my digital pins and make the RAMPS see "low" or "high". As far as I can tell, "low" is "open" and "high" is shorted, or equivalent.

I had decided that the best simulation of a simple switch that I could do with an Arduino Nano was to use a digital output pin in output mode - at "HIGH" it should stop the passing of current because there would be +5V vs. +5V. At "LOW" it should sink enough current to pull the line down. But I was wrong. High didn't stop current from passing. No matter whether my output was "low" or "high" it always seemed as shorted to the RAMPS.

I would expect that in low state, it would sink the 5 volts from the other end. In fact, I see that in low state, I am getting about 0.359 volts (pin to ground) (I presume my meter is not that accurate) and in high state I get a short excursion to 3.5 volts before it settles down to about 1.6 volts.

I became very confused.

Disconnecting the signal line changes the input to "low". But then I noticed that adding a 750k resistor made the input "low" and the junction point always read 5 volts.

So I finally decided that what was happening was that I had too much loss through the Arduino's digital output at all times - it seemed to be shorted "enough" to the other end to be a closed switch. Maybe the impedance was higher than I thought on output.

So I thought, "Maybe there should be some more impedance in the circuit." I found (amazingly) my resistor decade box. I tried various values and finally determined that 3k of resistance worked - that is, when the Arduino is "low" it sinks enough that the thing at the other end registers high, and when it is high, it fails to sink enough current that the sensor reads low.

Is this the right way to do this? If I send this to someone else, are they likely to need a pot (hooked up as a resistor) to adjust this to their system, or is it likely to work in many places if I pick a good central value on my system?

Is there some other way I should do this? I am trying to be cheap - my goal is to hold total parts cost under $12 and I think I am up around $10. But reliability and portability are the most important things.

Sorry about the wall of words, but I wanted to be sure my explanation was complete.

I can't say I understand what you are trying to achieve.

However if an I/O pin on Arduino A is set with pinMode(pinNumber, INPUT); or pinMode(pinNumber, INPUT_PULLUP); then Arduino B can safely use a pin set as OUTPUT to pull the input pin on Arduino A HIGH or LOW. There also needs to be a GND connection between the Arduinos.

...R

The problem is that it is not just an Arduino - Arduino connection. There could be anything on the other side - the RAMPS is just one example. I need to look at the interface and make the interface work.

The interface is three wires: VCC, Ground, and "signal". The simplest thing that connects to this is a switch that either shorts signal to ground or does not short signal to ground, with no connection to VCC.

I have no control over the thing that is on the other side of the three wire connector. I need to treat the connector as a black box.

I am trying to build a device that connects to that standard connector. I want my Arduino to act like the switch - and either sink the "sensor" line to ground or not.

So I have connected the 5 volt VCC line to the 5 volt pin on the Nano. I have connected the grounds together. There seems to be enough "juice" on vcc to run the Arduino Nano.

But when I define a pin as "output" and I connect it to that signal wire, it always registers to the black box as a short. It does not matter if I have the pin output as high or low. There always seems to be a sink through the output pin, and the voltage at the junction goes from under a volt (when the pin is "low") to a volt and a half with a short burst of 3.5 volts (when the pin is set to "high").

This is based on readings from a digital voltmeter (red) connected to the junction of "signal" and the Arduino pin with (black) connected to the common ground.

When the wire floats I see 5 volts relative to ground, same as VCC. When I connect it to a digital output pin on my Arduino Nano it always pulls the voltage down far enough that the other side thinks that the wire is shorted to ground. This happens whether the output pin is high or low. When it is low I see less than a volt, when it is high I see a pulse of 3+ volts and then it drops to about a volt and a half. I was surprised at this, I thought that it would be the same 5 volts coming from the output pin and the other side and that there would be something that looked like an open connection. But that is not the case.

I was able to get it to work on one particular black box by inserting a 3k ohm resistor between the wire and the Arduino Nano's output pin. That provided enough impedance that the other side would see the swing - that the swing happened across whatever threshold the black box was using.

But I believe that will be "fragile". That is, there will be some small changes on the black box side that will cause this 3k value to be wrong.

I could probably get around this by using a variable resistor, (two legs of a pot, say). But there has to be a better way to do this.

One thing I have thought about is, instead of trying to insert the right resistor into the circuit, perhaps I could just use a smaller current limiting resistor, and then I could switch the mode of the pin from input to output, so that in input mode it would be a high impedance connection, and in output it would be this low impedance thing that sinks the voltage well enough. I have not tested this, I am not even sure that it is acceptable or will work without breaking the Arduino.

So I am hoping that someone can help me make the Arduino simulate a switch.

Does this make it any clearer?

Brasshopper:
The problem is that it is not just an Arduino - Arduino connection.

How is an Nano to a RAMPS board not an Arduino to Arduino connection?

If you don't really mean a RAMPS board why mention it at all?

An Arduino I/O pin can sink or source a max of about 40mA and 20mA would be a more sensible limit. And the voltage at an Arduino I/O pin must be between 0v and 5v (assuming a 5v version of a Nano).

If you want useful advice you need to describe what you are actually trying to do. An Arduino is not the same as a simple switch. There are many situations in which it cannot substitute for a switch without external components - a relay, for example.

...R

Because it is not just an arduino output connected to an arduino input. The ramps supplies 5 volts and the arduino on the ramps reads the 5 volts, the Nano where my sketch is running does not either supply the 5V or not, it has to sink the 5V or not,. It is not an ordinary connection where you just connect one Arduino pin to another and one side asserts 5V and the other side sees it as high or vice versa. The 5V supplied by the RAMPS does not have the same electrical characteristic as 5V from an Arduino output pin, far as I can tell.

I don't know. Maybe it is, in fact, an input pin with INPUT_PULLUP set so that there is a high impedance path to 5V and you can just freely short that pin to ground without doing anything other than changing the digital state. But, if so, why could I not make pin 12 work when I set it to output and low or high?

So someone suggested an N-channel enhancement FET to actually do the sinking of the current from the RAMPS. I got one, and it seems to make no difference. And I checked the gate voltage. This is what I am seeing:

The +5v pin is actually running at 4.75 volts. But the output pin changes from very near zero (a few millivolts, probably in the noise of my meter) to about 1.85 volts. It never gets to 4.75 volts or 4.5 volts or whatever I should expect it to get to. This is less than the voltage of the arduino. More importantly, it is less than the voltage that the N-FET needs to start conducting. I was sort of surprised. I have looked at the code, and verified that pinMode(pin,OUTPUT) is set, and then I do digitalWrite(pin,LOW) or digitalWrite(pin,HIGH). Simple stuff.

So, what could be causing this? Something I am doing? Bad pin? I am trying to use D12. (OK, it would have been stupid to post without testing this, so I changed to pin D7, and that one works. I see 4.75 to a few millivolts, and when I connect the pin to the gate on the N-FET, it works as expected, draining the voltage from the RAMPS to ground. So far, the changes have been reliable.

Is it possible I blew out the first output pin I was using? I have to admit I experimented with it quite a bit. I tried to use it as a sink for the voltage (I thought it was supposed to be able to sink a small current in low and not sink it if it was "high" - and then I got this to kind of work with a 3k resistor in series, but it was not reliable. This time, I connected the gate to the pin through a small (200ish) ohm resistor but not the first time, still, well, I read some other posts and this is why I added the ability to select the output pin to my sketch.

In any case, I am not trying to do anything other than access the serial port, analog ports A0-A3, (for input) and one digital port for output. I access the millisecond clock to do some timings, and I access the EEPROM to save settings from one run to the next, so that I don't have to reflash for minor changes in sensitivity and the like. I am not using anything that uses port D12, like SPI or anything like that.

I guess I didn't see anything that I did that would have been wrong, but I have to admit I probably didn't take the proper static precautions - but I live in South Florida, static is almost never a problem here.

I'm sorry but I can't make sense of Reply #4. I don't mean to be unkind but I got completely lost before the end of the first paragraph.

Make a simple diagram showing how everything is connected - a pencil drawing is probably best. Then post a photo of the drawing. Verbal descriptions of circuits are too easy to misunderstand.

And it is much easier to help if you respond quickly while I still remember your project.

...R

Brasshopper:
Is it possible I blew out the first output pin I was using?

Yes. Test it independently.

I could definitely be wrong but it sounds like you're trying to simulate a HIGH signal just by throwing out a single wire from an I/O pin, but not having a complete circuit for that to return to the Arduino.

Perhaps an opto-isolator would solve the ambiguity problem.

Paul