With nothing connected to the input ports, this prints "1111111111111111".
If I apply 3v to the first port, it still prints "1111111111111111".
If I apply ground to the first port, it prints "0111111111111111".
I would like it to be the opposite. With nothing connected it should be "0000000000000000" and when 3v is applied to the first pin it should be "10000000000000000".
#include <Adafruit_MCP23017.h> //port extender
Adafruit_MCP23017 mcp;
void setup() {
// Setup port extender
Wire.begin();
mcp.begin(0);
for (int i = 0; i <= 15; i++) {
mcp.pinMode(i, INPUT);
}
for (int i = 0; i <= 15; i++) {
Serial.print(mcp.digitalRead(i));
}
Serial.println("");
}
void loop() {
}
Grumpy_Mike:
Look at the data sheet. You have to enable the pull up.
I was confused with this answer because I have the same issue as original question i wanted default value 00....but you mentioned like we need to enable pull up.
so now i have to enable pull down for all 16 additional gpios by adding resistor between pin and ground,right?
I was confused with this answer because I have the same issue as original question i wanted default value 00....but you mentioned like we need to enable pull up.
The suggestion to use pull-ups was so that you had valid inputs. Without an active sensor, pull-up or pull-down, the input pin(s) are floating and are invalid, unknown logic levels.
Why do you need inputs to be zero? You’ve not explained anything about your inputs other than you’re using 3 volts to test. Is that the supply voltage of the MCP23017? Is that the system voltage?
i have to enable pull down for all 16 additional gpios by adding resistor between pin and ground,right?
Yes, if you need pull-downs for some unknown, unexplained reason, you must must add external resistors. Most times though, you would just enable the internal pull-up and you’re done. If you’re reading a switch and you want the logic reversed so the the input is a zero with the button released and a one with the button pressed, let the compiler do it for you by reading the input like this:
so now i have to enable pull down for all 16 additional gpios by adding resistor between pin and ground,right?
Yes if you want the period between the power up or reset, to the code running to have the value of zero. This is sometimes needed if those pins are eventually going to be outputs and you have wired the outputs to be active high ( a common rookie mistake ). Then you might need a pull down resistor on those pins but more commonly you would design the system so that peripherals are active low.
Grumpy_Mike:
Yes if you want the period between the power up or reset, to the code running to have the value of zero. This is sometimes needed if those pins are eventually going to be outputs and you have wired the outputs to be active high ( a common rookie mistake ). Then you might need a pull down resistor on those pins but more commonly you would design the system so that peripherals are active low.
Outputs are routinely designed to be active high with pull-down resistor... especially if there's a MOSFET connected to them.
wvmarle:
Outputs are routinely designed to be active high with pull-down resistor... especially if there's a MOSFET connected to them.
Or also routinely designed to be active low with pull-up resistor... especially if there's a logic-level device connected to them.
Having the source voltage contained within the device receiving the control signal tends to make the interconnection more "fail-safe" in terms of accidental shorts.