External Power supply on Nano RP 2040

My board/sketch works fine if it’s connected to the USB port on the PC, but using a power bank or simple charger it false. What’s my problem? Thanks for any help.

Hi @uskd. Please post your full sketch.

I'll provide instructions you can follow to do that:

  1. If you are using Arduino IDE: select Tools > Auto Format from the menus.
  2. Click on the window that contains your sketch code.
  3. Press the Ctrl+A keyboard shortcut (Command+A for macOS users).
    This will select all the text.
  4. Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
    This will copy the selected text to the clipboard.
  5. Open a reply here on this forum topic by clicking the "Reply" button.
  6. Click the <CODE/> icon on the post composer toolbar.
    This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
  7. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the copied code into the code block.
  8. Move the cursor outside of the code block markup before you add any additional text to your reply.
  9. Repeat the above process if your sketch has multiple tabs.
  10. Click the "Reply" button to publish the post.

Thanks for your quick response!!
Hier is the code?

// Arduino Boards– Serielle Basis-Kommunikation
// Zeilenbasiertes Protokoll mit '\n' als Terminator.
#include <Adafruit_SCD30.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TCS34725.h>
#include <Adafruit_BME280.h>
//#include <WiFiS3.h> //am R4
#include <WiFiNINA.h>  //am Nano
#include <U8g2lib.h>
#include <Adafruit_BME680.h>

// Vollbild-Puffer, Hardware-I2C, Standard-SDA/SCL des Boards
// Funktioniert für SSD1315 mit SSD1306-Settings
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(
  U8G2_R0,       // Drehung: R0 = normal, R2 = 180° gedreht
  U8X8_PIN_NONE  // kein separates Reset-Pin
);

constexpr uint32_t BAUD = 115200;
constexpr int ANALOG_PIN = A0;
double SenSCD30[3];
double dataTCS[6];
double dataBME280[3];
double dataBME688[4];
char ssid[] = "MagentaWLAN-AE8G";   // dein WLAN-Name
char pass[] = "xxxxxxxxxxxxxxxxx";  // dein WLAN-Passwort
//char ssid[] = "WORKSHOP"; // dein WLAN-Name
//char pass[] = "qweyxc123"; // dein WLAN-Passwort
int status = WL_IDLE_STATUS;
WiFiServer server(8888);
Adafruit_SCD30 scd30;
Adafruit_BME280 SenBme280;  // I2C
Adafruit_BME680 SenBme688;
uint16_t ii;
Adafruit_TCS34725 tcs1(TCS34725_INTEGRATIONTIME_240MS, TCS34725_GAIN_4X);
// aktuelle Einstellungen merken:
uint8_t integTime = TCS34725_INTEGRATIONTIME_240MS;
uint8_t gainVal = TCS34725_GAIN_4X;
WiFiClient client;
unsigned long lastCheck = 0;
const unsigned long checkInterval = 2000;  // alle 200 ms prüfen

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Wire.begin();
  Wire.setClock(100000);
  // Wenn dein Display die Adresse 0x3D hat, diese Zeile aktivieren:
  // u8g2.setI2CAddress(0x3D);
  u8g2.begin();
  Serial.begin(BAUD);
  while (!Serial) {
    ;  // Warte auf USB-Serial
  }
  delay(100);
  Serial.println("READY");  // Handshake für MATLAB
  // Mit WLAN verbinden
  u8g2.clearBuffer();  // internen Bildschirm-Puffer löschen
  // Schrift einstellen und Text zeichnen
  u8g2.setFont(u8g2_font_ncenB08_tr);  // etwas größere Schrift
  u8g2.drawStr(0, 12, "Hallo WLAN");
  u8g2.sendBuffer();
  while (status != WL_CONNECTED) {
    Serial.print("Verbinde mit WLAN: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(5000);
  }
  Serial.println("WLAN verbunden.");
  IPAddress ip = WiFi.localIP();
  Serial.print("Arduino IP: ");
  Serial.println(ip);
  // Server starten
  server.begin();
  Serial.println("TCP-Server gestartet (Port 8888)");
  // Try to initialize CO2-Sensor
  for (ii = 0; ii < 5; ii++) {
    if (scd30.begin()) {
      Serial.println("SCD30 Found!");
      break;
    }
    Serial.println("Failed to find SCD30 chip");
    delay(120);
  }
  // WICHTIG: Bus im begin() wählen (Adresse ist fest 0x29)
  if (!tcs1.begin()) {
    Serial.println(F("Sensor tc1#1 NICHT gefunden!"));
  } else {
    Serial.println(F("Sensor tcs#1 bereit."));
  }
  if (!SenBme280.begin(0x76)) {
    Serial.println("no BME280");
  } else {
    Serial.println("BME280 found");
  }
  if (!SenBme688.begin(0x77)) {
    Serial.println("no BME688");
  } else {
    Serial.println("BME688 found");
  }
  // BME688 Einstellungen
  SenBme688.setTemperatureOversampling(BME680_OS_8X);
  SenBme688.setHumidityOversampling(BME680_OS_2X);
  SenBme688.setPressureOversampling(BME680_OS_4X);
  SenBme688.setIIRFilterSize(BME680_FILTER_SIZE_3);
  SenBme688.setGasHeater(320, 150);
  static uint8_t x = 0;
  u8g2.clearBuffer();  // internen Bildschirm-Puffer löschen
  // Schrift einstellen und Text zeichnen
  u8g2.setFont(u8g2_font_ncenB08_tr);  // etwas größere Schrift
  u8g2.drawStr(0, 12, "Hallo Arduino");
  u8g2.setFont(u8g2_font_6x10_tr);  // kleinere Schrift
  u8g2.drawStr(0, 28, "OLED 0.96\" SSD1315");
  u8g2.drawStr(0, 40, "I2C mit U8g2");
  u8g2.sendBuffer();  // Puffer an Display senden
  u8g2.setFont(u8g2_font_fur14_tr);
}

