ATmega328PB doesn't seem to be compatible with ATmega328P code

I wrote a program for ATmega328p and this program works great but i ordered this which came with ATmega328PB chip. here is the link: HW-229 USBtinyISP AVR ISP Programmer Bootloader For Arduino R3 Meag2560 with 10pin Programming Cable – Alexnld.com
I am able to run the bootloader but it doesn't do anything. Here is my program which wrote for ATmega328p

#include <mcp2515.h> 
//CAN FRAME FOR EACH BUTTON
struct can_frame canMsg0;

// global variables
byte lastInputState0 = 0x00;
byte lastInputState1 = 0x00;
byte canByte0 = 0x00;
byte canByte1 = 0x00;

// Chip select pin for MCP2515 
MCP2515 mcp2515(10);

//Defining the buttons and their pins for arduino R3 and if using a different microcontroller the pin assignment will change
#define  button0Pin     3      // SW1_1 PD3
#define  button1Pin     4      // SW1_3 PD4
#define  button2Pin     5      // SW2_1 PE0
#define  button3Pin     6      // SW2_3 PE1
#define  button4Pin     7      // SW3_1 PD5
#define  button5Pin     8      // SW3_3 PD6
#define  button6Pin     9      // SW4_1 PD7
#define  button7Pin     A0     // SW4_3 PB0
#define  button8Pin     A1     // SW5_1 PB1
#define  button9Pin     A2     // SW5_3 PB2
#define  button10Pin    A3     // SW6_1 PC3
#define  button11Pin    A4     // SW6_3 PC4
#define  button12Pin    A5     // SW7_1 PC5


//defining the can with baudrate and clock speed for configuration
#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0x90)
#define MCP_8MHz_500kBPS_CFG3 (0x82)


//button states starts with low
int buttonState0  = 0;
int buttonState1  = 0;
int buttonState2  = 0;
int buttonState3  = 0;
int buttonState4  = 0;
int buttonState5  = 0;
int buttonState6  = 0;
int buttonState7  = 0;
int buttonState8  = 0;
int buttonState9  = 0;
int buttonState10 = 0;
int buttonState11 = 0;
int buttonState12 = 0;

void setup() {

  //Can bus with id, dlc and data to be transferring
  canMsg0.can_id  = 0x036;
  canMsg0.can_dlc = 2;
  canMsg0.data[0] = 0x00;
  canMsg0.data[1] = 0x00;

  while (!Serial);
  Serial.begin(500000);

  //Initializing the SPI
  SPI.begin();

  mcp2515.reset();
  //Initializing the CAN protocol with 500k baudrate and 8MHz clock
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  //Starting the CAN in normal mode
  mcp2515.setNormalMode();


  //Making all pins as input due to using pull down resistor
  pinMode(button0Pin,   INPUT);
  pinMode(button1Pin,   INPUT);
  pinMode(button2Pin,   INPUT);
  pinMode(button3Pin,   INPUT);
  pinMode(button4Pin,   INPUT);
  pinMode(button5Pin,   INPUT);
  pinMode(button6Pin,   INPUT);
  pinMode(button7Pin,   INPUT);
  pinMode(button8Pin,   INPUT);
  pinMode(button9Pin,   INPUT);
  pinMode(button10Pin,  INPUT);
  pinMode(button11Pin,  INPUT);
  pinMode(button12Pin,  INPUT);

  lastInputState0 = canByte0;
  lastInputState1 = canByte1;

}
void loop() {

  canByte0 = 0x00;
  canByte1 = 0x00;

  //Read the state of the pushbutton value
  if (digitalRead(button0Pin)  == HIGH) {
    canByte0 |= (0x01 << 0);
  }
  if (digitalRead(button1Pin)  == HIGH) {
    canByte0 |= (0x01 << 1);
  }
  if (digitalRead(button2Pin)  == HIGH) {
    canByte0 |= (0x01 << 2);
  }
  if (digitalRead(button3Pin)  == HIGH) {
    canByte0 |= (0x01 << 3);
  }
  if (digitalRead(button4Pin)  == HIGH) {
    canByte0 |= (0x01 << 4);
  }
  if (digitalRead(button5Pin)  == HIGH) {
    canByte0 |= (0x01 << 5);
  }
  if (digitalRead(button6Pin)  == HIGH) {
    canByte0 |= (0x01 << 6);
  }
  if (digitalRead(button7Pin)  == HIGH) {
    canByte0 |= (0x01 << 7);
  }
  if (digitalRead(button8Pin)  == HIGH) {
    canByte1 |= (0x01 << 0);
  }
  if (digitalRead(button9Pin)  == HIGH) {
    canByte1 |= (0x01 << 1);
  }
  if (digitalRead(button10Pin) == HIGH) {
    canByte1 |= (0x01 << 2);
  }
  if (digitalRead(button11Pin) == HIGH) {
    canByte1 |= (0x01 << 3);
  }
  if (digitalRead(button12Pin) == HIGH) {
    canByte1 |= (0x01 << 4);
  }

  if (lastInputState0 != canByte0 || lastInputState1 != canByte1)
  {
    Serial.print("\nCAN Byte0: 0x");
    Serial.print(canByte0 < 16 ? "0" : "");
    Serial.print(canByte0, HEX);
    canMsg0.data[0] = canByte0;

    Serial.print(", CAN Byte1: 0x");
    Serial.print(canByte1 < 16 ? "0" : "");
    Serial.println(canByte1, HEX);
    canMsg0.data[1] = canByte1;
    mcp2515.sendMessage(&canMsg0);
  }
  else {mcp2515.sendMessage(&canMsg0); } // this will be transmitting zeros for every 500ms

  lastInputState0 = canByte0;
  lastInputState1 = canByte1;
  delay(500); // adding the delay to the system of buttons till displaying the hex symbol. 
}

