Arduino MKR1010 does not recognized by USB

Hello
I've been working with my 1010 for some time.
But today when I tried to download a sketch to the MKR the IDE (via web arduino Create) said it is not more connected.
I disconnected from all shields leaving the arduino only connected to th ePC
I'm using windows 10.
I restarted the PC, re installed the create agent and nothing was changed.

I discovered about the quick double reset and did it. The board is connected to the IDE the orange LED (not the charge one) is blinking slowly and when I try to download new sketch it is stacked again until I press double rest quickly.

What is the problem ? and how to solve it ?

The tricky thing about the boards with native USB functionality like your MKR WiFi 1010 is that 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.

Try this:

  • From the menu on the left side of the Arduino Web Editor, click "Sketchbook".
  • Click the "New Sketch" button.
  • Press the reset button on your MKR WiFi 1010 quickly twice.
  • Click the "Upload" button.

Does the problem still occur? If not, you know the problem is something in the sketch you were uploading previously. If you post the sketch here, we may be able to help you find the problematic code.

1 Like

Hi
I've tried what you suggested and I still have a problem but a slight different one.
After I opened a new sketch, made fast double reset and tried to upload it, the uploader got stacked and the orange LED is still flashing slowly.
I'm attaching a a screenshot of the editor.

Ori

OK - Its actually work !!
I refrshed the web browser and did what you asked and now the new sketch ("hello world" sketch :slight_smile: ) is working.

here is my original code/ Can someone understand where is the problem with it ?

