Device can't be found on COM port after updating IDE to 2.3.0

After I updates the IDE to version 2.3.0, I found that every time I upload a sketch, there would be an error message telling me there is no device found from COM port. Then I tried to double click the reset button on board (MKR1310) before uploading a sketch, it did work as I saw the device showed in the COM port, but during sketch uploading the device disconnected, could anyone please help me figure out it?

Hi @andreibailey33. Please try this:

  1. Select File > Examples > 01.Basics > BareMinimum from the Arduino IDE menus.
    A minimal sketch will open in an IDE window.
  2. Connect your MKR WAN 1310 board to your computer with a USB cable.
  3. Double click the reset button on the board.
  4. Select the board and port from the Arduino IDE menus.
  5. Select Sketch > Upload from the Arduino IDE menus.
  6. Wait for the upload to finish successfully.

Now reply on this forum thread to tell us whether the problem of the device disconnecting occurred this time after the upload of the "BareMinimum" sketch.

Hi ptillisch, thanks a lot for your quick reply. After uploading the "BareMinimum"sketch, I still got "no device found on COM...", a picture is attached for your reference. The picture is about IDE 2.3.0 on my Macbook.

I've also tried different USB cables and computers. And when I tried on my windows PC, where IDE is 2.2.1, BareMinimum can be successfully uploaded to MKRWAN1310 board, but when I tried another sketch, the MKR1310 board can't be connected, and computer gave alert like "your USB device can not be recognized"

OK, that is unfortunate.

I'm going to ask you to post the full verbose output from a failed upload attempt.


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


Please do this:

  1. Select File > Examples > 01.Basics > BareMinimum from the Arduino IDE menus.
    A minimal sketch will open in an IDE window.
  2. Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
    The "Preferences" dialog will open.
  3. Uncheck the box next to Show verbose output during: compilation in the "Preferences" dialog.
  4. Check the box next to Show verbose output during: ☐ upload.
  5. Click the "OK" button.
  6. Connect your MKR WAN 1310 board to your computer with a USB cable.
  7. Double click the reset button on the board.
    The LED marked "L" on the board should now start pulsing.
  8. Select the board and port from the Arduino IDE menus.
  9. Select Sketch > Upload from the Arduino IDE menus.
  10. Wait for the upload to fail.
  11. You will see a "Upload error: ..." notification at the bottom right corner of the Arduino IDE window. Click the "COPY ERROR MESSAGES" button on that notification.
  12. Open a forum reply here by clicking the "Reply" button.
  13. 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
  14. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the error output from the upload into the code block.
  15. Move the cursor outside of the code block markup before you add any additional text to your reply.
  16. Click the "Reply" button to post the output.

This time BareMinimum seemed successfully uploaded and there is no upload error notification. I pasted the output below. However, I found the debug button is grey and doesn't work this time.

Global variables use 2988 bytes (9%) of dynamic memory, leaving 29780 bytes for local variables. Maximum is 32768 bytes.
Performing 1200-bps touch reset on serial port /dev/cu.usbmodem142401
Waiting for upload port...
No upload port found, using /dev/cu.usbmodem142401 as fallback
"/Users/amandaqiao/Library/Arduino15/packages/arduino/tools/bossac/1.7.0-arduino3/bossac" -i -d --port=cu.usbmodem142401 -U true -i -e -w -v "/private/var/folders/lg/dwtpxpt11f34sd39z3c25fmc0000gn/T/arduino/sketches/3FC664B847EEC369F74791A573D48E9D/BareMinimum.ino.bin" -R
Set binary mode
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
version()=v2.0 [Arduino:XYZ] Apr 30 2019 12:41:57
chipId=0x10010005
Connected at 921600 baud
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
Atmel SMART device 0x10010005 found
write(addr=0x20004000,size=0x34)
writeWord(addr=0x20004030,value=0x10)
writeWord(addr=0x20004020,value=0x20008000)
Device       : ATSAMD21G18A
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
Chip ID      : 10010005
version()=v2.0 [Arduino:XYZ] Apr 30 2019 12:41:57
Version      : v2.0 [Arduino:XYZ] Apr 30 2019 12:41:57
Address      : 8192
Pages        : 3968
Page Size    : 64 bytes
Total Size   : 248KB
Planes       : 1
Lock Regions : 16
Locked       : readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
none
readWord(addr=0x41004018)=0
Security     : false
Boot Flash   : true
readWord(addr=0x40000834)=0x7000a
BOD          : true
readWord(addr=0x40000834)=0x7000a
BOR          : true
Arduino      : FAST_CHIP_ERASE
Arduino      : FAST_MULTI_PAGE_WRITE
Arduino      : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
chipErase(addr=0x2000)
done in 0.849 seconds