I know the pin configurations are difference for ATmega328PB so here is the code i wrote which compiles but doesn't do anything. any help would be a huge help for me.
#include <mcp2515.h>


//CAN FRAME FOR EACH BUTTON
struct can_frame canMsg0;

// global variables
byte lastInputState0 = 0x00;
byte lastInputState1 = 0x00;
byte canByte0 = 0x00;
byte canByte1 = 0x00;

// Chip select pin for MCP2515 
MCP2515 mcp2515(19);


//Defining the buttons and their pins for arduino R3 and if using a different microcontroller the pin assignment will change
#define PIN_SPI_SS 19
#define PIN_SPI_MOSI 22
#define PIN_SPI_MISO 23
#define PIN_SPI_SCK 24

#define  button0Pin     1      // SW1_1 PD3
#define  button1Pin     2      // SW1_3 PD4
#define  button2Pin     3      // SW2_1 PE0
#define  button3Pin     6      // SW2_3 PE1
#define  button4Pin     9      // SW3_1 PD5
#define  button5Pin     10      // SW3_3 PD6
#define  button6Pin     11      // SW4_1 PD7
#define  button7Pin     12     // SW4_3 PB0
#define  button8Pin     13     // SW5_1 PB1
#define  button9Pin     14     // SW5_3 PB2
#define  button10Pin    26     // SW6_1 PC3
#define  button11Pin    27     // SW6_3 PC4
#define  button12Pin    28     // SW7_1 PC5
#define  button13Pin    30
#define  button14Pin    31 
#define  button15Pin    32


//defining the can with baudrate and clock speed for configuration
#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0x90)
#define MCP_8MHz_500kBPS_CFG3 (0x82)



//button states starts with low
int buttonState0  = 0;
int buttonState1  = 0;
int buttonState2  = 0;
int buttonState3  = 0;
int buttonState4  = 0;
int buttonState5  = 0;
int buttonState6  = 0;
int buttonState7  = 0;
int buttonState8  = 0;
int buttonState9  = 0;
int buttonState10 = 0;
int buttonState11 = 0;
int buttonState12 = 0;

