Using SPI for IO Expander


I’m new to arduino and I got an IO expander chip that uses SPI to communicate with the arduino but I am having a hard time making things work. I triple checked my hardware connections and there seems to be no fault. I followed the steps in the documentation for arduino SPI and came up with this code: ( The 1 second delay is added so that I can just view things slowly on the serial monitor)

#include <stdio.h>
#include <string.h>
#include <SPI.h>

const uint8_t S = 5;  // Digital pin 10 
const uint8_t CS = 10; // Digital pin 5
uint16_t receivedVal;

void setup() {

  pinMode(S, OUTPUT);
  pinMode(CS, OUTPUT);

void loop() {

   digitalWrite(S, HIGH);
SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV128, LSBFIRST, SPI_MODE3));
   digitalWrite(CS, LOW);
   receivedVal = SPI.transfer16(0x0000);
   digitalWrite(CS, HIGH); // disable Slave Select
   digitalWrite(S, LOW);

I don’t know if I have something wrong with my code or I’m not implementing things right but on the serial monitor I can see that I am getting 0s which is logical because I’m not pressing any button but if I do press the buttons, only the 12th button shows an output on the serial monitor and the rest don’t. The buttons are connected from one side to +Vcc (5V) and the other side to the digital pins of the chip. I can’t upload the datasheet of the chip as Im still a new user but it’s M66006P by Mitsubishi. Any help is greatly appreciated :slight_smile:

I can post a data sheet for the M66006P

Which Arduino board are you using?

Please post a schematic of your wiring.

Read the forum guidelines to see how to properly post code.

You state: “I got an IO expander chip that uses SPI to communicate with the arduino” That tells me you have one of many thousand different devices. My eyes are not what they need to be and I cannot see what you have even with my glasses. Posting a Schematic, not a frizzy thing with technical links to each piece of hardware will help.

Hello I am using an arduino Uno R3 which looks like this one:
As for the schematic of the wiring, I drew it by hand and it’s shown below:


Hi it’s the M66006P by Mitsubishi; I posted its name at the end of the post. Also, I added a schematic now apologies for the confusion :slight_smile:

Does the chip have built-in pull-down resistors? If not, you will need pull-down resistors (10k between chip input pin and Ground) to keep the pins from floating when they aren’t connected to +5v.

Okay I will try that tomorrow once I get the 10k Ohms resistors, I hope this is the problem, thanks!!

The MCP23S17 is a 16 bit SPI port expander that has built in pullup resistors that can be enabled for each input. The MCP23S17 also has pin change interrupt capability that can be handy for reading switches.

Unfortunately it’s not available in my country. Ideally, I would want something with as much GPIOs as possible but the M66006P is the closest I’ll get :frowning:

That truly is a bummer. I use the I2C equivalent of the MCP23S17, the MCP23017 and really like them for adding GPIO to my projects.

Is that a real command?
I would normally do two SPI.transfer() with a byte in each.

yeahh, since I want to read 12 bits I’d rather use the transfer 16 which gives me 16 bits and just mask the unused 4 bits instead of transfer which gives me only 8 bits

I added pull down resistors but still nothing. I did notice that when I press on the button connected to D12 it does show an input on the serial monitor but it is not consistent with what I press and it does not read it every time.

The outputs/inputs per the data sheet are open drain. That tells us it has a n-channel fet driving it. Since there is nothing in the data sheet that I saw about pull ups, you need to add one to each I/O. Pull down resistors will not work. I believe you also have to set all the I/O to a logic 1 otherwise it will read its own “0” which is the dominate state. Let us know how you do. It is basically a pair of shift registers with serial in parallel out/parallel in/serial out )via SPI.

So I tried the pull up resistors and it did not work. I honestly feel like it’s a big hassle and a waste of time and as frustrating as it is, to feel beaten by a chip, I have to move on with the project for the submission date so I ordered a different chip, PCF8574, and I got it up and running for 24 buttons 3 chips :slight_smile:
Thanks to you and to everyone that gave this post a read and offered help :heart:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.