Write 11832 bytes to flash (185 pages)
write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2000, size=0x1000)

[==========                    ] 34% (64/185 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x3000, size=0x1000)

[====================          ] 69% (128/185 pages)write(addr=0x20005000,size=0xe40)
writeBuffer(scr_addr=0x20005000, dst_addr=0x4000, size=0xe40)

[==============================] 100% (185/185 pages)
done in 0.090 seconds

Verify 11832 bytes of flash with checksum.
checksumBuffer(start_addr=0x2000, size=0x1000) = 16fd
checksumBuffer(start_addr=0x3000, size=0x1000) = 6ed1
checksumBuffer(start_addr=0x4000, size=0xe38) = 3025
Verify successful
done in 0.011 seconds
CPU reset.
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
writeWord(addr=0xe000ed0c,value=0x5fa0004)

Then I tried uploading another sketch but failed, and found the error message.

Sketch uses 187156 bytes (71%) of program storage space. Maximum is 262144 bytes.
Global variables use 9612 bytes (29%) of dynamic memory, leaving 23156 bytes for local variables. Maximum is 32768 bytes.
Performing 1200-bps touch reset on serial port /dev/cu.usbmodem142401
Waiting for upload port...
No upload port found, using /dev/cu.usbmodem142401 as fallback
"/Users/amandaqiao/Library/Arduino15/packages/arduino/tools/bossac/1.7.0-arduino3/bossac" -i -d --port=cu.usbmodem142401 -U true -i -e -w -v "/private/var/folders/lg/dwtpxpt11f34sd39z3c25fmc0000gn/T/arduino/sketches/A7CEAC0AC5B93B9F35DAC4314C614635/120224_Amax_Fpeak_newmaxfx_BPF.ino.bin" -R
Set binary mode
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
version()=v2.0 [Arduino:XYZ] Apr 30 2019 12:41:57
chipId=0x10010005
Connected at 921600 baud
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
Atmel SMART device 0x10010005 found
write(addr=0x20004000,size=0x34)
writeWord(addr=0x20004030,value=0x10)
writeWord(addr=0x20004020,value=0x20008000)
Device       : ATSAMD21G18A
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
Chip ID      : 10010005
version()=v2.0 [Arduino:XYZ] Apr 30 2019 12:41:57
Version      : v2.0 [Arduino:XYZ] Apr 30 2019 12:41:57
Address      : 8192
Pages        : 3968
Page Size    : 64 bytes
Total Size   : 248KB
Planes       : 1
Lock Regions : 16
Locked       : readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
none
readWord(addr=0x41004018)=0
Security     : false
Boot Flash   : true
readWord(addr=0x40000834)=0x7000a
BOD          : true
readWord(addr=0x40000834)=0x7000a
BOR          : true
Arduino      : FAST_CHIP_ERASE
Arduino      : FAST_MULTI_PAGE_WRITE
Arduino      : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
chipErase(addr=0x2000)
done in 0.847 seconds

Write 187164 bytes to flash (2925 pages)
write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2000, size=0x1000)

[                              ] 2% (64/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x3000, size=0x1000)

[=                             ] 4% (128/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x4000, size=0x1000)

[=                             ] 6% (192/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x5000, size=0x1000)

