summary refs log tree commit diff
diff options
context:
space:
mode:
authoruser <user@node5.net>2024-05-04 15:57:25 +0200
committeruser <user@node5.net>2024-05-04 15:57:25 +0200
commit32b534792558e00d53edcbd984ffc8b10f5eff23 (patch)
tree992ff96eaf189f5815921f7c61d078b5604e9338
parenta3d122811cc001a4895d6166484a107a3fde34a3 (diff)
Custom keyboard v2 - MCP23017 blink C++ sample works, wiring documented
-rwxr-xr-xCustom keyboard V2/index.md131
1 files changed, 130 insertions, 1 deletions
diff --git a/Custom keyboard V2/index.md b/Custom keyboard V2/index.md
index 4c46a4a..0bebe8f 100755
--- a/Custom keyboard V2/index.md
+++ b/Custom keyboard V2/index.md
@@ -183,4 +183,133 @@ So up to this point, i had the intention of making this into a full hand wired k
 But I don't like how the wires at the top ended up looking,
 so I've chosen to cut my losses and use my prototype to decide how I want the offsets to be,
 and as a test bed to remove my last few unknowns, namely, thumb cluster and I/O expander chip.
-I should have cut more corners, to test the core concept, before spending time, on the niceties.
\ No newline at end of file
+I should have cut more corners, to test the core concept, before spending time, on the niceties.
+
+## MCP23017
+
+Using this tutorial:
+[makerhacks.com ─ Use the MCP23017 GPIO Port Expander to Add 16 IO Pins](https://www.makerhacks.com/mcp23017/)
+i was able to make the [MicroPython](https://en.wikipedia.org/wiki/MicroPython) example work, but issues arise. 
+It keeps throwing the exception: `[Errno 5] EIO`, furthermore `i2c.scan()` is not consistent with what it returns.
+All this is despite forgoing the breadboard, in favor of directly soldered connections :/
+
+### MCP23017 MicroPython code snippet
+
+```py
+# https://blog.node5.net/Custom%20keyboard%20V2#mcp23017-micropython-code-snippet
+# Based on: https://www.makerhacks.com/mcp23017#h-using-mcp23017-with-raspberry-pi-pico-and-micropython
+import time
+import machine
+
+i2c = machine.I2C(1,
+                  sda=machine.Pin(0),
+                  scl=machine.Pin(1),
+                  )
+i2cdevices = i2c.scan()
+print(i2cdevices)
+time.sleep(0.50)
+i = 0
+while 1:
+    try:
+        i2c.writeto_mem(0x20, 0x00, b'\xFF')
+        i2c.writeto_mem(0x20, 0x01, b'\x00')
+        i2c.writeto_mem(0x20, 0x13, b'\x00')
+        while 1:
+            i2c.writeto_mem(0x20, 0x13, b'\xFF')
+            time.sleep(0.50)
+            i2c.writeto_mem(0x20, 0x13, b'\x00')
+            time.sleep(0.50)
+    except Exception as ex:
+        i += 1
+        print(i, ex)
+
+```
+
+> [github.com ─ dhylands / rshell](https://github.com/dhylands/rshell) Can be used to make it easier, to work
+> with [MicroPython](https://en.wikipedia.org/wiki/MicroPython)
+
+The issues persisted, and i decided to move from MicroPython to C++ using the 
+[Adafruit library](https://github.com/adafruit/Adafruit─MCP23017─Arduino─Library)
+And since the [github.com ─ earlephilhower/arduino─pico](https://github.com/earlephilhower/arduino─pico) functions
+[`bool SetSDA` and `bool SetSCL`](https://arduino─pico.readthedocs.io/en/latest/wire.html)
+didn't work to change I²C 0 pins, 
+i change to the default pins `GP4` for `SDA` and `GP4` for `SCL` denoted by the Raspberry PI Pico pinout.
+
+![Raspberry Pi Pico pinout](https://www.raspberrypi.com/documentation/microcontrollers/images/pico-pinout.svg)
+
+Still the issues persisted, still working sporadically for a bit, and then stopped working.
+I then tried using pull─up resistors with a value of `4.7KΩ`. Still didn't work consistently.
+I then searched around, and came across this forum post:
+[forum.arduino.cc ─ Problems with MCP23017 ─ Solved](https://forum.arduino.cc/t/problems─with─mcp23017─solved/21997)
+And i followed the wiring described therein. Namely, tie `A0`, `A1` and `A2` to ground, 
+`Reset` to `3.3v` using a `1KΩ` pull-up resistor, and finally tie `SDA` and `SCL` to 3.3v using a `1KΩ` pull-up resistor
+(see the following ascii art diagram)
+
+```
+                            ┌── U ──┐
+[ LED Anode ]   GPB0 ( 8 )  ┤1    28├ GPA7 ( 7 )
+                GPB1 ( 9 )  ┤2    27├ GPA6 ( 6 )
+                GPB2 ( 10 ) ┤3    26├ GPA5 ( 5 )
+                GPB3 ( 11 ) ┤4    25├ GPA4 ( 4 )
+                GPB4 ( 12 ) ┤5    24├ GPA3 ( 3 )
+                GPB5 ( 13 ) ┤6    23├ GPA2 ( 2 )
+                GPB6 ( 14 ) ┤7    22├ GPA1 ( 1 )
+                GPB7 ( 15 ) ┤8    21├ GPA0 ( 0 )
+[ 3.3v ]               VDD  ┤9    20├ INTA
+[ GND ]                VSS  ┤10   19├ INTB  
+                       NC   ┤11   18├ RESET  [ 1KΩ → 3.3v ]
+[ GP4 & 1KΩ → 3.3v ]   SCL  ┤12   17├ A2     [ GND ]
+[ GP5 & 1KΩ → 3.3v ]   SDA  ┤13   16├ A1     [ GND ]
+                       NC   ┤14   15├ A0     [ GND ]
+                            └───────┘
+                   
+( x ) = Adafruit library number for this pin
+[ x ] = Microcontroller pin
+```
+
+### MCP23017 C++ code snippet
+
+```c++
+https://blog.node5.net/Custom%20keyboard%20V2#mcp23017-c-code-snippet
+Based on: https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/examples/mcp23xxx_blink/mcp23xxx_blink.ino
+// Blinks an LED attached to a MCP23017 pin.
+
+#include <Adafruit_MCP23X17.h>
+
+#define LED_PIN 8     // MCP23XXX pin LED is attached to
+
+Adafruit_MCP23X17 mcp;
+
+void setup() {
+	Serial.begin(9600);
+	// while (!Serial);  // Wait for Serial connection, enable for debug of early code
+	Serial.println("MCP23017 Blink Test!");
+	
+	// Find ID, by attempting initialization sequentially
+	int i = 0;
+	while (!mcp.begin_I2C(i)) {
+		Serial.print("Error with ID:");
+		Serial.println(i);
+		i++;
+		delay(10);
+	}
+	Serial.print("Success with ID:");
+	Serial.println(i);
+
+	mcp.pinMode(LED_PIN, OUTPUT);
+
+	Serial.println("Looping...");
+}
+
+void loop() {
+	Serial.print("High, millis: ");
+	Serial.println(millis());
+	mcp.digitalWrite(LED_PIN, HIGH);
+	delay(250);
+	
+	Serial.print("Low,  millis: ");
+	Serial.println(millis());
+	mcp.digitalWrite(LED_PIN, LOW);
+	delay(250);
+}
+```
\ No newline at end of file