Why does my UNO stop communicate when I unplug USB

I have:
Arduino UNO
Can controller MCP2515 / SPI
Oled with 1306 chip / on I2C

Have mostly Copy/Paste code to achieve UNO to send a string to MCP2515
and read the returned message on the CAN and print the value on my OLED.

This works fine and I get the OLED to show the message as long as my UNO is connected with USB to my computer,
(The TX-led blink every second and the CAN value updates)
But as soon as I disconnect the usb and power my UNO from Powerbank or DC-source it only updates once and then keep showing the first reed value from CAN and I have to reset or flip the power to get a new reeding.

I will upload my code here when I have made some clean up in my comments, Its probably very ugly code but it seem to work (half way at least)

Ty in advance!


#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h> //HL
#include <Adafruit_GFX.h> //HL
#include <Adafruit_SSD1306.h>   //HL

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

struct can_frame canMsg;
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);


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

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  // Serial.println("Example: Write to CAN");
  // Serial.println("ID  DLC   DATA");  //HL
}

void loop() {

  display.clearDisplay();

  canMsg1.can_id  = 0x60B;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x40;
  canMsg1.data[1] = 0x81;
  canMsg1.data[2] = 0x60;
  canMsg1.data[3] = 0x00;
  canMsg1.data[4] = 0x00;
  canMsg1.data[5] = 0x00;
  canMsg1.data[6] = 0x00;
  canMsg1.data[7] = 0x00;

 //HLTiwhile (!Serial);
 //Serial.begin(115200);
  
  mcp2515.sendMessage(&canMsg1);
 // mcp2515.sendMessage(&canMsg2);


if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print("     ");
    
    for (int i = 4; i<canMsg.can_dlc; i)  {  // print the data. HL ändrat 0 till 4 , tagit bort ++ efter i, 
      
      
      Serial.println(canMsg.data[i],HEX);     // lagt till ln efter print
      Serial.print(" ");

      display.display();

      display.setTextSize(2);             // Draw 2X-scale text
      display.setTextColor(SSD1306_WHITE);

      display.println("Batt. SoC");
      display.println("  ");

   // display.setCursor(40,20);             // Start at top-left corner

  // display.setTextColor(SSD1306_WHITE);
      display.print("   ");
  display.setTextSize(3);             // Draw 2X-scale text

  display.print(canMsg.data[i],DEC);

    display.println(" %");
        display.println(" ");

  // display.display();
  delay(1000);   //HL 2s-->1s

    }

}
}
 

Show a wiring diagram of the NON working version.

And that includes the power supply....

Which Arduino pin is the power bank output connected to?

    for (int i = 4; i < canMsg.can_dlc; i)  { // print the data. HL ändrat 0 till 4 , tagit bort ++ efter i,

Does something in this for loop change the value of canMsg.can_dlc? If not, it looks like it will loop forever.

With the code you posted and the wiring diagram you show, it should operate the same way whether powered by USB or via Vin. So something else is going on.
What is this 8,4V power source?

I don't think so, I did change that line from:

    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data

because its only one value Im interested in
oterwise I get eight values ex. 40 80 60 54 00 00 00 00 and I only want 54

2s Li Batteries,,

Yes it is like the USB connection to my computer simulates a serial connection and when I cut that connection it stops with all communication on SPI ..?

That would be 7.4V.
Are you sure the batteries are good, charged and can supply enough current for everything?

Fully charged is 4,2V each at 3,7V they are about 20% SoC and 3,4 empty (near harmful for battery)

So you checked the batteries, the voltage and current?

Yes It is 2x 20700 of good quality they can take charge at about 4A and give plenty more. I have a lot of those and I have tried with different once under this project.

Do you have a multimeter/ DMM so you can measure the 5V pin of the Nano?
If yes, does it read close to 5V when you use the batteries?

8,19V on battery
8,19V on battery under load
5,024V on 5V-pin on Arduino

But I think its another problem,, I will check my code and come back, now it won't update the values unless I have serial print on and serial window open in IDE

thanks a lot for your time this far I will try some things and get back in afternoon with updated code and maybe new questions...

If you are doing an if (!Serial) in your code then it will stop it from working when you disconnect the USB

Not on an UNO, the UNO has no way to detect the state of the USB connection.

Right. So I'm out of debugging suggestions.

OK, I have noticed that the new value from the CAN-bus does not update in the serial monitor either, it is the value that I receive from the first run of the program that repeat itself even in the serial monitor.

So either the program does not call on the string 60B more than once or the saved value from the returned string 58B is just saved and does not update more than once.

I will repost the code here since I have made some changes:

#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h> //HL
#include <Adafruit_GFX.h> //HL
#include <Adafruit_SSD1306.h>   //HL

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

struct can_frame canMsg;
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);


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

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
 canMsg1.can_id  = 0x60B;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x40;
  canMsg1.data[1] = 0x81;
  canMsg1.data[2] = 0x60;
  canMsg1.data[3] = 0x00;
  canMsg1.data[4] = 0x00;
  canMsg1.data[5] = 0x00;
  canMsg1.data[6] = 0x00;
  canMsg1.data[7] = 0x00;


  // Serial.println("Example: Write to CAN");
  // Serial.println("ID  DLC   DATA");  //HL
}

void loop() {


  display.clearDisplay();



 //HLTiwhile (!Serial);
 //Serial.begin(115200);
  
  mcp2515.sendMessage(&canMsg1);
 // mcp2515.sendMessage(&canMsg2);


if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
   // Serial.print(canMsg.can_id, HEX); // print ID
   // Serial.print(" "); 
   // Serial.print(canMsg.can_dlc, HEX); // print DLC
   // Serial.print(" ");
    
    for (int i = 4; i<canMsg.can_dlc; i)  {  // print the data. HL ändrat 0 till 4 , tagit bort ++ efter i, 
      
      
     //HLOn Serial.println(canMsg.data[i],HEX);     // lagt till ln efter print
        //HLOn    Serial.println(canMsg.data[i],DEC);     // lagt till ln efter print

     //Serial.print(" ");

      display.display();

      display.setTextSize(2);             // Draw 2X-scale text
      display.setTextColor(SSD1306_WHITE);

    display.setCursor(0,0);             // Start at top-left corner -->middle

      display.println("Batt.  SoC");
      display.println("  ");

    display.setCursor(0,25);             // Start at top-left corner -->middle

  // display.setTextColor(SSD1306_WHITE);
      display.print("   ");
  display.setTextSize(4);             // Draw 4X-scale text

  display.print(canMsg.data[i],DEC);

    display.println("%");
        display.println(" ");

  // display.display();
  delay(1000);   //HL 2s-->1s

    }

}
}
 

Thank you so much for the help!