I'm building a door lock that has a 4x3 keypad for entering the lock password.
While prototyping it works great.
I tried to connect the keypad with long wires (1.5 meters) so I could mount the keypad outside the door but I strated getting wonky readings from the keypad if any.
I'm guessing the wires length messes up the reading.
What can I do to resolve/bypass this issue?
The length of the wire has probably an impact on noise and you need to explore a lower value for the pullup resistors on the switches as a low resistor value is called a strong pull-up (as more current will flows) but this is more sensitive to noise. Don't go below 10kΩ as the combined pull-up
Connect it to the MCU
Turn on a pin going into the keypad
Press a button and measure the output voltage at the MCU side of the other wire
See if you are getting something close to 3.3V
You could also try slowing down the scan rate while reading the keypad,. Another option would be to move the node mcu to the keypad and run the long wires to the lock mechanism, that may be more reliable.
For each row pin you add (in parallel to going to the keypad) a resistor and then go to 5V
5V
|
|
R2
|
|
Pin ————————— row of KeyPad
The equivalent resistor when the R1 built in pull-up resistor is activated will be
Req = R1 x R2 / (R1 + R2)
If R1 = 50K and R2=2.2 K Ohms then for example you have Req = ~2K
(If R1 is very large compared to R2 R1+R2 is almost like R1 and thus the formula is basically R2 ➜ that’s how an external resistor can overrule the internal pull-up)
The capacitor would be similar but go to ground
5V
|
|
R2
|
|
Pin ————————— row of KeyPad
|
=== 2 to 10 nF
|
GND
It’s there to absorb electromagnetic perturbations that your long wire is likely submitted to (it’s like an antenna)
EFIT: it’s 5V if your arduino uses a 5V for the pins otherwise use 3.3V or whatever is suitable for the platform. This is important especially if your MCU’s pins are not 5V tolerant
As a SW guy, I'm leaning more toward SW solutions...
I've found a tutorial that connects a keypad to a 8pin digital to I2C expander module and then useS the keypad_I2C library instead of the regular one.
Will the I2C protocol work with 1.5 meter wires?
It only need two wires instead of 7 the keypad currently uses.
Also, in one other post I read that you can use a cat6 ethernet cable that has 8 wires, twisted in pairs and it is shielded. Will that work?
In all the code samples using the keypad library I didn't see it defines the pins I pass to it as INPUT_PULLUP pins.
I haven't checked if this library itself explicitly defines it in the code.
void Keypad::scanKeys() {
// Re-intialize the row pins. Allows sharing these pins with other hardware.
for (byte r=0; r<sizeKpd.rows; r++) {
pin_mode(rowPins[r],INPUT_PULLUP);
}
…
So the rows are set as INPUT_PULLUP
Shielded cables are good against EMF - so this can’t hurt
I2C has been invented for communication between two Integrated Circuit - so usually close together. It’s known to work for longer distance and pull-ups are also part of the answer
1. What do we do? We simply buy a Keypad and connect it with the DPins of the UNO as per Fig-1. Do we connect any external pull-up/pull-down resistors? No!
Figure-1:
There are two types of Keypads -- walking 0 and walking 1.
The Keypad.h Library supports walking 0 Keypad; where, the column lines are sequentially made LOW and then the row lines are sampled to detect the unique (x, y) position of a closed key.
Therefore, the row lines must be terminated to 5V so that when a LOW appears on a column line, the corresponding row line will assume LOW state upon closure of the key located across the said row-column (for example: R1-C1).
So, the row lines are indeed connected with internal pull-resistors and is done by the Keypad.h Library which has peen pointed in post #18 by @J-M-L.
wow, great detailed info.
I'll try a couple of thing:
First, I'll try using a CAT6 cable that is shielded and twisted.
Second, I'll try adding resistors to the row pins like suggested above.
Unfortunately, it will have to wait for the weekend as it's the only free time I have to play with this new hobby.
I will definitely keep you updated with the results.
I'll also explore the suggested option of moving the controller closer to the keypad, this will place the MCU in a hard to reach place and make maintenance harder but if the other options will fail I'll have no other choice.