CJMCU-2317 not responding

Hi.

I managed to make a panel of 20 switches with a Leonardo and two MCP23017 IC on a breadboard. Every time I press a switch it will act as Im pressing a combination of keyboard keys.

That has been working fine. But now that Im trying to improve things for safety and stability of the project Im facing problems when I try to build it with CJMCU-2317 board.

https://www.aliexpress.com/item/1005008592833333.html?spm=a2g0o.tesla.0.0.2c53IBxdIBxdhK&pdp_npi=6%40dis!GBP!£6.06!£2.83!!!!!%4021038e1e17578685458457195e9eff!12000045867867614!btf!!!!1!0!&afTraceInfo=1005008592833333__pc__c_ppc_item_bridge_pc_same_wf__OOi3HZJ__1757868546232

Im building from scratch, this is just connecting one switch and going from there, but for some reason, I can not get it to work.

I really would appreciate some help here because Im completely lost at this point.

Here is the scheme and the code Im using.

As side note, Im connecting the Arduino to the USB as source for the +5V.

#include <Wire.h>
#include <Bounce2mcp.h>
#include <Adafruit_MCP23017.h>
#include <Keyboard.h>


#define PIN0 0

Adafruit_MCP23017 mcp0; 

#define addr0 0x20     

BounceMcp debouncer0 = BounceMcp();  

void setup() {
  
  Serial.begin(9600);
  Keyboard.begin();
  
  mcp0.begin(addr0);    
 
    mcp0.pinMode(0, INPUT);
    mcp0.pullUp(0, HIGH); 
 
  debouncer0.attach(mcp0, PIN0, 5);              

}    // End Void setup

void loop() {   
   
   
   debouncer0.update();      
 
    if ( debouncer0.fell() ) {   
         Keyboard.press('t');
         delay(50);
         Keyboard.press('0');
         Keyboard.releaseAll();    
      
    }

  
}   // END  void loop

Why? Leonardo has enough pins for more than 20 buttons without any extra chips.

Please post a link, I'm not familiar with that.

Hi Paul.

Im using the expanders because in the final stage, the panel will have between 50 and 60 switches.

Here is the module Im using.

Hi Alf

Ah, it's just a board with an mcp23017 on it. How is than an improvement compared to the same chip(s) on your breadboard?

16 pins can read 64 switches. Leonardo has 20 pins, so could read 100 buttons/switches with no extra chips.

Because when I had everything on the breadboard it was a delicate mess mixed with the resistors needed and every time the panel was moved, some cables would get lose so I had to spend time looking for which one was lose and so. Using this module would allow me to clear the interior of the panel and keep things tidy.

When I started this project, I considered the matrix to connect the switches, but at the time I thought that using the chips would allow me to increase the numebr of switches whenever it was needed in an easier way than having to re-design and solder a new matrix every time.

Pro Micro would have been a better choice than a Leonardo when using breadboards. Pro Micro is breadboard compatible.

What are the resistors for? MCP23017, and most types of Arduino, have built-in pull-ups for each pin.

I guess if the buttons are mounted on a different panel to the Leonardo, having the MCP module mounted on the button panel and only 2 wires plus Vcc and GND going back to the Leonardo would make the wiring simpler.

Each MCP module could read an 8x8 matrix. You could add more buttons later by adding a second MCP to read another 64 switches.

I believe I have a Pro Micro somewhere. I could try to use it instead of the Leonardo. I remember I used the Leonardo because at that time, it was the one used in a project to simulate a keyboard, which it’s indeed what the panel does. The Arduino detects a switch being pressed and it sends a simulated key to the PC.

The resistors were pull-up resistors for the I2C bus like in the scheme. This is the actual schematic I followed to do the panel.

That is my idea. Set the Leonardo on the back of the panel, and only those 4 cables ( Vcc,GND,SDA and SCK ) going into the panel to each of the modules located on each secction.

Yes, but correct me if Im wrong please. As far as I know, implementing the matrix is more complex than having the buttons connected individually to each module,right? Of course, there are a lot more cables, but the schematics are more simple. Am I correct?

Yes it makes the code a little more complex, but there are Arduino libraries which will make the coding more simple.

Yes, and more components. More components makes the code more complex and the circuit more expensive.

No, because there are more components and more wires, the schematic is more complex.

My advice would be to use only the Leonardo/Pro Micro. You can read 100 buttons/switches with no extra chips, making the circuit simpler and cheaper, and allowing you to expand your 50-60 switches in the future.

Reviewing your code, I can't immediately see a problem. I would suggest:

  1. Comment out the keyboard library code while you test the remaining code by simply using Serial.print().
  2. I'm not familiar with that debouncer library, but I can forsee it becoming a performance and memory problem if you eventually want to use 60 debouncer objects.
  3. Debouncing is easy, you really don't need a library. Don't read the switches continuously, read them once every 20ms, then bouncing won't be a problem.
  4. Read all 16 switches/MCP pins with a single function, not individually pin-by-pin, otherwise you will get performance problems. Use bitRead() to extract the values for individual switches from the value returned by the function.
  5. If you find yourself naming multiple variables with digits as part of their names, or copying & pasting parts of your code multiple times, making only minor changes, you have left the path of programming wisdom. The forum can help.