Codes Works in Arduino IDE, But not Platform IO

May I ask whether the platform IDE or avrdude and other tools are faulty? It would be much appreciated if this could solve the problem. My development board model is Arduino UNO R3.

After PlatformIO is burned, it will be stuck at the following code when executing the program:

u8g2.firstPage(); do{ u8g2.setCursor(0, 0); u8g2.setFont(u8g2_font_ncenR12_tf); u8g2.setCursor(10, 35); u8g2.print(“Waiting”); } while (u8g2.nextPage()); delay(200);

The complete code is as follows:

#define BLINKER_BLE

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include "U8g2lib.h"
#include <Blinker.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <MFRC522.h>
#include <IRremote.h>


#define DHTPIN 7      // 指定 DHT11 数据引脚
#define DHTTYPE DHT11 // 指定传感器类型为 DHT11
float temp, humi;
bool mark = false;

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
DHT dht(DHTPIN, DHTTYPE);
IRrecv remote(6);
MFRC522 rfid(10, 9);

String ReadCardUID(MFRC522 *_name){
    String _CardUID = "";
    for (byte _i = 0; _i < _name->uid.size; _i++)
    {
        if (_name->uid.uidByte[_i] < 0x10)
            _CardUID += "0";
        _CardUID += String(_name->uid.uidByte[_i], HEX);
    }
    return _CardUID;
}

boolean IsNewCard(MFRC522 *_name){
    if (!_name->PICC_IsNewCardPresent())
        return false;
    if (!_name->PICC_ReadCardSerial())
        return false;
    return true;
}

void setup(){
    pinMode(3, OUTPUT); // 继电器 1
    pinMode(4, OUTPUT); // 继电器 2
    pinMode(5, OUTPUT); // LED

    SPI.begin();
    rfid.PCD_Init();

    remote.enableIRIn();
    dht.begin();
    u8g2.setI2CAddress(0x3C * 2);
    u8g2.begin();
    u8g2.enableUTF8Print();

    u8g2.firstPage();

    do{
        u8g2.setCursor(0, 0);
        u8g2.setFont(u8g2_font_ncenR12_tf);
        u8g2.setCursor(20, 35);
        u8g2.print("Gxdung");

    } while (u8g2.nextPage());
    delay(1000);
}

void loop(){

    while (mark == false){
        String uid;
        if (IsNewCard(&rfid)){
            uid = ReadCardUID(&rfid);
            rfid.PICC_HaltA();
            rfid.PCD_StopCrypto1();
        }

        if (uid == "b3682896"){
            u8g2.firstPage();
            do{
                u8g2.setCursor(0, 0);
                u8g2.setFont(u8g2_font_ncenR12_tf);
                u8g2.setCursor(20, 35);
                u8g2.print("User 01");
            } while (u8g2.nextPage());
            delay(1000);
            mark = true;
            return;
        }

        else if (uid == "53a7b595"){
            u8g2.firstPage();
            do{
                u8g2.setCursor(0, 0);
                u8g2.setFont(u8g2_font_ncenR12_tf);
                u8g2.setCursor(20, 35);
                u8g2.print("User 02");
            } while (u8g2.nextPage());
            delay(1000);
            mark = true;
            return;
        }

        else if (uid == "43324596"){
            u8g2.firstPage();
            do{
                u8g2.setCursor(0, 0);
                u8g2.setFont(u8g2_font_ncenR12_tf);
                u8g2.setCursor(20, 35);
                u8g2.print("User 03");
            } while (u8g2.nextPage());
            delay(1000);
            mark = true;
            return;
        }
        else{
            u8g2.firstPage();
            do{
                u8g2.setCursor(0, 0);
                u8g2.setFont(u8g2_font_ncenR12_tf);
                u8g2.setCursor(10, 35);
                u8g2.print("Waiting");
            } while (u8g2.nextPage());
            delay(200);
        }
    }

    while (mark == true){
        temp = dht.readTemperature(); // 读取温度值
        humi = dht.readHumidity();    // 读取湿度值
        u8g2.firstPage();
        do{
            u8g2.setCursor(0, 0);
            u8g2.setCursor(20, 25);
            u8g2.print("Temp:");
            u8g2.setCursor(70, 25);
            u8g2.print(temp);
            u8g2.setCursor(20, 45);
            u8g2.print("Humi:");
            u8g2.setCursor(70, 45);
            u8g2.print(humi);
        } while (u8g2.nextPage());

        if (remote.decode()){
            unsigned long value = remote.decodedIRData.decodedRawData;

            if (value == 3125149440){
                digitalWrite(3, !digitalRead(3));
                u8g2.firstPage();
                do{
                    u8g2.setCursor(36, 35);
                    u8g2.print("Relay 1");
                } while (u8g2.nextPage());
                delay(500);
            }
            else if (value == 3108437760){
                digitalWrite(4, !digitalRead(4));
                u8g2.firstPage();
                do{
                    u8g2.setCursor(36, 35);
                    u8g2.print("Relay 2");
                } while (u8g2.nextPage());
                delay(500);
            }
            else if (value == 3091726080){
                digitalWrite(5, !digitalRead(5));
                u8g2.firstPage();
                do{
                    u8g2.setCursor(50, 35);
                    u8g2.print("LED");
                } while (u8g2.nextPage());
                delay(500);
            }

            remote.resume();
            delay(50);
        }
    }
}