void setup() {

  //Can bus with id, dlc and data to be transferring
  canMsg0.can_id  = 0x036;
  canMsg0.can_dlc = 2;
  canMsg0.data[0] = 0x00;
  canMsg0.data[1] = 0x00;

  while (!Serial);
  Serial.begin(500000);

  //Initializing the SPI
  SPI.begin();

  mcp2515.reset();
  //Initializing the CAN protocol with 500k baudrate and 8MHz clock
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  //Starting the CAN in normal mode
  mcp2515.setNormalMode();


  //Making all pins as input due to using pull down resistor
  pinMode(button0Pin,   INPUT);
  pinMode(button1Pin,   INPUT);
  pinMode(button2Pin,   INPUT);
  pinMode(button3Pin,   INPUT);
  pinMode(button4Pin,   INPUT);
  pinMode(button5Pin,   INPUT);
  pinMode(button6Pin,   INPUT);
  pinMode(button7Pin,   INPUT);
  pinMode(button8Pin,   INPUT);
  pinMode(button9Pin,   INPUT);
  pinMode(button10Pin,  INPUT);
  pinMode(button11Pin,  INPUT);
  pinMode(button12Pin,  INPUT);
  pinMode(button13Pin,  INPUT);
  pinMode(button14Pin,  INPUT);
  pinMode(button15Pin,  INPUT);

  lastInputState0 = canByte0;
  lastInputState1 = canByte1;

}
void loop() {

  canByte0 = 0x00;
  canByte1 = 0x00;

  //Read the state of the pushbutton value
  if (digitalRead(button0Pin)  == HIGH) {
    canByte0 |= (0x01 << 0);
  }
  if (digitalRead(button1Pin)  == HIGH) {
    canByte0 |= (0x01 << 1);
  }
  if (digitalRead(button2Pin)  == HIGH) {
    canByte0 |= (0x01 << 2);
  }
  if (digitalRead(button3Pin)  == HIGH) {
    canByte0 |= (0x01 << 3);
  }
  if (digitalRead(button4Pin)  == HIGH) {
    canByte0 |= (0x01 << 4);
  }
  if (digitalRead(button5Pin)  == HIGH) {
    canByte0 |= (0x01 << 5);
  }
  if (digitalRead(button6Pin)  == HIGH) {
    canByte0 |= (0x01 << 6);
  }
  if (digitalRead(button7Pin)  == HIGH) {
    canByte0 |= (0x01 << 7);
  }
  if (digitalRead(button8Pin)  == HIGH) {
    canByte1 |= (0x01 << 0);
  }
  if (digitalRead(button9Pin)  == HIGH) {
    canByte1 |= (0x01 << 1);
  }
  if (digitalRead(button10Pin) == HIGH) {
    canByte1 |= (0x01 << 2);
  }
  if (digitalRead(button11Pin) == HIGH) {
    canByte1 |= (0x01 << 3);
  }
  if (digitalRead(button12Pin) == HIGH) {
    canByte1 |= (0x01 << 4);
  }
  if (digitalRead(button13Pin) == HIGH) {
    canByte1 |= (0x01 << 5);
  }
  if (digitalRead(button14Pin) == HIGH) {
    canByte1 |= (0x01 << 6);
  }
  if (digitalRead(button15Pin) == HIGH) {
    canByte1 |= (0x01 << 7);
  }
  if (lastInputState0 != canByte0 || lastInputState1 != canByte1)
  {
    Serial.print("\nCAN Byte0: 0x");
    Serial.print(canByte0 < 16 ? "0" : "");
    Serial.print(canByte0, HEX);
    canMsg0.data[0] = canByte0;

    Serial.print(", CAN Byte1: 0x");
    Serial.print(canByte1 < 16 ? "0" : "");
    Serial.println(canByte1, HEX);
    canMsg0.data[1] = canByte1;
    mcp2515.sendMessage(&canMsg0);
  }
  else {mcp2515.sendMessage(&canMsg0); } // this will be transmitting zeros for every 500ms

  lastInputState0 = canByte0;
  lastInputState1 = canByte1;
  delay(500); // adding the delay to the system of buttons till displaying the hex symbol. 
}

