I currently try to use a MCP23S17 using the library from Rob Tillaart. I successfully can "write" to the ports and control mosfets and status leds. The SPI-connection and basic functionality should work. My problem is the reading of input. I connected the following elements
The results are the same. Since the chip can not distinguish between an open connection, unconnected and connected I may have overlooked some additional settings.
Am I missing a register setting or are the connection faulty?
I don't think you read the library documentation carefully enough. Unlike the standard arduino pinMode() function, the pinMode() function in the library does not enable the internal pull-up resistors. There is a different function for that.
Changing the parameter value for pinMode() only sets which pins are input and which are output. 0xFF sets them all to output, I suspect. If you set the pin connected to the reed switch to output and then to high, and the reed switch closes, there could be a damaging short-circuit.
I referenced the wrong function. In the library ( ) you can set a whole port to either input or output. Basically the whole bit pattern has to be set for port A and B.
Agreed. Have you read the library documentation and figured out the right function yet?
I agree. I was wrong about 1 = output. But that's not the reason why you can't read your reed switch. You need to enable the internal pull-up for that pin. You need to figure out which library function does that. I can see it...
I think you already did that.
The first parameter of .pinMode8() is 0=port A, 1=port B
You seem to be doing something strange here. That first parameter of .pinMode() is intended to be 0 or 1. Not 0x0C or 0x0D. Why do you think that will affect the internal pullups?
Find the right function in the library docs for enabling the internal pullups. Its very easy to find. I found it in less than a minute, using your link from the first post. Use it.
They can be left disconnected, just like Arduino pins. But if you try to read them, you will get random results. Unless you enable the pullup resistors. just like Arduino pins.
Update and solution
I copied the executable example here in the forum from my original project and forgot to include the initialization from the SPI bus (all buses are configured elsewhere). The solution is to start the SPI transaction
SPI.begin();
The whole example now looks like
#include <Arduino.h>
// #include<DigitalExtender.h>
#include<MCP23S17.h>
// #include<Logger.h>
// bool DEBUGLOG = true;
uint8_t CS_PIN = 9;
MCP23S17 mcp23s17 = MCP23S17(CS_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
uint8_t rv = mcp23s17.begin();
Serial.println(rv ? "true" : "false");
//set config
rv = mcp23s17.pinMode8(0x00, 0b11111111); // 0 = output , 1 = input
Serial.println(rv);
delay(10);
rv = mcp23s17.setPullup8(0x01,0x00); // pullup 1=enabled 0b11111111
Serial.println(rv);
delay(10);
rv = mcp23s17.pinMode8(0x01, 0b11111111);
Serial.println(rv);
delay(10);
rv = mcp23s17.setPullup8(0x01, 0x00); // pullup 1=enabled
Serial.println(rv);
delay(10);
mcp23s17.usesHWSPI();
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("--- Digital Extender ---");
}
void blink() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void loop() {
uint8_t status = 0;
uint16_t status16 = 0;
for(uint8_t i=0;i<16;i++) {
status = mcp23s17.read1(i);
Serial.print(status);
}
Serial.print("\t");
status16 = mcp23s17.read16();
Serial.print(status16,BIN); Serial.print("\t");
// delay(500);
long val = 0;
long t0 = millis();
for(int i=0;i<1024;i++) {
if(digitalRead(6)) {
val++;
}
}
Serial.print("D6["); Serial.print(millis()-t0); Serial.print("]\t");
Serial.println(val);
delay(500);
blink();
}