[==                            ] 8% (256/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x6000, size=0x1000)

[===                           ] 10% (320/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x7000, size=0x1000)

[===                           ] 13% (384/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x8000, size=0x1000)

[====                          ] 15% (448/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x9000, size=0x1000)

[=====                         ] 17% (512/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0xa000, size=0x1000)

[=====                         ] 19% (576/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0xb000, size=0x1000)

[======                        ] 21% (640/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0xc000, size=0x1000)

[=======                       ] 24% (704/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0xd000, size=0x1000)

[=======                       ] 26% (768/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0xe000, size=0x1000)

[========                      ] 28% (832/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0xf000, size=0x1000)

[=========                     ] 30% (896/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x10000, size=0x1000)

[=========                     ] 32% (960/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x11000, size=0x1000)

[==========                    ] 35% (1024/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x12000, size=0x1000)

[===========                   ] 37% (1088/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x13000, size=0x1000)

[===========                   ] 39% (1152/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x14000, size=0x1000)

[============                  ] 41% (1216/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x15000, size=0x1000)

[=============                 ] 43% (1280/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x16000, size=0x1000)

[=============                 ] 45% (1344/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x17000, size=0x1000)

[==============                ] 48% (1408/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x18000, size=0x1000)

[===============               ] 50% (1472/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x19000, size=0x1000)

[===============               ] 52% (1536/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x1a000, size=0x1000)

[================              ] 54% (1600/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x1b000, size=0x1000)

[=================             ] 56% (1664/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x1c000, size=0x1000)

[=================             ] 59% (1728/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x1d000, size=0x1000)

[==================            ] 61% (1792/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x1e000, size=0x1000)

[===================           ] 63% (1856/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x1f000, size=0x1000)

[===================           ] 65% (1920/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x20000, size=0x1000)

[====================          ] 67% (1984/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x21000, size=0x1000)

[=====================         ] 70% (2048/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x22000, size=0x1000)

[=====================         ] 72% (2112/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x23000, size=0x1000)

[======================        ] 74% (2176/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x24000, size=0x1000)

[======================        ] 76% (2240/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x25000, size=0x1000)

[=======================       ] 78% (2304/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x26000, size=0x1000)

[========================      ] 80% (2368/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x27000, size=0x1000)

[========================      ] 83% (2432/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x28000, size=0x1000)

[=========================     ] 85% (2496/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x29000, size=0x1000)

[==========================    ] 87% (2560/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2a000, size=0x1000)

[==========================    ] 89% (2624/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2b000, size=0x1000)

[===========================   ] 91% (2688/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2c000, size=0x1000)

[============================  ] 94% (2752/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2d000, size=0x1000)

[============================  ] 96% (2816/2925 pages)write(addr=0x20005000,size=0x1000)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2e000, size=0x1000)

[============================= ] 98% (2880/2925 pages)write(addr=0x20005000,size=0xb40)
writeBuffer(scr_addr=0x20005000, dst_addr=0x2f000, size=0xb40)

[==============================] 100% (2925/2925 pages)
done in 1.288 seconds

