Arduino returns HIGH on digitalRead after the input is disconnected

Look at the scheme below, I have 4x 12V inputs (12V max), I used a voltage divider so i can feed 5V into Arduino ports. When I give voltage to one input, digitalRead returns HIGH, but the problem is that after the voltage is 0, digitalRead continues to return HIGH for 6-7 seconds. How can I solve this problem?

This is the code i used for reading from inputs:

#define pushButton1  A0
#define pushButton2  A1
#define pushButton3  A2
#define pushButton4  A3
void setup() {
  Serial.begin(9600);
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
  pinMode(pushButton3, INPUT);
  pinMode(pushButton4, INPUT);
}
void loop() {
  int buttonState;
  buttonState = digitalRead(pushButton1);
  Serial.print("1: ");
  Serial.print(buttonState);
  delay(1);
  
  Serial.print(" 2: ");
  buttonState = digitalRead(pushButton2);
  Serial.print(buttonState);
  delay(1);   
  Serial.print(" 3: ");
  buttonState = digitalRead(pushButton3);
  Serial.print(buttonState);
  delay(1);   
  Serial.print(" 4: ");
  buttonState = digitalRead(pushButton4);
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

Edit: Updated the scheme. And some more explaining, those 4x 12V input from the car, each one does something different, when i have 12V between GND and 1 from the car then i need the arduino to return HIGH from A3 and LOW just right after there is no more 12V between GND and 1 from the car.

My goal is to control some WS2812B leds depending on the commands from the car.

How can I solve this problem?

Add a pulldown resistor to the input or use INPUT_PULLUP in pinMode(), wire the input to go to GND when activated and reverse the program logic

UKHeliBob:
Add a pulldown resistor to the input or use INPUT_PULLUP in pinMode(), wire the input to go to GND when activated and reverse the program logic

Yea but I want to be able to tell them apart, to know which of 4 is high and which are low.

ciuz99best:
Yea but I want to be able to tell them apart, to know which of 4 is high and which are low.

So what is the problem ?
Each separate input will be held in a known state until activated whether you use an external pulldown resistor or the internal pullup resistor for each input

UKHeliBob:
So what is the problem ?
Each separate input will be held in a known state until activated whether you use an external pulldown resistor or the internal pullup resistor for each input

If you have spare time, can you please edit the scheme i have? I don't really know much of electronics. Anyways thank you for your response.

Wire one terminal of each switch together then to GND. No resistors needed
Wire the other contact of each switch to an Arduino pin. No resistors needed
Change the pinMode()s of the inputs to use INPUT_PULLUP to hold the inputs normally HIGH
Change the program to test when an input goes LOW to determine that it has been activated

Attach a wiring diagram showing the voltage divider and the device sending the 12 volt signal.
Using the right resistors in the voltage divider things outgh to work.

Railroader:
Attach a wiring diagram showing the voltage divider and the device sending the 12 volt signal.
Using the right resistors in the voltage divider things outgh to work.

I've updated the scheme with a slightly better one

A pin not connected to anything is termed floating. It will pick up noise from the environment and randomly read high or low (this is what an antenna is, essentially) - you need either a pullup or pulldown resistor to hold it in a defined state when nothing is connected.

If you leave the voltage divider connected, the low side of it will pull the pin low when nothing is connected. It sounds like you are disconnecting both sides of it.

You cannot tell both whether external voltage is high or low, and if something is connected, with a single digital pin.

DrAzzy:
A pin not connected to anything is termed floating. It will pick up noise from the environment and randomly read high or low (this is what an antenna is, essentially) - you need either a pullup or pulldown resistor to hold it in a defined state when nothing is connected.

The GND from the car remains connected even after the voltage is low.

DrAzzy:
You cannot tell both whether external voltage is high or low, and if something is connected, with a single digital pin.

Can you please give me another method of how i should do this? To know when the external voltage is high, and when it is to do something in arduino.

Reply #6 introduces an important concept VOLTAGE DIVIDERS


In your current schematic, you have resistors in series with the 12V inputs - that will limit the CURRENT, but not the VOLTAGE!

12V into 5V does not fit, and the 5V bucket will overflow - and burn out soon after.

You need TWO resistors per input (a divider), and if chosen carefully, they can bias the input toward LOW if it's disconnected.
Remember to keep your resistor values high enough not ot waste too mch current in the resistors themselves!

lastchancename:
In your current schematic, you have resistors in series with the 12V inputs - that will limit the CURRENT, but not the VOLTAGE!

12V into 5V does not fit, and the 5V bucket will overflow - and burn out soon after.

As long as that overflow is small enough - i.e. current limiting resistor large enough - that's no problem. At 10k a 12V signal into a 5V Arduino will result in a current of about 0.65 mA. That's no problem. The inputs with 15k resistor have even less current.

A voltage divider - with second resistor between pin and GND - is better of course, if only because when the power is disconnected the second resistor ensures the pin is held LOW.

In this schematic (that's how it's called, not scheme) when the 12V is disconnected entirely the pin is left floating and will read HIGH or LOW randomly. When the incoming signal is low (grounded) it'll be held low that way.

What did I miss? Why is there a 10K resistor from the Arduino Ground to the car ground? If you are mounting this into a vehicle, you want the grounds to be connected together or you are asking for difficult to diagnose problems later.

If I understand correctly, there are four switches from (1), (2), (3) and (4) to the vehicle +12V.

Here's a circuit that I came up with:

That are highly unusual resistor values; and the resulting voltage is 3.3V. A bit low. I'd target ~4.5V.

22k & 12k or 22k & 15k would be more common values, giving 4.2V resp. 4.9V on the Arduino input.