MCP23017 working with arduino uno but not working with atmega328p on breadboard

I have connect the MCP23017 to Arduino and it's working fine. but I want to use atmega328p on breadboard. this is the link which I referred for atmega328p on breadboard. "https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoToBreadboard". I connected MCP23017 and atmega328p on breadboard but it is not working. this is rough circuit diagram. and this same connection i have done on breadboard but not working.

This is my code.

#include <Adafruit_MCP23X17.h>
#include <Pushbutton.h>

const uint8_t led[9] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const uint8_t ledPowerandIntervalMode = 11;

const uint8_t btn[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
const uint8_t btnHorizontalPattern = 9;
const uint8_t btnVerticalPattern = 10;
const uint8_t btnRandomPattern = 11;
const uint8_t btnStopPattern = 12;
const uint8_t btnIntervalSelection = 13;

const uint8_t buzzer = 12;

Adafruit_MCP23X17 mcp;
Pushbutton *btnObj[9],
  btnHorizontalPatternObj(btnHorizontalPattern),
  btnVerticalPatternObj(btnVerticalPattern),
  btnRandomPatternObj(btnRandomPattern),
  btnStopPatternObj(btnStopPattern),
  btnIntervalSelectionObj(btnIntervalSelection);

const uint16_t horizontalPatternSequence[] = {
  0b0000000000000001,
  0b0000000000000010,
  0b0000000000000100,
  0b0000000000001000,
  0b0000000000010000,
  0b0000000000100000,
  0b0000000001000000,
  0b0000000010000000,
  0b0000000100000000,
};

const uint16_t verticalPatternSequence[] = {
  0b0000000000000001,
  0b0000000000001000,
  0b0000000001000000,
  0b0000000000000010,
  0b0000000000010000,
  0b0000000010000000,
  0b0000000000000100,
  0b0000000000100000,
  0b0000000100000000,
};
byte pattern = 0;

static byte previousOnLed, btnOff;
uint8_t actualIndex = 0;

byte btnIntervalSelectionCounter = 0;

unsigned long previousMillis;
long interval_;

bool btnHorizontalPatternFlag = false,
     btnVerticalPatternFlag = false,
     btnRandomPatternFlag = false,
     btnStopPatternFlag = false,
     btnIntervalSelectionFlag = true,
     skipInterval = true;
bool btnFlag[9] = { false, false, false, false, false, false, false, false, false };

void setup() {
  Serial.begin(9600);
  if (!mcp.begin_I2C(0x20)) {
    Serial.println("Error");
    while (1)
      ;
  }
  digitalWrite(ledPowerandIntervalMode, HIGH);

  randomSeed(analogRead(A0));
  for (int i = 0; i <= 8; i++) {
    btnObj[i] = new Pushbutton(btn[i]);
    pinMode(led[i], OUTPUT);
    mcp.pinMode(btn[i], INPUT);
  }

  pinMode(ledPowerandIntervalMode, OUTPUT);
  pinMode(buzzer, OUTPUT);

  mcp.pinMode(btnHorizontalPattern, INPUT);
  mcp.pinMode(btnVerticalPattern, INPUT);
  mcp.pinMode(btnRandomPattern, INPUT);
  mcp.pinMode(btnStopPattern, INPUT);
  mcp.pinMode(btnIntervalSelection, INPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (btnIntervalSelectionObj.getSingleDebouncedPress(mcp) && btnIntervalSelectionFlag == true) {
    tone(buzzer, 2500, 150);
    btnIntervalSelectionCounter++;
    btnHorizontalPatternFlag = btnVerticalPatternFlag = btnRandomPatternFlag = btnStopPatternFlag = true;
    if (btnIntervalSelectionCounter == 6) {
      btnIntervalSelectionCounter = 1;
    }
  }
  intervalSelection(btnIntervalSelectionCounter);
  Serial.println(interval_);

  patternSelection();

  switch (pattern) {
    case 'H':
      HandVPattern(currentMillis, horizontalPatternSequence);
      if (btnObj[btnOff]->getSingleDebouncedPress(mcp) && btnFlag[btnOff] == true) {
        tone(buzzer, 2500, 150);
        digitalWrite(led[btnOff], LOW);
        skipInterval = true;
      }
      break;
    case 'V':
      HandVPattern(currentMillis, verticalPatternSequence);
      if (btnObj[btnOff]->getSingleDebouncedPress(mcp) && btnFlag[btnOff] == true) {
        tone(buzzer, 2500, 150);
        digitalWrite(led[btnOff], LOW);
        skipInterval = true;
      }
      break;
    case 'R':
      randomPattern(currentMillis);
      if (btnObj[btnOff]->getSingleDebouncedPress(mcp) && btnFlag[btnOff] == true) {
        tone(buzzer, 2500, 150);
        digitalWrite(led[btnOff], LOW);
        skipInterval = true;
      }
      break;
    case 'S':
      stopPattern();
      break;
  }
}

void intervalSelection(byte btnIntervalSelectionCounter) {
  switch (btnIntervalSelectionCounter) {
    case 1:
      interval_ = 60000;
      break;
    case 2:
      interval_ = 30000;
      break;
    case 3:
      interval_ = 15000;
      break;
    case 4:
      interval_ = 10000;
      break;
    case 5:
      interval_ = 5000;
      break;
  }
}

void patternSelection() {
  if (btnHorizontalPatternObj.getSingleDebouncedPress(mcp) && btnHorizontalPatternFlag == true) {
    tone(buzzer, 2500, 150);
    pattern = 'H';
    btnVerticalPatternFlag = btnRandomPatternFlag = btnIntervalSelectionFlag = false;
  }
  if (btnVerticalPatternObj.getSingleDebouncedPress(mcp) && btnVerticalPatternFlag == true) {
    tone(buzzer, 2500, 150);
    pattern = 'V';
    btnHorizontalPatternFlag = btnRandomPatternFlag = btnIntervalSelectionFlag = false;
  }
  if (btnRandomPatternObj.getSingleDebouncedPress(mcp) && btnRandomPatternFlag == true) {
    tone(buzzer, 2500, 150);
    pattern = 'R';
    btnHorizontalPatternFlag = btnVerticalPatternFlag = btnIntervalSelectionFlag = false;
  }
  if (btnStopPatternObj.getSingleDebouncedPress(mcp) && btnStopPatternFlag == true) {
    tone(buzzer, 2500, 150);
    pattern = 'S';
    btnHorizontalPatternFlag = btnVerticalPatternFlag = btnRandomPatternFlag = btnIntervalSelectionFlag = true;
  }
}

void HandVPattern(unsigned long currentMillis, const uint16_t patternSequence[]) {
  if (currentMillis - previousMillis >= interval_ || skipInterval == true) {
    skipInterval = false;
    previousMillis = currentMillis;
    for (uint8_t i = 0; i < 9; i++) {
      if (patternSequence[actualIndex] & (1 << i)) {
        btnOff = i;
        btnFlag[i] = true;
        digitalWrite(led[i], HIGH);
      } else {
        btnFlag[i] = false;
        digitalWrite(led[i], LOW);
      }
    }
    actualIndex++;
    if (actualIndex >= 9) {
      actualIndex = 0;
    }
  }
}

void randomPattern(unsigned long currentMillis) {
  if (currentMillis - previousMillis >= interval_ || skipInterval == true) {
    skipInterval = false;
    previousMillis = currentMillis;
    int randomNum = randomNumGenerator();
    btnOff = randomNum;
    btnFlag[randomNum] = true;
    btnFlag[previousOnLed] = false;
    digitalWrite(led[previousOnLed], LOW);
    digitalWrite(led[randomNum], HIGH);
    previousOnLed = randomNum;
  }
}

void stopPattern() {
  for (int i = 0; i <= 8; i++) {
    digitalWrite(led[i], LOW);
    btnFlag[i] = false;
  }
  actualIndex = 0;
  skipInterval = true;
}

int randomNumGenerator() {
  static int randomNum = random(0, 9);
  int randomNum_ = random(0, 9 - 1);
  if (randomNum_ >= randomNum) {
    randomNum_++;
  }
  randomNum = randomNum_;

  return randomNum;
}

Circuit diagram

Forget the external hardware for the moment. Can you upload a simple sketch to the chip ?

@UKHeliBob
Yes I have test simple example from Arduino ide like blink, blink without delay and led with button this 3 example I have test with atmega328p on breadboard it is working.

Is the problem that you cannot upload the code or that it does not work once uploaded ?

Upload the the perfect it was work. But after upload nothing has work. Like with Arduino board.

Then I would suspect a wiring fault

Which pins of the Uno did you connect the MCP23017 to ?

I have attached the circuit diagram image. Please check

But that does not show how you connected it to the Uno. Which pins on the MCP23017 were connected to which pins on the Uno ?

28 no pn of atmeg328p is connected with 12 no pn of mcp23017 and 27 no pin of 328 is connected with 13 no pin of mcp23017.

You have still not answered the question

Which pins on the MCP23017 were connected to which pins on the Uno ?

12 no pin of mcp23017 to A5 of uno and 13 no pin of mcp23017 to A4 of uno. but i do not want to use arduino. I want to use as standalone. on arduino it's working.

I2C requires pullup resistors on the comm lines. At a minimum, try pinMode A4 and A5 to INPUT_PULLUP at the very beginning of setup.

Maybe I missed it but, I don't see a decoupling cap on the expander chip.

2x 4.7k pull-up resistors required on the I2C lines.

why need 4.7k pull up register in i2c line ? what is the logic behind it to using pull up register ?

Resistors not registers.

Because there are only 2 logic level outputs from I2C, Low and open circuit . The default condition is all chips on the line are in open circuit mode, and the pull-up resistor makes the line high.

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