Verify 187164 bytes of flash with checksum.
checksumBuffer(start_addr=0x2000, size=0x1000) = 688f
checksumBuffer(start_addr=0x3000, size=0x1000) = 4e92
checksumBuffer(start_addr=0x4000, size=0x1000) = 3f48
checksumBuffer(start_addr=0x5000, size=0x1000) = d517
checksumBuffer(start_addr=0x6000, size=0x1000) = 8bf0
checksumBuffer(start_addr=0x7000, size=0x1000) = 1117
checksumBuffer(start_addr=0x8000, size=0x1000) = 45ff
checksumBuffer(start_addr=0x9000, size=0x1000) = cc6e
checksumBuffer(start_addr=0xa000, size=0x1000) = d1c4
checksumBuffer(start_addr=0xb000, size=0x1000) = ef65
checksumBuffer(start_addr=0xc000, size=0x1000) = cc9b
checksumBuffer(start_addr=0xd000, size=0x1000) = 6da
checksumBuffer(start_addr=0xe000, size=0x1000) = 1f16
checksumBuffer(start_addr=0xf000, size=0x1000) = 14a6
checksumBuffer(start_addr=0x10000, size=0x1000) = 3786
checksumBuffer(start_addr=0x11000, size=0x1000) = 8d44
checksumBuffer(start_addr=0x12000, size=0x1000) = 6f87
checksumBuffer(start_addr=0x13000, size=0x1000) = 58cf
checksumBuffer(start_addr=0x14000, size=0x1000) = a2a
checksumBuffer(start_addr=0x15000, size=0x1000) = 74a3
checksumBuffer(start_addr=0x16000, size=0x1000) = 1f62
checksumBuffer(start_addr=0x17000, size=0x1000) = 3f6f
checksumBuffer(start_addr=0x18000, size=0x1000) = bfe6
checksumBuffer(start_addr=0x19000, size=0x1000) = a980
checksumBuffer(start_addr=0x1a000, size=0x1000) = 7c76
checksumBuffer(start_addr=0x1b000, size=0x1000) = 17c4
checksumBuffer(start_addr=0x1c000, size=0x1000) = 7aa1
checksumBuffer(start_addr=0x1d000, size=0x1000) = 54fd
checksumBuffer(start_addr=0x1e000, size=0x1000) = 2e39
checksumBuffer(start_addr=0x1f000, size=0x1000) = b8c3
checksumBuffer(start_addr=0x20000, size=0x1000) = 81f9
checksumBuffer(start_addr=0x21000, size=0x1000) = 10e8
checksumBuffer(start_addr=0x22000, size=0x1000) = d9b4
checksumBuffer(start_addr=0x23000, size=0x1000) = 5487
checksumBuffer(start_addr=0x24000, size=0x1000) = 4b9a
checksumBuffer(start_addr=0x25000, size=0x1000) = fabc
checksumBuffer(start_addr=0x26000, size=0x1000) = 9a31
checksumBuffer(start_addr=0x27000, size=0x1000) = 5325
checksumBuffer(start_addr=0x28000, size=0x1000) = 61c9
checksumBuffer(start_addr=0x29000, size=0x1000) = 94ab
checksumBuffer(start_addr=0x2a000, size=0x1000) = 4d35
checksumBuffer(start_addr=0x2b000, size=0x1000) = ac63
checksumBuffer(start_addr=0x2c000, size=0x1000) = 505d
checksumBuffer(start_addr=0x2d000, size=0x1000) = a0e5
checksumBuffer(start_addr=0x2e000, size=0x1000) = 7ad
checksumBuffer(start_addr=0x2f000, size=0xb1c) = 9b99
Verify successful
done in 0.165 seconds
CPU reset.
readWord(addr=0)=0x20007ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10010305
writeWord(addr=0xe000ed0c,value=0x5fa0004)

There is a change in Arduino IDE 2.3.0. You now must select the debug probe you have connected between your computer and the MKR WAN 1310 board from the IDE's Tools > Programmer menu.

This indicates the problem is caused by something in that sketch program. Please post your full sketch.

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.

The tricky thing about the boards with native USB capability like your board is the USB code that creates the CDC serial port is running on the same microcontroller as your sketch. This means your sketch code can break the USB code, or stop it from running. When that happens, the board either no longer produces a serial port, or produces a port but doesn't respond to uploads.

As you found, the board can be recovered from this state by performing the "double click" reset technique, which stops the sketch program from running and activates the bootloader program that allows uploads. But if you upload the same sketch that breaks the CDC serial port functionality once more, then the problem just comes right back. This is why the problem doesn't occur when you upload the "BareMinimum" example, since that sketch program doesn't interfere with the board's USB functionality.

/* ****************************************************************************
 * Created By: Tauseef Ahmad
 * Create On: 8th January, 2022
 * 
 * How To Interface ADXL335 Analog Accelerometer Sensor With Arduino
 * Tutorial: https://youtu.be/HrIBGJep-1o
 * 
 *****************************************************************************/