void loop() {
  static String serLine, wlanLine;
  //delay(50);
  readMeasurement();
  // Auf Client (Matlab) warten
  if (!client || !client.connected()) {
    WiFiClient newClient = server.available();
    if (newClient) {
      client = newClient;
      Serial.println("Neuer Client verbunden");
    }
  }
  // Beispielantwort ohne Blockieren:
  // if (es_gibt_etwas_zu_senden) client.print("Antwort\n");
  if (client && client.connected()) {
    while (client.available() > 0) {
      char wlanC = client.read();
      if (wlanC == '\r') continue;  // CR ignorieren
      if (wlanC == '\n') {          // Zeilenende
        wlanLine.trim();
        if (wlanLine.length()) handleClientCommand(wlanLine);
        wlanLine = "";  // Buffer leeren
      } else {
        // Einfache Überlaufsicherung (optional)
        if (wlanLine.length() < 200) wlanLine += wlanC;
      }
    }
  }
  // Zeile einlesen (nicht-blockierend)
  while (Serial.available()) {
    char serC = (char)Serial.read();
    if (serC == '\r') continue;  // CR ignorieren
    if (serC == '\n') {          // Zeilenende
      serLine.trim();
      if (serLine.length()) handleCommand(serLine);
      serLine = "";  // Buffer leeren
    } else {
      // Einfache Überlaufsicherung (optional)
      if (serLine.length() < 200) serLine += serC;
    }
  }
  delay(50);
}  // End of Loop

