If the pin with the 10K pullup also has a weak pulldown resistor to prevent the pin floating if there is nothing connected on it then setting the pin to input and reading the pin state should give low if no wind sensor connected and high if it is connected.
This all depends on how the wind sensor works as it might not always be high.
My understanding of the wind sensor is that it works with a reed contact. I use an interrupt to detect if the pin is high - I count the no of highs per second and can thus relate this number to the wind speed.
If the sensor is not turning at all then it stays low.
What would be a "weak" pulldown resistor value? 1 K? Or less?
What would be a "weak" pulldown resistor value? 1 K? Or less?
No that would be strong, weak would be 30K.
However their is no difference in the reading of a sensor not being connected and a sensor connected and no wind. The sessor sends two states high and low, and the digital pin can only sense high or low, so I don’t think it will do what you want.
What are you going to do with the information sesnsor plugged in or not?
This question has been asked many times before and often there is nothing useful a program can do with the information.
I would like to build serveral devices with some sensors, but only one device will be equipped with a wind sensor. I'd like to share the same software and I would like to detect if a wind sensor is attached or not.
Of course I could do this via configuration but in this case there is always the possibility of a misconfiguration (sensor data is not being read because of a lacking configuration or due to a wrong configuration wind sensor data will always be 0 km/h because no sensor is attached.
So a hardware solution would be preferable but if there is no solution then I will go this way.
A reliable solution would be to use a second input, connected to GND through a strong pull-down (1-5k). So you have create a connector with three pins: GND, signal and the detection.
Now if you set the pinMode to INPUT_PULLUP the internal pull-up resistor (typically about 30k) is too weak to pull the pin >1.5V ( 0.3 * Vdd according to the data sheet, so 1.5V for a 5V Arduino), so a LOW reading on the pin means sensor connected, a HIGH reading on that pin means sensor not connected.
thanks for your suggestion. Unfortunately I am a newbie in electronics, so I do not quite understand your idea.
My sensor only has two wires - one goes to GND, the other to a digital input.
So your idea is to use a second input and to connect this input to GND via a pulldown resistor. If I understood correctly I do not have to connect the input with another element, it's just the resistor connected to GND.
For the final solution I'd like to use an ESP32 - how would I have to change the value for the resistor if I used an ESP32 with 3.3 V?
Connect your sensor to a 3-pin connector, connect the third pin through a resistor to the GND pin. Then you can use that third pin to detect whether there's a connection.
The internal pull-ups of the ESP are about the same value as those of the Arduino so no change there. It's a voltage divider so it's the ratio that matter rather than the actual voltage applied.
But what do I on the unit with the wind sensor? The software should stay the same - if there is no wind the software would report nothing, even if the unit had a wind sensor.
How does the software know what 0 km/h means - no sensor or no wind? I'd like to distinguish between the two states.
Set a static flag if the wind speed ever goes above 0 and only start/continue to report wind speed if the flag is set.
A newly powered up device with a wind sensor will not report anything for wind speed until there is wind detected and then it will always report wind speed, even if it's zero.
A newly powered up device without a wind sensor will not report anything for wind speed and it will never report wind speed.
Although I like the answer Riva gave, you never use the loop if there never is a pulse from a sensor. or if you ever get a pulse, you enable the loop.
I am not sure if this idea will work, but you might be able to add a small capacitor
set the pin to output, charge the cap
set the pin to input. pull-down will drain the cap.
no physical device connected means no cap and the input will be zero when you change the pin state.
if there is a cap, the voltage will be high until the cap drains, meaning a time lag from the charge.
I am wearing my big target shirt. Anyone who wants can shoot holes in this idea.
If you set a pin to OUTPUT, HIGH and have nothing connected, I expect it to remain HIGH for at least a short while after you set it to INPUT. After some time the state will become undetermined (floating pin). Now if there's a cap connected to the pin, it will remain HIGH for a long time as it only very slowly loses its charge.
Comment #2 is strange: if it's indeed a reed contact, connected to GND on one side and to Vcc through a pull-up resistor on the other side (which makes total sense), the line should be HIGH always, with pulses being LOW. So the exact opposite. A RISING interrupt will then give the end of the pulse, a FALLING the start of the pulse. Which one you use doesn't normally matter. Let's assume the described wiring is correct, so it's just a reed contact between GND and the pin.
To make the cap trick work: cap between GND and the pin, set pin to LOW, then switch to INPUT_PULLUP and immediately check the pin state. If there's a cap there will be a short delay before the pin switches to HIGH (1/2-1 times RC - so a 100 nF cap with the ~30k built-in pull-up gives about 2 ms of LOW before it's HIGH. If there's no cap, the pin will be pulled HIGH pretty much instantly (probably within 1-2 clock cycles). A 5-10 nF cap would work as well, pin remains LOW long enough, 100-200 µs or so, to detect this.
That cap will not affect the measurement (unless the pulses come in extremely fast) as even a very short contact between the pin and GND will discharge the cap. Though a small current limiting resistor (200Ω or so) may be in order to protect the reed contacts.
Nice idea! Maybe I can apply it to my water flow sensors, I'm actually using the first pulse as sensor present signal (there is supposed to be flow all the time anyway). Those sensors work slightly different though, as there are three wires, no pull-up resistor needed, and a 50% duty cycle. Driven high and low.
If your going to add extra hardware then why not just have another wire on the GND wire of the wind vane that will always be low no matter the state of the reed switch, connect this to another Arduino pin with internal pullup. If it high there is no device, if it's low then the device is plugged in.
Capacitors are okay but may need careful selection so not to interfere with pulses at all wind speeds.
Riva:
If your going to add extra hardware then why not just have another wire on the GND wire of the wind vane that will always be low no matter the state of the reed switch, connect this to another Arduino pin with internal pullup. If it high there is no device, if it's low then the device is plugged in.
Capacitors are okay but may need careful selection so not to interfere with pulses at all wind speeds.
pin count.
but your idea of a part of the code that gets executed only when a signal is present seems to fit the requirement based on the extremely limited information presented.
a 'wind sensor' possibly a rotating anemometer, might be 30 RPM with a magnet and reed switch.
without a data sheet we will never know.
another hardware solution is to use an AI pin. test for resistance in setup()
almost all pins on the ESP32 are A/I capable.
use a voltage divider in hardware (power waste if battery powered)
test for value, the voltage would be less than the low value trip on a digital input.
if AI reports a value, then set WindExist flag to 1, set pin to read digital input.
Trying to detect if a sensor (in my case a wind sensor) is connect to a specific digital input. In other my words I try to differenciate the two cases:
a) device has a wind sensor connected to a specific input pin
b) device has no wind sensor connected
You still seem not to get it. A digital pin can only report back one of two states, a high or a low where as you want to report three states.
If you use an analogue input then you can detect more states but you need to know how the output of your sensor is driving the analogue input. When you know this you will know if the biasing of the input with a voltage divider is going to work and what value of resistor to use. You connect them from input to 5V and from the same input to Ground.
Many sensors pull down or up only. For this trick to work you need a sensor that both pulls up and down, and you need an analogue input pin.