#include "SHM.h"
//--------------------------------------------------------------
#define xPin A0
#define yPin A1
#define zPin A2
//--------------------------------------------------------------
//calibrated minimum and maximum Raw Ranges for each axis
//use calibration.ino file to get these values
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
int xMin = 615;
int xMax = 966;
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
int yMin = 620;
int yMax = 969;
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
int zMin = 638;
int zMax = 947;
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//--------------------------------------------------------------
// Take multiple samples to reduce noise
//static const uint16_t N = 512; //MUST BE EXP 2;
//--------------------------------------------------------------
static constexpr double G = 9.80665;           // Gravitational Constant
static const int f = 2;                        // Frequency Target
static const double sampleTime = (Ts * 1000);  //Sampling Period in Miliseconds
//Period for sampling
const long period = 10000;  //10 Sec
static const double EMA_Alpha_low = 0.61;
static const double EMA_Alpha_High = 0.81;
/********** ACCELEROMETER THRESHOLDS **********/
//const double xNoise = 0.01;
//const double yNoise = 0.01;
//const double zNoise = 0.01;

void setup() {
  //analogReference(EXTERNAL);
  Serial.begin(9600);
}


void loop() {
  // ACCELERATION SAMPLING//--
  //float xAccelMax = 0.0;
  //float yAccelMax = 0.0;
  //float zAccelMax = 0.0;

  // MAXIMUM FREQUENCY
  float zFreq = 0.0;

  // PHASE SHIFT
  float zPhase = 0.0;

  //Array's for imaginary components of FFT to prevent overflow.
  double imagZ[N] = {};

  double xAccelArray[N] = { 0 };
  double yAccelArray[N] = { 0 };
  double zAccelArray[N] = { 0 };

  double xAccelLPF[N] = { 0 };
  double yAccelLPF[N] = { 0 };
  double zAccelLPF[N] = { 0 };

  double xAccelHPF[N] = { 0 };
  double yAccelHPF[N] = { 0 };
  double zAccelHPF[N] = { 0 };

  double xAccelBP[N] = { 0 };
  double yAccelBP[N] = { 0 };
  double zAccelBP[N] = { 0 };
  //---------------------------------------------------------
  int i;

  for (int i = 0; i < N; i++) {
    int xRaw = analogRead(xPin);
    int yRaw = analogRead(yPin);
    int zRaw = analogRead(zPin);

    //xRaw/=samples;
    //yRaw/=samples;
    //zRaw/=samples;
    //--------------------------------------------------------------
    //Convert raw values to 'milli-Gs"
    //Convert value of RawMin to -1000
    //Convert value of RawMax to 1000
    long xMilliG = map(xRaw, xMin, xMax, -1000, 1000);
    long yMilliG = map(yRaw, yMin, yMax, -1000, 1000);
    long zMilliG = map(zRaw, zMin, zMax, -1000, 1000);
    //--------------------------------------------------------------
    // re-scale to fractional Gs
    float x_g_value = xMilliG / 1000.0;
    float y_g_value = yMilliG / 1000.0;
    float z_g_value = zMilliG / 1000.0;

    // // Apply threshold to remove noise ***DETERMINE THRESHOLGS***
    //x_g_value = (abs(x_g_value) <= xNoise) ? 0 : x_g_value;
    //y_g_value = (abs(y_g_value) <= yNoise) ? 0 : y_g_value;
    //z_g_value = (abs(z_g_value) <= zNoise) ? 0 : z_g_value;

    //Converts G's to m/s/s
    double xAccel = x_g_value * G;
    double yAccel = y_g_value * G;
    double zAccel = z_g_value * G;

    // Store values in arrays
    xAccelArray[i] = xAccel;
    yAccelArray[i] = yAccel;
    zAccelArray[i] = zAccel;

    xAccelLPF[i] = EMA_Alpha_low * xAccelArray[i] + (1 - EMA_Alpha_low) * xAccelLPF[i];
    yAccelLPF[i] = EMA_Alpha_low * yAccelArray[i] + (1 - EMA_Alpha_low) * yAccelLPF[i];
    zAccelLPF[i] = EMA_Alpha_low * zAccelArray[i] + (1 - EMA_Alpha_low) * zAccelLPF[i];

    xAccelHPF[i] = EMA_Alpha_High * xAccelLPF[i] + (1 - EMA_Alpha_High) * xAccelHPF[i];
    yAccelHPF[i] = EMA_Alpha_High * yAccelLPF[i] + (1 - EMA_Alpha_High) * yAccelHPF[i];
    zAccelHPF[i] = EMA_Alpha_High * zAccelLPF[i] + (1 - EMA_Alpha_High) * zAccelHPF[i];

    xAccelBP[i] = xAccelHPF[i] - xAccelLPF[i];
    yAccelBP[i] = yAccelHPF[i] - yAccelLPF[i];
    zAccelBP[i] = zAccelHPF[i] - zAccelLPF[i];

    //HPF(xAccelLPF, Fch, Ts, N);
    //HPF(yAccelLPF, Fch, Ts, N);
    //HPF(zAccelLPF, Fch, Ts, N);
    Serial.print(xAccelBP[i]);
    Serial.print("\t");
    Serial.print(yAccelBP[i]);
    Serial.print("\t");
    Serial.print(zAccelBP[i]);
    //Serial.print("Accel: ");
    //Serial.print(xAccelArray[i]);
    //Serial.print("\t");
    //Serial.print(yAccelArray[i]);
    //Serial.print("\t");
    //Serial.print(zAccelArray[i]);
    //Serial.println("");
    //Delay between samples


    //Apply Low Pass Filter to data to remove High Freq Components

    //--------------------------------------------------------------
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    ///Serial.print(xAccel,2);
    //Serial.print(x_g_value,2);
    ///Serial.print(" m/s^2");
    ///Serial.print("\t");
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    ///Serial.print(yAccel,2);
    //Serial.print(y_g_value,2);
    ///Serial.print(" m/s^2");
    ///Serial.print("\t");
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    ///Serial.print(zAccel,2);
    //Serial.print(z_g_value,2);
    ///Serial.print(" m/s^2");
    //Serial.println();
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //--------------------------------------------------------------
    // Find maximum values among every 512 samples
    //double xAccelMax = findMaxAmongBlocks(xAccelBP, N, N);
    //double yAccelMax = findMaxAmongBlocks(yAccelBP, N, N);
    //double zAccelMax = findMaxAmongBlocks(zAccelBP, N, N);
    // Print the acceleration values from the arrays

    //Serial.print("Accel: ");
    //Serial.print(xAccelMax);
    //Serial.print("\t");
    //Serial.print(yAccelMax);
    //Serial.print("\t");
    //Serial.print(zAccelMax);
    //Serial.println("");

    //Determine Max Acceleration from sensor values.
    //xAccelMax = findMax(xAccelArray);
    //yAccelMax = findMax(yAccelArray);
    //zAccelMax = findMax(zAccelArray);

    //Serial.println(" ");
    //Serial.print("Max Accel: ");
    //Serial.print(xAccelMax);
    //Serial.print("\t");
    //Serial.print(yAccelMax);
    //Serial.print("\t");
    //Serial.print(zAccelMax - G);
    imagZ[i] = 0.0;
  }
  //FFT Constructor For Z direction of Accelerometer
  arduinoFFT Z_FFT(zAccelBP, imagZ, N, Fs);

  //Remove DC Component Of the data.
  Z_FFT.DCRemoval();

  //Apply Hamming window to the data to improve resolution
  Z_FFT.Windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD);

  //Compute the FFT of the data
  Z_FFT.Compute(FFT_FORWARD);

  //Find Maximum Frequency Of Data
  zFreq = float(Z_FFT.MajorPeak());

  //Serial.print(zAccelMax - G);
  //Serial.print("\t");
  //Serial.print("Peak Freq: ");
  Serial.println(float(Z_FFT.MajorPeak()));
  //Serial.print("\t");
  Serial.println();  // Add a newline at the end for better formatting

  //Find Bin of Peak Frequency
  int binZ = zFreq * N / Fs;

  //Check for NaN values in the frequency outputs.
  checkNan(zFreq);
  delay(Ts * 1000);
}
//Delay between samples
//float start = millis();
//while((millis() - start) < period);