void readMeasurement() {
  //static uint8_t x = 0;
  //static String text;
  uint16_t r1, g1, b1, c1;
  unsigned long now = millis();
  if (now - lastCheck > checkInterval) {
    lastCheck = now;
    if (scd30.dataReady()) {
      if (scd30.read()) {
        SenSCD30[0] = scd30.temperature;
        SenSCD30[1] = scd30.relative_humidity;
        SenSCD30[2] = scd30.CO2;
      } else {
        SenSCD30[0] = -99;
        SenSCD30[1] = -99;
        SenSCD30[2] = -99;
      }
    } else {
      SenSCD30[0] = -999;
      SenSCD30[1] = -999;
      SenSCD30[2] = -999;
    }
  }
  tcs1.getRawData(&r1, &g1, &b1, &c1);
  float ct1 = tcs1.calculateColorTemperature(r1, g1, b1);
  float lux1 = tcs1.calculateLux(r1, g1, b1);
  dataTCS[0] = r1;
  dataTCS[1] = g1;
  dataTCS[2] = b1;
  dataTCS[3] = c1;
  dataTCS[4] = ct1;
  dataTCS[5] = lux1;
  dataBME280[0] = SenBme280.readTemperature();
  dataBME280[1] = SenBme280.readHumidity();
  dataBME280[2] = SenBme280.readPressure();
  if (SenBme688.performReading()) {
    dataBME688[0] = SenBme688.temperature;
    dataBME688[1] = SenBme688.humidity;
    dataBME688[2] = SenBme688.pressure;
    dataBME688[3] = SenBme688.gas_resistance / 1000.0f;
  } else {
    dataBME688[0] = -999;
    dataBME688[1] = -999;
    dataBME688[2] = -999;
    dataBME688[3] = -999;
  }
  updateDisplay();
}

void handleClientCommand(String cmd) {
  String upper = cmd;
  upper.toUpperCase();
  if (upper == "MEAS?") {
    client.print(dataTCS[0]);
    client.print(',');
    client.print(dataTCS[1]);
    client.print(',');
    client.print(dataTCS[2]);
    client.print(',');
    client.print(dataTCS[3]);
    client.print(',');
    client.print(dataTCS[4]);
    client.print(',');
    client.print(dataTCS[5], 2);
    client.print(',');
    client.print(dataBME280[0], 2);
    client.print(',');
    client.print(dataBME280[1], 2);
    client.print(',');
    client.print(dataBME280[2], 2);
    client.print(',');
    client.print(SenSCD30[0]);
    client.print(',');
    client.print(SenSCD30[1]);
    client.print(',');
    client.print(SenSCD30[2], 3);
    client.print(',');
    client.print(dataBME688[0], 2);
    client.print(',');
    client.print(dataBME688[1], 2);
    client.print(',');
    client.print(dataBME688[2], 2);
    client.print(',');
    client.print(dataBME688[3], 2);
    client.println("");
    return;
  }
  if (upper == "PING") {  //Ping kann nur über WIFI gesenset werden
    //Serial.println("PONG"); // Ausgabe for client
    client.println("PONG");
    return;
  }
  if (upper == "LED ON") {
    digitalWrite(LED_BUILTIN, HIGH);
    client.println("OK");
    return;
  }
  if (upper == "LED OFF") {
    digitalWrite(LED_BUILTIN, LOW);
    client.println("OK");
    return;
  }
  if (upper == "A0?" || upper == "ANALOG?") {
    int raw = analogRead(ANALOG_PIN);
    Serial.println(raw);  // Nur Zahl zurückgeben
    return;
  }
  // ECHO
  if (upper.startsWith("ECHO ")) {
    // Original-Case ab Position 5 ausgeben
    Serial.println(cmd.substring(5));
    return;
  }
  // Unbekanntes Kommando
  client.println("ERR:UNKNOWN_CMD");
}

