How do I get a stable input on a digital pin for arduino?

I know that using the digitalRead() function will return a random number if the pin being read is not connected to anything. I need a stable 1 or 0, not a random input.
I tried to use input pull-up (pin 5), but I am using a GPIO pin as the ground (pin 6) as I can't use the ground pin. The input pull-up overrides the LOW state of pin 6 so I can't get pin 5 to read LOW. Any way to get the Arduino to return a stable input?

Say what ? :woozy_face:

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

I am using a generic keyboard switch. there is not schematic, I am just connecting the switch via a wire. Would I need to use an external resistor?

The schematic is in your head and output is paper and pencil.


I can't use pull-ups because I am making a matrix of switches. I am testing 1 switch because the matrix didn't work. the pull-ups will just override the LOW of the pin that is not being read

This is a scaled up version of the matrix I am making. as you can see, I can't connect the switches to ground.

That's not true:

Col1-4 are going to be inputs (internal pull-ups).

Row1, or 2 or 3 are made low in order to scan the keys.

What is the problem ?

@klzsh, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

Not unless you use a switch to pull it to ground of course.

The internal pull-up pulling the input HIGH is in fact, a very stable input.

A couple of questions:

Why Input pull-up?

Why are there external 10k resistors of you are using the internal pull-up?

What is the significance fo using a 10k external pull-up resistor?

This is a generic schematic of a switch matrix with ghosting diodes.

The 10k resistors shown in the schematic are representing the pull-ups.

Replace them with the internal 50k pull-ups or use external 10ks, it’s up to you.

The internal pull-ups are free though. :wink: .

To turn on an internal pull-up resistor:

pinMod(myInputPin, INPUT_PULLUP);

10k is a common resistance to have in a kit.

It happens to be low enough value to work as a good pull-up or a pull down.

Select too large a value, these become weak and when they approach >1Meg the pin can start to look like it is floating again.

The Keypad library takes care of all of that. You tell it what pins are column pins and what pins are row pins and it switches the mode on them as needed. No need for external pull-up or pull-down resistors.

I tried to use the keypad library with the matrix, I put the pin in correctly, but I got no output when I pressed a key. Even when I tried it with this new schematic, it didn't work.

Have you got the diodes the correct way round?


Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.