Have you tested whether the MFRC522 works in its own minimal sketch?

Yes, it can run in Arduino IDE. But in PIO, the statement after while (mark == true) cannot be executed

Well mark = true; is only set when a card is recognized and the UID can be matched to one of the required ones. Can you add Serial.println() statements to see if it detects a new card at all.

On the Arduino IDE, the card can be detected and read in a loop, but the PIO cannot.
I think there is nothing wrong with my code.

True. I just want to isolate where the problem is. It’s a difference whether it’s in the used display library or the card library.

Can you provide this info:

  • In the Arduino IDE, Tools->Board->Board Managr → “avr” → core version is what?
  • Arduino IDE “Tools” menu settings are what?
  • version of card and u2g8 library used in the Arduino IDE (can e.g. be read from the build log, File->Preferences → Verbose Build)
  • full platformio.ini

Mixly: avrdude Version is 6.3-20190619

Use version 1.0 of the SPI library
Use version 1.0 of the Wire library
Use version 2.32.15 of the U8g2 library
Use version 0.3.9 of the Blinker library
Use version 1.0 of the SoftwareSerial library
Using version 1.0.2 of the Adafruit Unified Sensor library
Use version 1.4.2 of the DHT sensor library
Using version 1.4.8 of the MFRC522 library
Use version 3.7.1 of the IRremote library

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 "D:\Program Files\mixly2.0-win32-x64\arduino-cli\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

Using Port : COM7
Using Programmer : arduino
Overriding Baud Rate : 115200
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 : Arduino
     Description     : Arduino
     Hardware Version: 3
     Firmware Version: 4.4
     Vtarget         : 0.3 V
     Varef           : 0.3 V
     Oscillator      : 28.800 kHz
     SCK period      : 3.3 us

Arduino IDE: avrdude: Version 6.3-20190619

Use version 1.0 of the library SPI
Use version 1.0 of the library Wire
Use version 2.33.15 of library U8g2
Use version 0.3.10230510 of the library blinker-iot-blinker-library-10ff617
Use version 1.0 of the library SoftwareSerial
Using version 1.1.9 of the library AdafruitSensor
Use version 1.4.4 of the library DHTSensor
Using library MFRC522 version 1.4.10
Use version 4.1.2 of the library IRremote

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\gxdung\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

     Using Port                    : COM7
     Using Programmer              : arduino
     Overriding Baud Rate          : 115200
     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 : Arduino
     Description     : Arduino
     Hardware Version: 3
     Firmware Version: 4.4
     Vtarget         : 0.3 V
     Varef           : 0.3 V
     Oscillator      : 28.800 kHz
     SCK period      : 3.3 us

Platform IO ini:
image

It would be very grateful if this could solve the problem.

Try and match these versions exactly in the platformio.ini’s lib_deps by using e.g.

lib_deps =
   olikraus/U8g2@2.33.15
   ; ...

statements.

That’s not the AVR core version.

My bad. My AVR core version is 1.8.6