void handleCommand(String cmd) {
  String upper = cmd;
  upper.toUpperCase();
  if (upper == "BME688?") {
    Serial.print("Temperature: ");
    Serial.print(dataBME688[0]);
    Serial.print(" degrees C, ");
    Serial.print("Relative Humidity: ");
    Serial.print(dataBME688[1]);
    Serial.print(" %, ");
    Serial.print("Pressure: ");
    Serial.print(dataBME688[2]);
    Serial.print(" hPa, ");
    Serial.print("Gas: ");
    Serial.print(dataBME688[3]);
    Serial.println(" kOhm");
    return;
  }
  if (upper == "SCD30?") {
    Serial.print("Temperature: ");
    Serial.print(SenSCD30[0]);
    Serial.print(" degrees C, ");
    Serial.print("Relative Humidity: ");
    Serial.print(SenSCD30[1]);
    Serial.print(" %, ");
    Serial.print("CO2: ");
    Serial.print(SenSCD30[2], 3);
    Serial.println(" ppm");
    return;
  }
  if (upper == "MEAS?") {
    Serial.print(dataTCS[0]);
    Serial.print(',');
    Serial.print(dataTCS[1]);
    Serial.print(',');
    Serial.print(dataTCS[2]);
    Serial.print(',');
    Serial.print(dataTCS[3]);
    Serial.print(',');
    Serial.print(dataTCS[4]);
    Serial.print(',');
    Serial.print(dataTCS[5], 2);
    Serial.print(',');
    Serial.print(dataBME280[0], 2);
    Serial.print(',');
    Serial.print(dataBME280[1], 2);
    Serial.print(',');
    Serial.print(dataBME280[2], 2);  //Serial.print(',');
    Serial.println("");
    return;
  }
  if (upper == "PING") {
    Serial.println("PONG");
    return;
  }
  if (upper == "LED ON") {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("OK");
    return;
  }
  if (upper == "LED OFF") {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("OK");
    return;
  }
  if (upper == "A0?" || upper == "ANALOG?") {
    int raw = analogRead(ANALOG_PIN);
    Serial.println(raw);  // Nur Zahl zurückgeben
    return;
  }
  // ECHO
  if (upper.startsWith("ECHO ")) {
    // Original-Case ab Position 5 ausgeben
    Serial.println(cmd.substring(5));
    return;
  }
  // Unbekanntes Kommando
  Serial.println("ERR:UNKNOWN_CMD");
}


void updateDisplay() {
  static uint8_t x = 0;
  static String text;
  u8g2.clearBuffer();  // internen Bildschirm-Puffer löschen
  // Schrift einstellen und Text zeichnen
  text = "CO2: " + String(SenSCD30[2], 1) + " ppm";
  u8g2.drawStr(0, 16, text.c_str());
  text = "Temp: " + String(dataBME280[0], 1) + " °C";
  u8g2.drawStr(0, 32, text.c_str());
  text = "Hum: " + String(dataBME280[1], 1) + " %";
  u8g2.drawStr(0, 48, text.c_str());
  text = "VOC: " + String(dataBME688[3], 1) + " kOhm";
  u8g2.drawStr(0, 64, text.c_str());
  // u8g2.drawStr(0, 28, String(SenSCD30[0], 2));
  //u8g2.drawStr(0, 40, String(SenSCD30[1], 2));
  /*
    // Kleine Animation: wanderndes Kästchen
    u8g2.drawFrame(x, 50, 20, 10);
    x++;
    if (x > 108) x = 0;  // 128 - 20 = 108
  */
  u8g2.sendBuffer();  // Puffer an Display senden
}

Here we have the explanation:

This code will make your program stay in an infinite loop until the serial port is opened in Arduino IDE Serial Monitor (or an equivalent application). The reason that is useful is because on native USB boards like yours, the board is not reset when you open Serial Monitor. That means any serial output printed between the time the program starts running and when you get Serial Monitor open will be lost.

In applications where missing that serial output would be a problem, it's useful to add this code to the sketch in order to make the program wait for the Serial Monitor before running.

However, if you are wanting to run your program when the board is not connected to Serial Monitor, you must remove that while loop to allow the rest of the sketch to run.

Hi,

Thank you very much, and apologise for my stupid question. It was so easy!
Thank' again.

Best regards

Uwe

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

Nothing to apologize about. This is something that many of us have stumbled over at some point.

A confusing thing about it is that most of us always have the useful Serial Monitor view open in Arduino IDE, and don't even think of that as being a potential. So the user doesn't actually pinpoint the specific "good" condition as being specifically that Serial Monitor is running, but instead only that the board is connected to the computer in general. That makes it more difficult to track down the reason for the "bad" state where the sketch program doesn't run.

Regards, Per