Arduino Nano R4 with u8g2 and LED-Display 128x64 on macos

I have a RC-board with an OLED display to transmit data over nRF24 with an Nano every that works fine. (Pull-up resistors are soldered)

Using the same board with a Nano R4 and adjusted code shows a weired behaviour:

Uploading works, but the board hangs. Unplugging the board and powering from a walladapter lets the board work partially: LED blinks, the frame is displayed, string and loopcounter are not.

When I Insert

  u8g2.setDrawColor(1);
  u8g2.setCursor(4,32);
  u8g2.print("***");
  u8g2.print(loopcounter);

in setup, nothing is displayed any more, but the LED still blinks, loop is not blocked.

My code: (modified Blink example, transmitter code has >1000 lines)

 /*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */
#include <U8g2lib.h>

// give it a name:
int led = LED_BUILTIN;
uint8_t loopcounter = 13; // dummy

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup() 
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  u8g2.begin();
  u8g2.drawFrame(4,4,40,10);

  u8g2.sendBuffer();
}

void loop() 
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  u8g2.setCursor(4,32);
  u8g2.print(loopcounter++);
  delay(1000);               // wait for a second
}

Error message:


Opening DFU capable USB device...
Device ID 2341:0074
Run-Time device DFU version 0101
Claiming USB DFU (Run-Time) Interface...
Setting Alternate Interface zero...
Determining device status...
DFU state(0) = appIDLE, status(0) = No error condition is present
Device really in Run-Time Mode, send DFU detach request...
Device will detach and reattach...
Opening DFU USB Device...
Claiming USB DFU Interface...
Setting Alternate Interface #0 ...
Determining device status...
DFU state(2) = dfuIDLE, status(0) = No error condition is present
DFU mode device DFU version 0101
Device returned transfer size 64
Copying data from PC to DFU device
Download	[=========================] 100%        53684 bytes
Download done.
DFU state(7) = dfuMANIFEST, status(0) = No error condition is present
DFU state(2) = dfuIDLE, status(0) = No error condition is present
Done!
processing.app.SerialException: Error opening serial port '/dev/cu.usbmodem2101'.
	at processing.app.Serial.<init>(Serial.java:154)
	at processing.app.Serial.<init>(Serial.java:82)
	at processing.app.SerialMonitor$2.<init>(SerialMonitor.java:132)
	at processing.app.SerialMonitor.open(SerialMonitor.java:132)
	at processing.app.AbstractMonitor.resume(AbstractMonitor.java:132)
	at processing.app.Editor.resumeOrCloseSerialMonitor(Editor.java:2158)
	at processing.app.Editor.access$1300(Editor.java:116)
	at processing.app.Editor$UploadHandler.run(Editor.java:2115)
	at java.lang.Thread.run(Thread.java:748)
Caused by: jssc.SerialPortException: Port name - /dev/cu.usbmodem2101; Method name - openPort(); Exception type - Port not found.
	at jssc.SerialPort.openPort(SerialPort.java:167)
	at processing.app.Serial.<init>(Serial.java:143)
	... 8 more
Error opening serial port '/dev/cu.usbmodem2101'.

Calling the serial monitor shows:

Board at /dev/cu.usbmodem2101 is not available

Controlling the port in terminal at the same time with

ls /dev/cu*

shows an open port:

/dev/cu.usbmodem2101

macos 14, u8g2 version 2.35.50

What is going wrong?

Hi @ruediheimlicher

I was not able to reproduce this. When I run your sketch on my board, it blinks after uploading, even when the board is only powered by the USB cable.

My suggestion for troubleshooting the problem you encountered would be to add some Serial.println calls to determine whether the u8g2.begin call is hanging. Something like this:

  Serial.begin(9600);
  while (!Serial) {}  // Wait for serial port to be opened in Serial Monitor.
  pinMode(led, OUTPUT);

  Serial.println("Initializing display...");
  if (u8g2.begin()) {
    Serial.println("Display initialization successful!");
  } else {
    Serial.println("Display initialization failed :(");
  }

I was able to reproduce this. I was able to fix it by adding a u8g2.setFont call to the setup function and a u8g2.sendBuffer call after the u8g2.print call:

#include <U8g2lib.h>

// give it a name:
int led = LED_BUILTIN;
uint8_t loopcounter = 13; // dummy

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup() 
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  u8g2.begin();
  u8g2.setFont(u8g2_font_ncenB08_tr);

  u8g2.drawFrame(4,4,40,10);

  u8g2.sendBuffer();
}

void loop() 
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  u8g2.setCursor(4,32);
  u8g2.print(loopcounter++);
  u8g2.sendBuffer();
  delay(1000);               // wait for a second
}

Give that sketch a try.

It seems to be a quirk of Arduino IDE 1.x. I am able to reproduce it by uploading while the port of the board is open in Serial Monitor. I don't think it causes any problems, so you can ignore it if you like. If it bothers you, just update to the modern Arduino IDE 2.x and the problem will no longer occur.

Thanks for the tip, upgrading did the trick. I seems to work well now.

You are welcome. I'm glad it is working now.

Regards, Per