I have built an Arduino minimal breadboard circuit to test some parts I recently bought and I successfully managed to get the I2C MCP23008 working perfectly with LEDs and an LCD display.

However I have just recently tried to use the SPI version of the same chip with some really odd results. I have tried searching for code examples and libraries, but struggled to get anything working.

MCP23S08p Arduino interfacing

So I studied the datasheet to create the following code to flash an LED, however the flashing seems to work only briefly and then stops. Sometimes it stops and then stays lit randomly.

// inslude the SPI library:
#include <SPI.h>
const int chipSelect = 10;

void setup() {
  // initialize SPI:

  pinMode(chipSelect, OUTPUT);

  // set the chipSelect as an output:
  digitalWrite(chipSelect, HIGH);

  sendCommand(0x00, 0x00);

void sendCommand(int address,int data) {
  digitalWrite(chipSelect, LOW);
  //  send in the address and value via SPI:

  // SPI addressing 0x40 with A0 and A1 set to ground and R/W bit set to output

  // take the chipSelect pin high to de-select the chip:
  digitalWrite(chipSelect, HIGH);

void loop() {

I have a couple of these devices and they both came from a supplier on eBay. I would like to validate my code before I suspect that the devices are dodgy and are not a genuine part.

Can anyone help me validate my code, or has anyone had a similar experience with the SPI bus and other devices not behaving properly?


Could it be due to the length of my wires to the Arduino?

enter image description here

  • Do you have a 100nF (0.1µF) capacitor between VDD and VSS? – Majenko Jul 31 '16 at 23:47
  • @Majenko yes I do, I've missed that in my schematic. I've also tried pull-ups on the spi lines, even though they are usually not required but you never know? – A Devanney Aug 1 '16 at 8:04
  • Your picture does not seem to show 0.1uF caps on each VCC/AVCC pin of your devices (and AREF on the 328P as well - do not connect AREF to VCC). Those will help with consistent operation of all 3 chips in the picture as well. – CrossRoads Dec 20 '19 at 19:45

I have figured it out, lesson learned. I should have also drawn how my circuit connects to the Arduino, this would have probably then become obvious to me and everyone else.

Basically I had MOSI -> MOSI and the same with MISO -> MISO, rather than crossing them MOSI -> MISO respectively.

Circuit working fine now duh!


  • MOSI stands for "Master Out Slave In". MISO stands for "Master In Slave Out" so MOSI-MOSI, MISO-MISO is the correct wiring. The schematic in your question shows the connections MOSI-SO, MISO-SI, which is incorrect. – per1234 Nov 30 '17 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.