There are many differences between the ATmega328p and the ATmega328pb, not just pin configurations. You might as well consider them to be two very different MCUs.

Please explain which differences you tried to take into account, and how.

What does the linked ISP programmer have to do with your question?

Hello, I made changes to the pin configurations only. What changes do you think i should make to make this work fast? I am using the programmer called usbtiny. I am just showing what i was using by sharing the link.

To start with, you probably should use Minicore as the Arduino core, and select the correct board. Fuses have to be changed if the chip is new.

I did use minicore and it says permission denied.

"permission denied" is not an Arduino problem.

2 Likes

ok i m lost then completely. Don't know what to do at this point.

Can please show a picture of the problem....If it is text please copy it and paste it in code tags(the <CODE/> button).

I am able to flash it now and it says it is writing the program on the device but nothing happens.

Sketch uses 4968 bytes (15%) of program storage space. Maximum is 32256 bytes.
Global variables use 367 bytes (17%) of dynamic memory, leaving 1681 bytes for local variables. Maximum is 2048 bytes.
"C:\Users\nakku\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\nakku\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v  -patmega328p -cusbtiny  "-Uflash:w:C:\Users\nakku\AppData\Local\Temp\arduino\sketches\54DBB7F9E639E19D3E75DDABAF4F186D/328pbswitch.ino.hex:i"

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\nakku\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

         Using Port                    : usb
         Using Programmer              : usbtiny
avrdude: usbdev_open(): Found USBtinyISP, bus:device: bus-0:\\.\libusb0-0001--0x1781-0x0c9f
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : USBtiny
         Description     : USBtiny simple USB programmer, https://learn.adafruit.com/usbtinyisp
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9516 (probably m328pb)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: Using SCK period of 10 usec
avrdude: reading input file "C:\Users\nakku\AppData\Local\Temp\arduino\sketches\54DBB7F9E639E19D3E75DDABAF4F186D/328pbswitch.ino.hex"
avrdude: writing flash (4968 bytes):

Writing | ################################################## | 100% 8.26s

avrdude: 4968 bytes of flash written
avrdude: verifying flash memory against C:\Users\nakku\AppData\Local\Temp\arduino\sketches\54DBB7F9E639E19D3E75DDABAF4F186D/328pbswitch.ino.hex:
avrdude: load data flash data from input file C:\Users\nakku\AppData\Local\Temp\arduino\sketches\54DBB7F9E639E19D3E75DDABAF4F186D/328pbswitch.ino.hex:
avrdude: input file C:\Users\nakku\AppData\Local\Temp\arduino\sketches\54DBB7F9E639E19D3E75DDABAF4F186D/328pbswitch.ino.hex contains 4968 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 4.99s

avrdude: verifying ...
avrdude: 4968 bytes of flash verified

avrdude done.  Thank you.

What should happen?

Now that you have figured out how to actually program the device, try uploading the Blink example, after determining which pin the LED is connected to, and making any necessary changes.

I did upload the blink example but nothing happens either. It says it is writing the program to the device.

I have a custom made switch board which i am trying to make it work. I just want to figure out why it is not working when it is working with ATmega328p.

Something might be wrong with the code (wrong LED pin, for example) or the board, but you have posted no information regarding either.

How can you possibly expect someone to help?

So what changed between it not flashing and you getting it to flash ?

what other information should i give. I thought i gave all the info. please tell me what info you need. I changed the LED pin to 7. i also tried 13. but nothing.

I needed to download usb tiny software and change the device signature to 0x1e 0x95 0x16 because it is different than 328p.

Please read and follow the instructions in the "How to get the best out of this forum" post.

Post code, using code tags, a complete schematic and photos of the board. Explain what you have done to debug the problem.

I changed the LED pin to 7. i also tried 13.

Why don't you KNOW which pin the LED is connected to?

I wish i could upload the schematics but i cannot do that. It is custom board for switches so not a development board. I can tell you the pin configurations. if thats okay

Don't bother.

ok!