MCP23017 to read hall sensors

Hi Guys

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 in advance.

Seems you don't know enough of I2C, you can change that here

The MCP23017 library uses the Wire library to access the I2C bus.

Hi Whandall

I used the linked provided and ran the scanner which provided the following output;

I2C scanner. Scanning ...
Found address: 39 (0x27)
Done.
Found 1 device(s).

I then ran the following code to get an output from pin 1 on my expander and all I receive is ?? & small squares in my serial monitor.

I want receive a 0 or 1/high or low from pin 1 on my expander which is connected to a hall sensor?

#include <Wire.h>
const byte MY_ADDRESS = 39;
void setup() {
  Wire.begin(MY_ADDRESS);           
  Serial.begin(9600);     
}

void loop() {
    Wire.requestFrom(MY_ADDRESS, 6);   

    
    while(Wire.available()) {
        char c = Wire.read();   
        Serial.print(c);       
    }

    delay(500);

}

Now you know how you can use the library with the found address,
and you know more about I2C. :grinning:

You print what you read as char, try Serial.println((int)c);,
which will put the values out numerically, I doubt the MCP speaks ASCII.

You can write your own access, but to do so,
you need to understand the data sheet of the MCP23017.

Maybe it is simpler to learn how to use the library.

I rarely use raw Wire, the combination of shifted address and R/W confuses me.

Hi Whandall

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.

Hi Whandall

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.

I tried to show you how you can learn to use the device,
but you seem to be more interested in copy and paste.

Good luck with your project.

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!

But first you have to know how the forum works.

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.

Hi Grumpy_Mike

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));
}

Example diagram:

Move those to other i/o pins. 0 and 1 are used for uploading code and the serial monitor.

a7

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."

Wait. Are you trying to use I2C?

Then you have to hook SCL and SDA to specific pins on the Mega.

a7

Hi alto777

I am using SDA (pin 20) and SCL (pin 21) as per the documentation and labelled on the Mega board.

Hmm.

So… now you are using 20 and 21 and it still doesn't work?

Try scanning the bus:

See if it is responding at all with code you had nothing to do with.

a7

It apparently does detect it, scan results below;

I2C Scanner
Scanning...
I2C device found at address 0x27 !
done

The Adafruit breakout board ships configured for an address of 0x20. Have you reprogrammed it to 0x27 or is this an indication of a problem?

Hi EmilyJane

It is not a Adafruit breakout board, I detailed the board at the top of my post.

FYI - the scan I done to find the address was using code suggested by alto777, which uses the wire library. Not sure if that is useful to tell you.