how to avoid ghosting in multiplexing

Hi guys.

I’m leaning multiplexing so I just tried a very simple example which is a 2x2 multiplexing and unfortunately there’s the ghosting problem, any idea to fix it?

It’s okay that a specific LED goes on when you push a button, goes off when release the button.
It’s also okay to push any of the two buttons at the same time, two LED goes on and off.
BUT, when I push 3 at the same time, all leds light up.

Thanks

the code is really simple and nothing special

int output1 = 22;
int output2 = 23;
int input1 = 24;
int input2 = 25;
int led1 = 30;
int led2 = 31;
int led3 = 32;
int led4 = 33;

void setup()
{
  pinMode(output1,OUTPUT);
  pinMode(output2,OUTPUT);
  pinMode(input1,INPUT);
  pinMode(input2,INPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  Serial.begin(9600);
  digitalWrite(output1,LOW);
  digitalWrite(output2,LOW);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

void loop()
{
  digitalWrite(output1,HIGH);
  if(digitalRead(input1)==HIGH)
  {digitalWrite(led1,HIGH);}
  if(digitalRead(input1)==LOW)
  {digitalWrite(led1,LOW);}
  if(digitalRead(input2)==HIGH)
  {digitalWrite(led2,HIGH);}
  if(digitalRead(input2)==LOW)
  {digitalWrite(led2,LOW);}
  digitalWrite(output1,LOW);

  digitalWrite(output2,HIGH);
  if(digitalRead(input1)==HIGH)
  {digitalWrite(led3,HIGH);}
  if(digitalRead(input1)==LOW)
  {digitalWrite(led3,LOW);}
  if(digitalRead(input2)==HIGH)
  {digitalWrite(led4,HIGH);}
  if(digitalRead(input2)==LOW)
  {digitalWrite(led4,LOW);}
  digitalWrite(output2,LOW);
}

How a Key Matrix Work so use diodes.

Don't you think this would be more readable/maintainable ?

void loop()
{
  digitalWrite(output1, HIGH);
  digitalWrite(led1, digitalRead(input1));
  digitalWrite(led2, digitalRead(input2));
  digitalWrite(output1, LOW);

  digitalWrite(output2, HIGH);
  digitalWrite(led3, digitalRead(input1));
  digitalWrite(led4, digitalRead(input2));
  digitalWrite(output2, LOW);
}

Whandall:
so use diodes.

What Whandall is trying to say is use more diodes, You need 4, not just 2, one in series with each switch.

Also, you should really have a series resistor for each led. I know, its just a test circuit. But if you used only one resistor in a real project, they would get dimmer if 2, 3 or 4 of the leds light up at once.

c4a654d9b38556d205e3c5ea157e91b019a37810.png
Of course, a 2 by 2 matrix gives no advantage at all. But for 3 by 3 you use 6 pins instead of 9, and just 4 (actually, for 12 buttons) if you Charlieplex.

PaulRB:
What Whandall is trying to say is use more diodes, You need 4, not just 2, one in series with each switch.

Not just more but more to the point, in the right place - you always need a diode at every switch if you multiplex.

Next is the matter of wrongly using pull-downs instead of pull-ups. This matters not just because you can employ the internal pull-ups (which might not be totally reliable if you have moisture problems in the switches, or long connections), but because it is somewhat safer (particularly if you were not using diodes and did not care about “ghosting”) to “strobe” the rows by switching from input to output and back again (though if you are using internal pull-ups, you also have to write LOW and back to HIGH).

PaulRB:
Also, you should really have a series resistor for each led. I know, its just a test circuit. But if you used only one resistor in a real project, they would get dimmer if 2, 3 or 4 of the LEDs light up at once.

Not only get dimmer, but if the LEDs have significantly different characteristics, and particularly different colours, then only one (the “redder” one) will light up.

Paul__B:
...if you Charlieplex.

@Paul__B, charlieplexing is not suitable, because ghosting is important to the OP. Remember this discussion?

Oh yes, you are right of course!

It only works if you use green LEDs as the diodes!

(Which considering the ready and cheap availability of SMD green LEDs, is actually quite practical!)

Paul__B:
It only works if you use green LEDs as the diodes!

@SynonZelra: Paul__B is kidding, Charlieplexing is not an option for you, forget it was even mentioned.

Thanks guys

I tried to add a diode at every switch and it works.

@Paul__B How can I know if I should use a pull-up or pull-down in a circuit ?

If you have a choice, choose pull-up. Arduino has built-in pull-up resistors, so that can save components. Also, many sensors and switches need to be mounted away from the circuit board, and this is safer with pull-ups than with pull-downs because the wires to the sensor or switch are not connected directly to the positive supply, so there is less danger of an accidental short-circuit.

PaulRB:
@SynonZelra: Paul__B is kidding, Charlieplexing is not an option for you, forget it was even mentioned.

Well, a bit tricky and I would not necessarily recommend it, but why do you think I was kidding about the green LEDs?