#ifndef SHM_h
#define SHM_h

/********** LIBRARIES **********/
#include "Arduino.h"
#include <algorithm>
#include <math.h>
#include <arduinoFFT.h>
#include <LoRa.h>
#include <stdio.h>
#include <iostream> 
#include <RTCZero.h>
#include "DHT.h"

static const uint16_t N = 512; //MUST BE EXP 2
static constexpr double pi = 3.14159265358979323846; 
/********** FILTER PARAMETERS **********/
constexpr double Fc = 5; // Cutoff Frequency for LPF
constexpr double Fch = 2;//cutoff Frequency for HPF
static const int maxFreq = 25; //Max Frequency Detected by FFT
static constexpr double Fs = maxFreq * 2; // Nyquist Sampling Freq = 2 * MaxFrequency
static const double Ts = 1.0 / Fs; //Sampling Period


//double findMax(double data[]) {
  //double maxVal = 0;
  //for (int i = 0; i < N; ++i) {
    //double absVal = abs(data[i]);
    //if (absVal > abs(maxVal)) {
      //maxVal = data[i];
   // }
  //}
  //return maxVal;
//}

double findMaxAmongBlocks(double array[], int size, int blockSize) {
  double maxVal = array[0];
  for (int i = 1; i < size; i++) {
    if (array[i] > maxVal) {
      maxVal = array[i];
    }
    // If i is a multiple of blockSize, print the maximum and reset maxVal
    if (i % blockSize == 0) {
      Serial.println(maxVal);
      maxVal = array[i + 1];
    }
  }
  return maxVal;
}

