My USB disconnects for my Xiao Seeed nRf

Sketch uses 58020 bytes (7%) of program storage space. Maximum is 811008 bytes.
Global variables use 8352 bytes (3%) of dynamic memory, leaving 229216 bytes for local variables. Maximum is 237568 bytes.
Upgrading target on COM8 with DFU package C:\Users\megan\AppData\Local\Temp\arduino\sketches\B3AEED2AE458457F1D22D0DD1B374C5C\Blink.ino.zip. Flow control is disabled, Single bank, Touch disabled
########################################
########################################
##################################
Activating new firmware
Device programmed.

Hi @mmirkhan. I'm going to ask you to post the full verbose output from an upload.


:exclamation: This procedure is not intended to solve the problem. The purpose is to gather more information.


Please do this:

  1. Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
    The "Preferences" dialog will open.
  2. Uncheck the box next to Show verbose output during: compilation in the "Preferences" dialog.
  3. Check the box next to Show verbose output during: ☐ upload.
  4. Click the "OK" button.
    The "Preferences" dialog will close.
  5. Attempt an upload, as you did before.
  6. Wait for the upload to finish.
  7. Right click on the black "Output" panel at the bottom of the Arduino IDE window.
    A context menu will open.
  8. From the context menu, click Copy All.
  9. Open a forum reply here by clicking the "Reply" button.
  10. 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.
    Code block icon on toolbar
  11. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the upload output into the code block.
  12. Move the cursor outside of the code block markup before you add any additional text to your reply.
  13. Click the "Reply" button to post the output.

Please also post the full sketch you are using when this occurred.

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

  1. Select Tools > Auto Format from the Arduino IDE menus.
    This is done to make the code easier for us to read.
  2. Select Edit > Copy for Forum (Markdown) from the Arduino IDE menus.
  3. In a forum reply here, click on the post composer field.
  4. Press the Ctrl+V keyboard shortcut.
    This will paste the sketch to the post composer.
  5. Move the cursor outside of the code block markup before you add any additional text to your reply.
  6. Repeat the above process if your sketch has multiple tabs.
  7. Click the "Reply" button to post your reply.

When your code requires a library that's not included with the Arduino IDE please post a link to where you downloaded that library from, or if you installed it using Library Manager (Sketch > Include Library > Manage Libraries... in Arduino IDE) then say so and state the full name of the library.

Sketch uses 58020 bytes (7%) of program storage space. Maximum is 811008 bytes.
Global variables use 8352 bytes (3%) of dynamic memory, leaving 229216 bytes for local variables. Maximum is 237568 bytes.
Upgrading target on COM8 with DFU package C:\Users\megan\AppData\Local\Temp\arduino\sketches\7E54EF05860714385ECB2B4699336C7E\sketch_apr27a.ino.zip. Flow control is disabled, Single bank, Touch disabled
########################################
########################################
##################################
Activating new firmware
Device programmed.

Thank you so much for helping, above was the output. Below is the code I used. Not sure why but when I run it it disconnects my USB.


```cpp
#include <Wire.h>
#include <Adafruit_SHT31.h>
#include <SPI.h>
#include <SD.h>

#define TEMPERATURE_COLUMN 0
#define HUMIDITY_COLUMN 1

#define SD_CS_PIN 7

Adafruit_SHT31 sht31 = Adafruit_SHT31();

File dataFile;

void setup() {
  Serial.begin(9600);

  // Initialize SD card
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("SD initialization failed!");
    return;
  }
  Serial.println("SD initialization done.");

  // Initialize SHT31 sensor
  if (!sht31.begin()) {
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
  Serial.println("SHT31 found.");

  // Create a new file on the SD card
  dataFile = SD.open("data.csv", FILE_WRITE);
  if (dataFile) {
    // Write headers to the file
    dataFile.println("Temperature,Humidity");
    dataFile.close();
  } else {
    Serial.println("Error opening data.csv");
  }
}

void loop() {
  // Read temperature and humidity from the sensor
  float temperature = sht31.readTemperature();
  float humidity = sht31.readHumidity();

  // Print the values to serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C, Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  // Write the values to the CSV file
  writeToCSV(temperature, humidity);

  delay(5000);  // Adjust delay as needed
}

void writeToCSV(float temperature, float humidity) {
  dataFile = SD.open("data.csv", FILE_WRITE);
  if (dataFile) {
    // Write data to the file
    dataFile.print(temperature);
    dataFile.print(",");
    dataFile.println(humidity);
    dataFile.close();
  } else {
    Serial.println("Error writing to data.csv");
  }
}

22

11

Not sure if this might be useful

Thanks for the information. That makes things more clear. My hypothesis was that you were experiencing a problem I have had while using the board, which is that it doesn't produce a serial port if the sketch program doesn't have an #include directive for Adafruit_TinyUSB.h. However, I can see that your sketch does not have that problem because it uses the "Wire" library, which in turn uses the "Adafruit TinyUSB Library". So that's not it.

I tried your sketch, but unfortunately I don't have an SHT31 on hand so it only gets to this part and then stays there in an infinite loop, not producing the disconnection you experienced:

So I'm limited in my ability to assist you. Hopefully some of the other forum helpers are more knowledgeable in this subject.

I did notice a problem in your code here:

If the initialization of the SD card fails, the return statement causes the setup function to immediately exit and the loop function to run. It doesn't make sense to do that because the loop function tries to use the SHT31, which has not been initialized since the sht31.begin() call was skipped, and also trying to use the SD card even though it was not initialized.

So I would recommend changing that code to something like this:

  // Initialize SD card
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("SD initialization failed!");
    while(true) {}  // Do not continue with program.
  }

It might be that the SD card initialization failed for you (which you wouldn't have any direct indication of since the problem with the serial port means you won't see that "SD initialization failed!" message, and that the program attempting to use the uninitialized SHT31 or SD card crashed the program, which results in the loss of the serial port (this is because on the Seeed XIAO nRF52840, the USB stack runs on the same microcontroller as the sketch program).

My recommendation is to try to "bisect" the problem by checking to see if the problem occurs when using each of the components of your project individually:

Make a simple sketch that only writes the text "Hello, world!" to the SD card.

Make a simple sketch that only reads the temperature and humidity from the SHT31 and prints it to Serial Monitor.

If you find that the problem still occurs with one of those simple sketches, this will allow you to focus your troubleshooting efforts on that specific code. You can share this minimal demonstration here on the forum and that will be very useful to the helpers.

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