MCP23S17 and arduino MICRO [Solved]

Hi everybody.

I try to use MCP23S17 on my arduino Micro. I use library from playground. I define SS on pin 10 and use standard pins for MISO,MOSI and SCK. My problem is nothing appênds. I connect 8 LED (one for each ppins on ports A) and run this program:

#include <SPI.h>

#include <MCP23S17.h>
#define ON 1
#define OFF 0
MCP chip1(0);
MCP chip2(1);
MCP chip3(2);
MCP chip4(3);
MCP chip5(4);
MCP chip6(5);
MCP chip7(6);
MCP chip8(7);
MCP chip9(8);

void setup() {
// put your setup code here, to run once:
while (!Serial);
Serial.println(“GO”);
Serial.println(“SS:” + String(SS));
Serial.println(“SCK:” + String(SCK));
Serial.println(“MOSI:” + String(MOSI));
Serial.println(“MISO:” + String(MISO));
delay(10000);

for (int i = 1;i<=16;i++)
{
chip1.pinMode(i,OUTPUT);
chip2.pinMode(i,OUTPUT);
chip3.pinMode(i,OUTPUT);
chip4.pinMode(i,OUTPUT);
chip5.pinMode(i,OUTPUT);
chip6.pinMode(i,OUTPUT);
chip7.pinMode(i,OUTPUT);
chip8.pinMode(i,OUTPUT);
chip9.pinMode(i,OUTPUT);
}
for (int i = 1;i<=16;i++)
{
for (int chip =1;chip <= 9;chip++)
{
switch (chip)
{
case 1: chip1.digitalWrite(i,ON); delay(100); chip1.digitalWrite(i,OFF); break;
case 2: chip2.digitalWrite(i,ON); delay(100); chip2.digitalWrite(i,OFF); break;
case 3: chip3.digitalWrite(i,ON); delay(100); chip3.digitalWrite(i,OFF); break;
case 4: chip4.digitalWrite(i,ON); delay(100); chip4.digitalWrite(i,OFF); break;
case 5: chip5.digitalWrite(i,ON); delay(100); chip5.digitalWrite(i,OFF); break;
case 6: chip6.digitalWrite(i,ON); delay(100); chip6.digitalWrite(i,OFF); break;
case 7: chip7.digitalWrite(i,ON); delay(100); chip7.digitalWrite(i,OFF); break;
case 8: chip8.digitalWrite(i,ON); delay(100); chip8.digitalWrite(i,OFF); break;
case 9: chip9.digitalWrite(i,ON); delay(100); chip9.digitalWrite(i,OFF); break;
}
Serial.println(“Chip:(” + String(chip) + “) Pin:(” + String(i) + “)”);

}
}
}

void loop() {

}

Are you really connecting LEDs to pins 0 and 1, AND using those pins for Serial, too? Do you really have an LED on the 8) pin?

Thank for reply

LEDs are connected to pins 1 to 8 of the MCP23S17; Nothing are connected on MIRCO except MCP23S17

mrobergedr: Thank for reply

LEDs are connected to pins 1 to 8 of the MCP23S17; Nothing are connected on MIRCO except MCP23S17

I think a schematic is in order, then.

An array of MCP objects would make that silly inner for loop a lot less messy.

Looping 9 times, and doing something different each time certainly makes the loop ineffective.

My problem is nothing appênds.

My problem is that I don't understand that statement. I can't figure out what you want to append to what.

I just want to know if someone was able to use MCP23S17 with an Arduino MICRO.

See Buttons, I2C, interrupts and port-expanders

@mrobergedr:

Please use code tags.

Read this before posting a programming question

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum

and use standard pins for MISO,MOSI and SCK

What pins, exactly?

See https://www.arduino.cc/en/Main/ArduinoBoardMicro

SPI: on the ICSP header. These pins support SPI communication using the SPI library. Note that the SPI pins are not connected to any of the digital I/O pins as they are on the Arduino/Genuino Uno, they are only available on the ICSP connector and on the nearby pins labelled MISO, MOSI and SCK.