void checkNan(float& freq) {
 if (isnan(freq)) {
  freq = 0;
 }
}

void LPF(double *data) {
	float alpha = 1.0 / ((2.0*pi*Fc*Ts) + 1.0);
	double output = data[0];
	for (uint16_t i = 1; i < N; i++)
	{
		output = alpha*data[i] + (1-alpha) * output;
    data[i] = output;
	}
}

void HPF(double *data, double Fch, double Ts, int size ){
  double RC = 1.0/(2.0*pi*Fch);
  double alpha = RC/(RC+Ts);
  double preoutput = data[0];
  for (uint16_t i = 1; i < N; i++){
  double output = alpha * (preoutput + data[i] - data[i-1]);
  preoutput = output;
  data[i] = output;
  }
}
#endif

Many thanks for your explanation. I pasted the main codes and a h. file here. But this situation (device cannot be connected and windows keeps noticing me I have a malfunctioned USB device) only happens since a few days ago and before I could still upload a sketch. I also attached the library here, but not all the library files are used for the codes.

Andrei libraries.zip (2.8 MB)

The problem is caused by running out of SRAM.

Each of these arrays require N * sizeof(double) bytes (4 kB) of SRAM. The MKR WAN 1310 has 32 kB of SRAM and some of that is already used up by other parts of the program so this code is not feasible.

If you reduce the value of N, the sketch will no longer soft brick your board.

Here is a minimum demonstration sketch:

// Demonstration of soft bricking of native USB board caused by intializing array larger than available SRAM.
// Press and release the reset button on the board twice quickly and then upload a valid sketch to recover the board.
void setup() {
  volatile double foo[13 * 512] = {};  // volatile keyword used to prevent the unused array from being optimized away by compiler.
}
void loop() {}

Many thanks for the detailed solution you provided! I further moved the arrays out of loop function to make them as global variables, then the board worked again.

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

Regards,
Per

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