Issues with 3x3 Matrix of Reed Switches

Hi,

I am fairly new to electronics and am having some issues with a project I am trying to build that requires a 3x3 matrix of digital reed switches triggered by magnets.

I think I am seeing some issues with the circuit. The output is not as expected. When there are no magnets on the board, the code featured below prints:
1 1 1
1 1 1
1 1 1
And when a magnet is detected on top of a sensor, the state of it turns to 0. However, there are some instances where placing a magnet next to a sensor either doesn't trigger the change from 1 to 0, OR it may trigger others in the same row to turn to 0 even if there is no magnet present. I assume from that that the issue is not in the code, but with the circuit I have built. I may be missing some components like resistors or diodes somewhere maybe?

Here is a schematic and my current code for reference:

#define rows 3 // define number of rows
#define columns 3 //define number of columns
 
int rowpins[rows] = {4, 3, 2} ; // set row pin numbers
int columnpins[columns] = {10, 9, 8} ; // set column pins
int currentState[rows][columns]; // array for current state during pick
int previousState[rows][columns]; // array for comparing state during pick
int reading[rows][columns]; // array for pin read value
 
void setup () {
 
  Serial.begin (9600); // Start serial comms
  delay (2000);
  initialise (); // initialise function
 
}
 
void loop () {
 
  initialise();
 
}
 
void initialise () {

  Serial.println ("Board initialising");
  Serial.println (" ");
  delay (2000);
  Serial.println (" ... ");
  Serial.println (" ");
  delay (500);
  Serial.println (" ... ");
 
  for (int i = 0; i < rows; i++) { // Set rows to LOW INPUT
    pinMode (rowpins[i], INPUT_PULLUP);
    digitalWrite (rowpins[i], LOW);
  }
 
  for (int j = 0; j < columns; j++) { // set columns to INPUT with PULLUP
    pinMode (columnpins[j], INPUT);
    digitalWrite (columnpins[j], HIGH);
  }
 
  for (int i = 0; i < rows; i++) {
    pinMode (rowpins[i], OUTPUT); // set row to LOW OUTPUT
    for (int j = 0; j < columns; j++) {
    reading[i][j] = digitalRead (columnpins[j]); // read board positions and set for initialise
    currentState[i][j] = reading[i][j];
    previousState[i][j] = reading[i][j];
    }
    pinMode (rowpins[i], INPUT_PULLUP); // set row back to INPUT
  }

  delay (500);
  Serial.println (" ");
  Serial.println ("Current Layout:");
  Serial.println (" ");

  for (int a = 0; a < rows; a++) {
    for (int b = 0; b < columns; b++) {
      Serial.print (reading[a][b]);
      Serial.print ("  ");
    }
    Serial.println (" ");
  }
}

Nope. It seems to me that you are incorrectly establishing the state of the pins.

Try this program.

const byte
rows = 3,
columns = 3,
rowpins [ rows ] = { 4, 3, 2 },
columnpins [ columns ] = { 10, 9, 8 }
;

boolean
currentState [ rows ] [ columns ],
previousState [ rows ] [ columns ]
;

void initialise ()
{
  Serial.println ( "Board initialising\n" );
  delay ( 2000 );
  Serial.println ( " ... \n" );
  delay ( 500 );
  Serial.println ( " ... " );
  
  for ( byte n = 0; n < rows; n ++ ) pinMode ( rowpins [ n ], INPUT_PULLUP );
  for ( byte n = 0; n < columns; n ++ ) pinMode ( columnpins [ n ], INPUT_PULLUP );
  
  for ( byte r = 0; r < rows; r ++ )
  {
    pinMode ( rowpins [ r ], OUTPUT ); digitalWrite ( rowpins [ r ], LOW );
    delayMicroseconds ( 20 );
    for ( byte c = 0; c < columns; c ++ )
    {
      previousState [ r ] [ c ] = currentState [ r ] [ c ];
      currentState [ r ] [ c ] = digitalRead ( columnpins [ c ] );
    }
    pinMode ( rowpins [ r ], INPUT_PULLUP );
  }
  
  delay ( 500 );
  Serial.println ( "\nCurrent Layout :\n");
  
  for ( byte r = 0; r < rows; r ++ )
  {
    for ( byte c = 0; c < columns; c ++ )
    {
      Serial.print ( "\t" );
      Serial.print ( currentState [ r ] [ c ] );
    }
    Serial.println ();
  }
}