Sorry but I don’t know how to send schematic

on arduino                  on MCP23S17

pin 1 left side (MOSI)      pin 13 (SI)
pin 2 right side (MISO)     pin 14 (SO)
pin 1 right side (SCK)      pin 12 (CLK)
pin 15 left side (IO10)     pin 11 (CS)
                            pin 10 to Ground)
                            pin 9 to VDD
                            pin 15-16-17 to ground add 0
                            pin 18 RESET to VDD
                            pin 21 to 28 to LEDs

This is the new code:

#include <SPI.h>
#define SS (10)
#include <MCP23S17.h>
#define ON 1
#define OFF 0
MCP chip1(0);



void setup() {
  // put your setup code here, to run once:
  pinMode(SS, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  while (!Serial);
  Serial.println("GO");
  Serial.println("SS:" + String(SS));
  Serial.println("SCK:" + String(SCK));
  Serial.println("MOSI:" + String(MOSI));
  Serial.println("MISO:" + String(MISO));
  delay(1000);
  SPI.begin();
  
  for (int i = 1;i<=16;i++)
  {
    chip1.pinMode(i,OUTPUT);
  }
}

void loop() {
  for (int i = 1;i<=16;i++)
  {
    chip1.digitalWrite(i,ON); delay(100); chip1.digitalWrite(i,OFF); 
    Serial.println("Pin:(" + String(i) + ")");
  }

}
  while (!Serial);

There’s no Serial.begin().

I made the correction but no led bliking.

#include <SPI.h>

#include <MCP23S17.h>
#define ON 1
#define OFF 0
MCP chip1(0);



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  while (!Serial);
  Serial.println("GO");
  Serial.println("SS:" + String(SS));
  Serial.println("SCK:" + String(SCK));
  Serial.println("MOSI:" + String(MOSI));
  Serial.println("MISO:" + String(MISO));
  delay(1000);
  SPI.begin();
  
  for (int i = 1;i<=16;i++)
  {
    chip1.pinMode(i,OUTPUT);
  }
}

