Arduino not retaining code after unplugging

I am using an Arduino Nano BLE 33 Sense for my project but I have a problem where the Arduino is not retaining the code after I unplug it. So in short, after uploading if connected to the computer it WORKS, if I unplug it and plug it back in the computer or any other Power sources it DOESN'T WORK. Please help.

Not knowing what your project hardware is, the first thing I do when I suspect a processor problem is to download the file/example / digital / blink without delay.

Try that and see if you get the same results.

A bit confused? Do you mean download a different file and see if it works with that

Post the code, using code tags.

#include <Arduino_APDS9960.h>

int ledState = LOW;
int Buz = 7; // Buzzer pin

unsigned long previousMillis = 0;

const long intervalLong = 1000;
const long intervalMed = 500;
const long intervalShort = 100;

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

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS9960 sensor!");
  }

  // set the LEDs and buzzer pins as outputs
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(Buz, OUTPUT);

  // turn all the LEDs off
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);
  digitalWrite(Buz, LOW); // Initialize buzzer as OFF
}

void loop() {
  unsigned long currentMillis = millis();

  // check if a proximity reading is available
  if (APDS.proximityAvailable()) {
    int proximity = APDS.readProximity();

    if (proximity > 150) {
      if (currentMillis - previousMillis >= intervalLong) {
        previousMillis = currentMillis;

        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }

        digitalWrite(LEDG, ledState);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDB, HIGH);

        // Turn off the buzzer
        digitalWrite(Buz, LOW);
      }
    } else if (proximity > 50 && proximity <= 150) {
      if (currentMillis - previousMillis >= intervalMed) {
        previousMillis = currentMillis;

        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }

        digitalWrite(LEDB, ledState);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, HIGH);

        // Turn off the buzzer
        digitalWrite(Buz, ledState);
      }
    } else {
      if (currentMillis - previousMillis >= intervalShort) {
        previousMillis = currentMillis;

        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }

        digitalWrite(LEDR, ledState);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDG, HIGH);

        // Turn on the buzzer when the blue LED is blinking
        digitalWrite(Buz, ledState);
      }
    }

    Serial.println(proximity);
  }
}

So I just uploaded the Blinkwithoutdelay example and now it works even if I unplug it and plug it back back in, however, when I tried to upload my code it doesn't even work plugged in.

Thank you so much, it is working now after uploading the blink code and reuploading my code, however, I was just wondering if you know if I can power the nano ble 33 with a 9v battery?

Yes, if you change

while (!Serial);

That is where it hangs, waiting for the serial connection to be made.

Got it

I don't really know for sure but I think it's doubtful. 9V batteries have very little energy. You would be better off with 4 AA batteries.

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