/*
Sketch generated by the Arduino IoT Cloud Thing "LED"
https://create.arduino.cc/cloud/things/a3799e6c-75da-4de8-b7d4-a137dd0a327e

Arduino IoT Cloud Properties description

The following variables are automatically generated and updated when changes are made to the Thing properties

bool LED;
bool stepRight;
float pressure;
float flow;

Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#define LED_PIN 2
int PressurePin = 5;
int flowsensor = 4; // Sensor Input
boolean dir = true;// gre
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned long currentTime;
unsigned long cloopTime;

//motor
//======
// Setup the builtin LED, the step and direction pins.
const int stepPin = 9;
const int dirPin = 1;
// Set the PWM duty cycle and counter pin.
const int dutyCycle = 25;
const int counterPin = 0 ;
const int gearRatio = 26.85; //for the large motor
const int stepAngle = 1.8 ; //degrees
const int microStepRatio = 0.25; //current configuratoin of pollulu with M0 and M1 floating
const int stepsPerRevolution = (360/stepAngle)*(1/microStepRatio)*gearRatio;

// Define step targets and counters.
int stepTarget;
volatile int stepsTaken = 0;
// Store the direction the stepper is moving (one or zero).
int stepperDir;

void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

pinMode(flowsensor, INPUT_PULLUP);
pinMode(4, INPUT);
//digitalWrite(flowsensor, HIGH);
attachInterrupt(digitalPinToInterrupt(flowsensor), flowf, RISING ); // Setup Interrupt
interrupts() ;// Enable interrupts
currentTime = millis();
cloopTime = currentTime;

//Motor
//======
// Setup the stepper.
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, LOW);
pinMode(stepPin, OUTPUT);
stepperDir = 1;
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();

pinMode(LED_PIN, OUTPUT);
//pinMode(Pin0, OUTPUT);
//pinMode(Pin1, OUTPUT);
//pinMode(Pin2, OUTPUT);
//pinMode(Pin3, OUTPUT);
}

void flowf () // Interrupt function
{
flow_frequency++;
}
void loop() {
ArduinoCloud.update();
// Your code here

int sensorVal=analogRead(PressurePin);
float voltage = (sensorVal5.0)/1024.0;
float pressure_pascal = (3.0
((float)voltage-0.46))1000000.0; //calibrate here - recalibrated from 0.475 to 0.46
float pressure_bar = pressure_pascal/10e5;
pressure = pressure_bar
14.5038;

currentTime = millis();
// Every second, calculate and print litres/hour

if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
l_hour = (flow_frequency * 60 / 10); // (Pulse frequency x 60 min) / 10Q = flowrate in L/hour - acording to the datsheet of our flow meter
flow=l_hour;
flow_frequency = 0; // Reset Counter
Serial.print(currentTime, DEC); // time stamp
Serial.print(" , ");
Serial.print(l_hour, DEC); // Print litres/hour
Serial.print(" ,L/hour,");
Serial.print("Pressure = ,");
Serial.print(pressure_bar);
Serial.print(" , bars, ");
Serial.print ("psi, ");
Serial.print (pressure);
Serial.println();
}

}

void onLEDChange() {
// Do something
digitalWrite(LED_PIN, LED);
Serial.print("The light is ");
if (LED) {
Serial.println("ON");
} else {
Serial.println("OFF");
}
}

void onStepRightChange() {
//set direction clockwise
if (stepperDir == 1 ) {
digitalWrite(dirPin, HIGH );
stepperDir = 0;
} else {
digitalWrite(dirPin, LOW );
stepperDir = 1;
}

Serial.println("stepping");
Serial.println(stepsPerRevolution);
//digitalWrite(dirPin, LOW );
//stepperDir = 1;
for (int x=0; x<stepsPerRevolution ; x++) //stepsPerRevolution 21480
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
//Serial.println("stepping");

}

}

Would you mind also posting the contents of thingProperties.h? I took a look at your sketch and no obvious problems stood out at me. I'd like to run the program on my MKR WiFi 1010 to see if I can reproduce the problem, then bisect to narrow down the cause.

Attached is the code of thingProperties.h

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char THING_ID[] = "a3799e6c-75da-4de8-b7d4-a137dd0a327e";

const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)

void onLEDChange();
void onStepRightChange();

bool LED;
bool stepRight;
float pressure;
float flow;

void initProperties(){

ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(LED, READWRITE, ON_CHANGE, onLEDChange);
ArduinoCloud.addProperty(stepRight, READWRITE, ON_CHANGE, onStepRightChange);
ArduinoCloud.addProperty(pressure, READ, 1 * SECONDS, NULL);
ArduinoCloud.addProperty(flow, READ, 1 * SECONDS, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Here's the minimal demonstration of the problem:

void setup() {
  volatile byte x = 1 / 0;  // declared volatile so the unused variable isn't optimized away by the compiler
}

void loop() {}

Unfortunately, Arduino Web Editor suppresses compiler warnings. If it didn't do that, you would have had a nice clue to the problem:

C:\Users\per\Desktop\sketch_may5a\sketch_may5a.ino:41:55: warning: division by zero [-Wdiv-by-zero]

 const int stepsPerRevolution = (360 / stepAngle) * (1 / microStepRatio) * gearRatio;

Because you've used the int data type, this line:

const int microStepRatio = 0.25; //current configuratoin of pollulu with M0 and M1 floating

is equivalent to:

const int microStepRatio = 0;

thus the divide by zero. Apparently that crashes the processor and therefore no USB code runs to create the serial port.

Similarly, this line:

const int gearRatio = 26.85; //for the large motor

is the equivalent of:

const int gearRatio = 26; //for the large motor

and this line:

const int stepAngle = 1.8 ;  //degrees

is the equivalent of:

const int stepAngle = 1;  //degrees

You should use the float data type if you want to use numbers with decimal points. Also note that when decimal fractions are assigned to an integer data type they are truncated, not rounded (thus 1.8 becomes 1 instead of the expected 2). You can achieve rounding by using round().

Thanks a lot.
Indeed a stupid mistake.
Its a pity that the web compiler does not issue a warning/error on such mistake

You're welcome.

lightov:
Indeed a stupid mistake.

Ideally, we would never make mistakes, but that's not going to happen. The next best thing is to treat the mistakes we do make as an opportunity to learn. I learned that one possible cause of the USB code not running is a divide by zero. I had never experimented with divide by zero before to determine exactly what the result is.

lightov:
Its a pity that the web compiler does not issue a warning/error on such mistake

Even with the Arduino IDE, the default setting is for no warnings (you can enable them via e File > Preferences > Compiler warnings). I find compiler warnings extremely helpful for pointing out problems like this which might otherwise take a lot of work to track down. However, not all warnings indicate a serious problem. Many Arduino libraries generate warnings, and it often is better to just ignore those warnings instead of trying to fix inconsequential problems in somebody else's code. Beginners tend to get very freaked out by warnings. They don't understand that there is a difference between a warning and an error. So this might be the reasoning behind Arduino suppressing compiler warnings.