void loop() {
  for (int i = 1;i<=16;i++)
  {
    chip1.digitalWrite(i,ON); delay(100); chip1.digitalWrite(i,OFF); 
    Serial.println("Pin:(" + String(i) + ")");
  }

}
[/

You only have one delay there:

    chip1.digitalWrite(i,ON); delay(100); chip1.digitalWrite(i,OFF);

That means they will be on most of the time.

How about:

    chip1.digitalWrite(i,ON); 
    delay(100); 
    chip1.digitalWrite(i,OFF); 
    delay(100);

And don't try to cram lots of statements on one line. Code is supposed to be readable.

I hope you have resistors in series with your LEDs. Do you?

  for (int i = 1;i<=16;i++)
  {
    chip1.digitalWrite(i,ON); delay(100); chip1.digitalWrite(i,OFF); 
    Serial.println("Pin:(" + String(i) + ")");
  }

on for 100 millisecond, turn off and next led on for 100 millisecond and so on.

Yes I have resistor on LEDs.

If I move the LEDs to the Arduino port 3 to 10 and change the code to:

for (int i = 3;i<=10;i++)
{
digitalWrite(i,ON);
delay(100);
digitalWrite(i,OFF);
}

the LEDs work well so it’s not the LEDs who is the problem but the MCP. I just want to be sure that the arduino Micro board can drive a MCP23S17 , because I did not find schematic using it only UNO nano and mega.

Certainly it can. Perhaps take a photo of your setup?

I got it to work with my own code. Here is the wiring:

Here is the code:

// MCP23017 and MCP23S17 demo
// Author: Nick Gammon
// Date: 11 September 2015

#include <Wire.h>
#include <SPI.h>

// MCP23017 registers (everything except direction defaults to 0)

const byte  IODIRA   = 0x00;   // IO direction  (0 = output, 1 = input (Default))
const byte  IODIRB   = 0x01;
const byte  IOPOLA   = 0x02;   // IO polarity   (0 = normal, 1 = inverse)
const byte  IOPOLB   = 0x03;
const byte  GPINTENA = 0x04;   // Interrupt on change (0 = disable, 1 = enable)
const byte  GPINTENB = 0x05;
const byte  DEFVALA  = 0x06;   // Default comparison for interrupt on change (interrupts on opposite)
const byte  DEFVALB  = 0x07;
const byte  INTCONA  = 0x08;   // Interrupt control (0 = interrupt on change from previous, 1 = interrupt on change from DEFVAL)
const byte  INTCONB  = 0x09;
const byte  IOCON    = 0x0A;   // IO Configuration: bank/mirror/seqop/disslw/haen/odr/intpol/notimp
const byte  GPPUA    = 0x0C;   // Pull-up resistor (0 = disabled, 1 = enabled)
const byte  GPPUB    = 0x0D;
const byte  INFTFA   = 0x0E;   // Interrupt flag (read only) : (0 = no interrupt, 1 = pin caused interrupt)
const byte  INFTFB   = 0x0F;
const byte  INTCAPA  = 0x10;   // Interrupt capture (read only) : value of GPIO at time of last interrupt
const byte  INTCAPB  = 0x11;
const byte  GPIOA    = 0x12;   // Port value. Write to change, read to obtain value
const byte  GPIOB    = 0x13;
const byte  OLLATA   = 0x14;   // Output latch. Write to latch output.
const byte  OLLATB   = 0x15;


const byte  DEVICE_ADDRESS = 0x20;  // MCP23017 is on I2C port 0x20

const byte ssPin = 10;   // slave select pin, if non-zero use SPI
const byte expanderPort = 0x20;

// set register "reg" on expander to "data"
// for example, IO direction
void expanderWrite (const byte reg, const byte data ) 
  {
  startSend ();
    doSend (reg);
    doSend (data);
  endSend ();
} // end of expanderWrite

// prepare for sending to MCP23017 
void startSend ()   
{
  
  if (ssPin)
    {
    digitalWrite (ssPin, LOW); 
    SPI.transfer (expanderPort << 1);  // note this is write mode
    }
  else
    Wire.beginTransmission (expanderPort);
  
}  // end of startSend

// send a byte via SPI or I2C
void doSend (const byte what)   
{
  if (ssPin)
    SPI.transfer (what);
  else
    Wire.write (what);
}  // end of doSend

// finish sending to MCP23017 
void endSend ()   
{
  if (ssPin)
    digitalWrite (ssPin, HIGH); 
  else
    Wire.endTransmission ();
 
}  // end of endSend

void setup ()
{
  
  if (ssPin)  // if we have an SS pin it is SPI mode
    {
    digitalWrite (ssPin, HIGH);
    SPI.begin ();
    pinMode (ssPin, OUTPUT);
    }
  else
    Wire.begin (DEVICE_ADDRESS);   

  // byte mode (not sequential)
  expanderWrite (IOCON, 0b00100000);
  
  // all pins as outputs
  expanderWrite (IODIRA, 0);
  expanderWrite (IODIRB, 0);
  
}  // end of setup

void loop ()
  {
  expanderWrite (GPIOA, 0xAA);
  delay (100);  
  expanderWrite (GPIOA, 0);
  delay (100);  
  }  // end of loop

As you can see from the photo, half of the LEDs are on (as I sent 0xAA) and I can assure you that they flash.

Note: you must open the serial monitor, or the code hangs waiting for the Serial connection to be established.

Your code sort-of works as well, although the serial prints in loop() slow it right down. Also one of the pins flickers in a rather strange way.

Still, I suggest if neither my code nor your code works, you have a wiring problem.

thank I will try it tomorow

Thank you very much. It's working. I will study your sketch and build my own class for driving stepper motor with MCP23S17

I will study your sketch and build my own class for driving stepper motor with MCP23S17

You realize, I hope, that driving a low voltage, low current LED and driving a high voltage, high current stepper motor are QUITE different tasks.

Yes I have builded a high current driver using TIP122.

I am not a beginner with arduino and electronic. I had just use a library who don't work. But SPI for me are really new.

Thank a lot for your time and patience