void setup ()
{
  Serial.begin ( 115200 );
  delay ( 2000 );
  // initialise ();
}

void loop ()
{
  initialise ();
}

Happy Holidays!

1 Like

Thanks for your help.

I am still seeing the same issues as before after trying the code you posted here.
The code seems to work totally fine if I have one magnet present in the grid, the corresponding grid point will change from 1 to 0 just fine.

However, once I put more than 1 magnet in the grid at a time, the readings start to get wonky and grids that don't have a magnet present change from 1 to 0. Any idea why I might be seeing this? If the hardware aspect is fine, maybe it's the magnets?

Hi @danielleamya
Simplify, use the library Keypad.h

#include <Keypad.h>                                                // Library  Keypad
const byte ROWS = 3;                                               // Keypad 3 rows
const byte COLS = 3;                                               // Keypad 3 colun
char hexaKeys[ROWS][COLS] =                                        // Char def  Keypad
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'}
};
byte rowPins[ROWS] = {4, 3, 2};            
byte colPins[COLS] = {10, 9, 8}; 
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);  // Initialise  Keypad
//-----------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
}
//-----------------------------------------------------------------------------------------------
void loop(){
  char customKey = customKeypad.getKey();
  if (customKey){
    Serial.println(customKey);
  }
}

1 Like

For correct operation with more than 1 magnet at the same time, the pin control is different. I haven't used the 'Keyapd' library but according to that
https://playground.arduino.cc/Code/Keypad/
it supports reading more than 1 button at a time and as @ruilviana you recommend it will probably be easiest for you.

1 Like

Hey, thanks for the guidance here. I'll need to tweak the code a bit from the MultiKey example the library provides, but at a high level this will probably work. However, the current 3x3 board I'm building is actually prototype for a 8x8 grid that will have up to 16 magnetic pieces active at a time. The Keypad library looks to only support up to 10 active keys at a time.

Do you know if I can have 2 custom keyboards operating at once to get all 16 key states? Or do you know of a library or solution that can support up to 16 keys at once?

EDIT:
After further testing, it seems that this program gives me a similar issue as the other ones. Sometimes if I place a magnet on grid id 0 for example, the output suggests that the entire row or column is activated.

Looks like a chessboard. Have you thought about connecting reed relays without using pin multiplexing? However, with an 8x8 board, you will need 64 pins. In this case, you can use 8 serial registers (type 8 parallel in's to 1 serial out, such as 74HC166 or 74HC165) connected in cascade and control all 8 with about only 4 pins. You will need to add a pull up or pull down resistor to the every reed relays at the inputs of the registers.

Thanks. I was recommended this once before, but wasn't sure how to approach it due to my limited understanding of electronics and how to properly connect this kind of circuit. Are there any resources you can suggest for this? I'm also not too sure about how to program for this either... Would they be plugged into digital outputs that can output a message like 00000001 to indicate that the last input in the row has a magnet present

Hopefully this makes sense

We were all beginners. The important thing is that you want to learn.

Start with the 'shiftIn' and 'shiftOut' commands. The difference in these commands is that 'shiftOut' is for more outputs and is most often used with 74HC595 and 'shiftIn' is for multiple inputs (as for your case) and is most often used with 74HC165.

https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/

Yes, the data will be something like that, in the presence of a magnet it will 0 or 1 , depending on whether you use pullup or pulldown resistors.

1 Like

Wow, thanks so much for all the help and resources. This is great!

I'll order some and give it a whirl.

Circling back around after various tests, just wanted to reach out and say thanks so much for your help and guidance! :grin: I was able to finish up a prototype with 16 inputs and am on to building the full 64-input chessboard grid.

I'm glad I helped. Good luck with your project!