# Help needed on detecting switch to ground

Hi,

I have a beginner's question. I am trying to read the status of an oil pressure switch in the car. The switch is closed on 0.3 bars and is switched to the ground. When closed there is 12.6V and 22mA on the connections.

I am trying to connect to it and directing the switch status using an Arduino NANO 33 BLE on pin 4. Here is my code:

``````int sensorPin      = 4;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(sensorPin, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("Sensor is OPEN");
else
Serial.println("Sensor is CLOSE");

}
``````

Will that work? Do I need to define the pin as a pullup?

Thanks,

Ofer

Here’s an interesting note about the NANO33

The microcontroller on the Arduino NANO 33 BLE Sense runs at 3.3V, which means that you must never apply more than 3.3V to its Digital and Analog pins. Care must be taken when connecting sensors and actuators to assure that this limit of 3.3V is never exceeded. Connecting higher voltage signals, like the 5V commonly used with the other Arduino boards, will damage the Arduino NANO 33 BLE Sense.

It can be done with a voltage divider to reduce the ~12 to 3.3v

R1 = 47k
R2 = 15k

Thanks. Make sense. The divider with your parameters will generate a Vout of 3.05V and will draw 0.2 mA.

I will try it.

It seems highly unlikely that there would be 12.6V on the switch when it is closed...

Also, that will be the voltage of the battery, which varies between about 11V when discharged and about 14V when fully charged with the engine running.

Anything over 2v will register high.

14v would register as 3.38v should be fine

It wouldn’t be a bad idea to put a 3.3v Zener at the input (reverse biased) to clamp it. Or a TVS which is quicker...

Perry,

I measured 12.6v with a running engine between the sensor wire and ground when the switch is open. I hope its the right way to measure it.

Ofer

This is the current circuit based on the recommendations. I will add a 3.3v Zener to it. I tested it on the desk simulating the car with the resistor and it works great. Today I will test it in the car.

Let us know how it goes. The Zener is a good idea but TVS is faster... it’s just for insurance for the pin in case of spikes. A 14v TVS before the divider would work. Good luck.

Hi Everyone,

The problem I have is that the voltage from the car dashboard is not stable. The sensor is working fine but the voltage from the dashboard is not stable. This is the reason I am developing this system, I want it to work even if the dashboard is not working. So I want to detect when the sensor is connecting the ground even if there is no 12.6V on the input. I thought to do it by using pull-up but I am not sure how will it work if in addition to the pull-up I will have power coming from the battery. Any suggestions?

Thanks,

Ofer

Please explain what you mean by "the voltage from the dashboard is not stable." Give examples.

The analog input cannot detect the difference between the voltage being zero, and the switch closed.

if you look at the schematics below, the car dashboard is putting 12.6 v on the sensor for directing if the sensor is connected to the ground. The problem I have is that this system is not reliable and some times the dashboard is not working and there is no 12.6v on that line. The pressure sensor is still working and above a certain pressure, it is connecting the ground. I want my board to detect when the sensor is connecting the ground in also in a case were the 12.6v coming from the dashboard is not working. Will pull-up on my board in addition to the voltage divider will work?

Let the pressure switch operate a double pole 12V relay, one pole to dashboard, the other to Arduino.

Hi,

Another problem that I discovered is that with the current voltage-divider the pin is never pulled down. Even if both sides of the voltage-divider is grounded, the pin keep read HIGH. Only if I reduce the 15K resistor to around 5K it starts to pull down but then the voltage-divider is not working. Any suggestions?

I would expect the circuit shown to do what you want. I'm surprised you are having problems even if both sides of the voltage divider is grounded. That suggests to me that something else is pulling the pin high, but I don't know what. I would be looking for something else pulling the pin high, something that should not be there. Have you defined the input as INPUT, not INPUT_PULLUP? Also, have you measured the voltage on the pin with a DDM rather than reply on what the Nano reports?

I did measure 3.3 V on the pin when the pin defined as INPUT. I have tested it on pines 5 to 8. I am using the Arduino NANO BLE and I think that it is pull-up by default. I reduced the resistors on the voltage-divider to 15k and 5k the circuit start to work as expected. I also changed the input pin to be INPUT_PULLUP just to be sure that this is how it is defined. Is there a reason for not using smaller resistors?

Thanks,

Ofer

Is there a reason for not using smaller resistors?

Yes, you don't want to draw too much current through the 572 Ohms in the dashboard in case that circuitry thinks the switch is closed. In any case, my concern is that if the input is showing as high when the resistors are grounded then there is something else going on that you don't know about. If that's true it might be a problem later, or not, bit if you don't know what it is then you can't do something about it.

As your external resistors are acting as pull up resistors the internal ones should be disabled.

Only loose about 0.1V across the IRLED...

You can simply use a diode between the pin (with pull-up) and the switch. No divider is needed.