I currently have a Arduino mega but eventually intend to use an ESP32 for my project because it is faster, however the draw backs of an ESP32 is that it has less GPIO pins, so I am trying to configure a port expander on my Arduino mega first.
I have a MCP23017 [MCP23017 IO Expansion Board SKU: CQRMCP23017A - CQRobot-Wiki], which I want to use to connect to several hall sensors and that sets a variable if any hall sensor is triggered . To be honest I'm a struggling a bit getting started. I know that the SDA and SCL of the MCP23017 connects to SDA (pin 20) and SCL (pin 21) on my Arduino mega as well as 5v and GND but I'm not sure about using the wire library or MCP23017 library. The example code from the MCP23017 manufacture seems overkill, I don't understand it and every other library example & youtube video demonstrates writing to outputs like an LED.
The Arduino wire library uses Wire.requestFrom(2, 6); // Request 6 bytes from slave device number two but how do I know what slave address it is? Does it start from 1 or 0?
I would appreciate some help getting started, all I need is a basic understanding of how it works then I can build the rest of the code myself.
Thanks for responding but I don't see how the data sheet with no commands will help me to write commands for my sketch. The data sheet has detailed information of the MCP23017 which is suited for an electronic expert.
For example, I have programmed stepper motors even though I don't have a detailed knowledge of a TMC2209 data sheet .
The data sheet specifies what can be read or written, and what effect that has.
You want to read pins, so you have to find out how to initialize the device,
how to configure the pins, and how to read them.
You can glance over the electrical stuff.
But as I already stated, that is what a library does, interfacing the user and the hardware,
without forcing the user to fully understand the inner workings.
It may be straight forward to you but for me it is a very technical document that makes no sense and might as well be written in another language. There is no associated code or code example for me to play with.
I made post on this forum hoping that someone had a similar experience using a port expander and could help me with my code or had an example related to what I needed.
I know that a port expander can receive an input I just need to know how to do it with a hall sensor. I know how to use a hall sensor, it reads low when a magnetic is detected, so I just need to know when a hall sensor connected to pin 1 on a port expander (MCP23017) is low.
Thank you for your responses Whandall. I am not looking for a copy & paste just someone to point me in the right direction with useful advice to get started considering I am not an expert!
I have previously received useful advice from other members of this forum on other post I made that pointed me in the right direction and enabled me to build a functional robot arm with multiple stepper, dc motors & sensors consisting of hundreds of lines of code I wrote myself. And through the useful advice I received I have been able to contribute to other user post on this forum from my experience.
If everyone was an expert there would be no need for this or any forum!
Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section. Therefore I have moved your post here.
You might want to look at this [How to get the best from this from this Forum (How to get the best out of this forum) before you proceed any further.
Try reading this:-
It is the same chip inside, and the library they supply does contain examples.
It is worth knowing that I2C communications will be at the same speed no matter processor you use.
Your advice has been noted regarding the classification of post.
In terms of getting the MCP23017 working, I followed the example in the link you provided and I receive an error in the serial monitor when it try's to connect to the MCP23017 , the wiring configuration and code is exactly the same. The error is not very descriptive, so I'm not sure how to troubleshoot this?
Example code:
//#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>
#define LED_PIN 0 // MCP23XXX pin LED is attached to
#define BUTTON_PIN 1 // MCP23XXX pin button is attached to
// only used for SPI
//#define CS_PIN 39
// uncomment appropriate line
//Adafruit_MCP23X08 mcp;
Adafruit_MCP23X17 mcp;
void setup() {
Serial.begin(9600);
//while (!Serial);
Serial.println("MCP23xxx Combo Test!");
// uncomment appropriate mcp.begin
if (!mcp.begin_I2C()) {
//if (!mcp.begin_SPI(CS_PIN)) {
Serial.println("Error.");
while (1);
}
// configure LED pin for output
mcp.pinMode(LED_PIN, OUTPUT);
// configure button pin for input with pull up
mcp.pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("Looping...");
}
void loop() {
//added line below to test button press
if (mcp.digitalRead(BUTTON_PIN) != 0)Serial.println(mcp.digitalRead(BUTTON_PIN));
mcp.digitalWrite(LED_PIN, !mcp.digitalRead(BUTTON_PIN));
}
I moved the connection from pins 0 & 1 to pins 2 & 3 and it still didn't work.
But I don't think that is the problem because it is not even connecting to the MCP23017 in the first place because the if statement in setup checks for a connection
// uncomment appropriate mcp.begin
if (!mcp.begin_I2C()) {
// if (!mcp.begin_SPI(CS_PIN)) {
Serial.println("Error.");
while (1);
}
and on startup prints "MCP23xxx Combo Test!" "Error."