How to upload a sketch and externally power up an Arduino MKR 1010

Hi guys,

I’m having some troubles in powering up my Arduino MKR Wifi1010 externally, which means not by directly connect my device to the pc. Beside this, I’ve also some doubts on how to upload the sketch of my project on my board. Actually, everytime I need to execute this sketch, I have to re-compile and re-upload the latter from the pc (through USB), while I’d like to find out how to “save” the sketch inside the memory of Arduino itself, and make it run autonomously.
I’ve checked around googling and searching for related topics, but I actually didn’t find a plain, clear procedure which would suit for an MKR 1010; I’m sorry if this is a commonly asked question or if it is a too basic procedure - as I hope it is :slightly_frowning_face:
I’d be grateful if you could help me.

Venus

You power it by connecting 0V to ground and +5V to the 5V pin.

You have not shared your code, before you do please read
How to use this forum
Especially item #7 on posting code.

You are asking for advice about secret code we cannot see. There are some pretty smart people on here but I am confident none of them are telepathic.

I suspect you have something like:

while(!Serial.available);

In your code, it will get stuck there while serial is not available, such as when it's not got a USB connection.

ENUSHK:
I'd like to find out how to "save" the sketch inside the memory of Arduino itself, and make it run autonomously.

That's exactly what happens when you upload a sketch to your MKR WiFi 1010 using the Arduino IDE.

PerryBebbington:
You power it by connecting 0V to ground and +5V to the 5V pin.

That’s exactly what I did, but still…
Also connecting directly the 5V pin of the MKR 1010 to the 5V pin of an ordinary Arduino Uno doesn’t make the work: my MKR doesn’t react. It works properly only if connected through the alimentation (Micro-USB) to the PC.

PerryBebbington:
You have not shared your code, before you do please read
How to use this forum
Especially item #7 on posting code.

I’m sorry, I thought this issue was independent of the code, that works quite fine and doesn’t seem to be problematic. I didn’t report it, because it’s quite long, full of redundancies and has to be fixed to be fully readable…
In synthesis, it’s designed for an antitheft program system, which is connected to PIR sensors to detect movements and deliver notifications to a Blynk app.
Here’s the code:

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG

#define PIR1 8
#define PIR2 6
#define PIR3 7 
#define LED1 5
#define LED_RISK 4
#define SWITCH 10

#define Notify_Pin 5

bool state = false;
int counter = 0;
int status = WL_IDLE_STATUS;

const long  timer_interval = 1000;     // interval of time for serial communication
const long  interval_medium = 10000;  // interval of time for medium risk
const long  interval_long = 50000; 
const long  extinction = 30000;

unsigned long previousMillis = 0;
unsigned long telapsed = 0;

char auth[] = "XXXXXXXXXXXXXXXXX";
char ssid[] = "XXXXXXXXXXXXXXXXX";
char pass[] = "XXXXXXXXXXXXXXXXX";

WidgetTerminal terminal(V1);
#define BLYNK_PRINT Serial 

void setup(){
  pinMode(LED1, OUTPUT); 
  pinMode(LED_RISK, OUTPUT); 
  pinMode(PIR1, INPUT); 
  pinMode(PIR2, INPUT);
  pinMode(Notify_Pin, OUTPUT);
  pinMode(SWITCH, OUTPUT);


  while (!Serial) {}
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();
  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);   
  Start_Virtual();

}

BLYNK_WRITE(V3) { // step
  int stepValue = param.asFloat(); // set the global step value
  int set_tolerance = stepValue;
  } 
  
void loop(){

  Blynk.run();
  Disabler();
   
  unsigned long currentMillis = millis(); // prende il tempo all'inizio del loop

  if(digitalRead(PIR1) == HIGH || digitalRead(PIR2) == HIGH ){

    if(!state){
      digitalWrite(LED1, HIGH);
      state = true;
      counter++;
      telapsed = currentMillis-previousMillis;
      
      if ((currentMillis - previousMillis <= interval_medium) && (counter > 2)) {
        Serial.print("N. conteggi\t");
        Serial.println(counter);
        Serial.println("Medium Risk");
        Blynk.virtualWrite(V1,"Detected:", counter, "\t-->Medium Risk\n");

        //terminal.println("xxxxx");
        //terminal.flush();
      }
      if ((currentMillis - previousMillis <= interval_long) && counter >= 6) {
        Risk_Detected();
        Blynk.virtualWrite(V1,"Detected:", counter, "\t-->High Risk\n");
        }
      }
      previousMillis = currentMillis;
    } else if (digitalRead(PIR1) == LOW) {
    if(state){
      digitalWrite(LED1, LOW);
      state = false;    
    }   
  }

  if (!state) {
    if ((currentMillis - previousMillis >= extinction)) {
        previousMillis = currentMillis;
        Warning_Extinction();
      }
  }

  if (currentMillis - previousMillis == timer_interval) {
    BLYNK_WRITE(V1);
    Serial_Send();
  }
  delay(10);
   
}





void Disabler() {
  if (digitalRead(SWITCH) == HIGH) {
    counter = 0;
  }
  if (counter <= 6) {
    digitalWrite(LED_RISK, LOW);
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void Risk_Detected() {
  Serial.println("High Risk");
  Serial.print("N. conteggi\t");
  Serial.println(counter);
  digitalWrite(Notify_Pin, HIGH);
  digitalWrite(LED_RISK, HIGH);
  Blynk.notify("HIGH RISK detected!");
}

void Warning_Extinction () {
  counter = 0;
  Serial.println("Warning extinguished");  
  Serial.print("N. conteggi\t");
  Serial.println(counter);
}

BLYNK_READ(V2) {
  Blynk.virtualWrite(V2, digitalRead(LED1));
}

void Serial_Send() {
  String content = "";  //null string constant ( an empty string )
  char character;
  while(Serial.available()) {
    character = Serial.read();
    content.concat(character);
    //Serial.println();  
   }
  if (content != "") {
    Blynk.virtualWrite(V1, content);
   } 
  terminal.flush();
}

void Start_Virtual() {
  IPAddress ip = WiFi.localIP();
  Blynk.virtualWrite(V1,"-------------------\n");
  Blynk.virtualWrite(V1,"Successfully connected to Wifi\n");
  Blynk.virtualWrite(V1,"Blynk v ", BLYNK_VERSION, ": Device started\n");
  Blynk.virtualWrite(V1,"IP Address: ", ip, "\n");
  Blynk.virtualWrite(V1,"-------------------\n");
}


BLYNK_WRITE(V1) {
  String content = "";  //null string constant ( an empty string )
  char character;
  while(Serial.available()) {
    character = Serial.read();
    content.concat(character);
    Serial.println();  
   }
  if (content != "") {
    Blynk.virtualWrite (V1, content);
   } 
}

EDIT: I tried commenting the code line

while (status != WL_CONNECTED)

And I connected the alimentation of the MKR 1010 to a common Power bank. Now it works!
Thank you very much :slight_smile:
Have a nice day.

alimentation

You're the second person in a week who's use that word to mean power supply. As I now know the other person is French I conclude you are French (apart from that one word I would not know English is not your native language :slight_smile: )

I'm sorry, I thought this issue was independent of the code

You won't believe how many times I've read comments like that for a problem that was caused by code.....

Now it works!

GOOD! :slight_smile:

Have a nice day

And you!
(Is it raining in France? Hasn't stopped here in the UK for weeks!)