arduino-libraries/ArduinoIoTCloud

Library changes pin mode of A0 to INPUT

per1234 opened this issue · 0 comments

Describe the problem

Under the following conditions, the library uses an analog read of pin A0 to seed the pseudorandom number generator:

  • Board does not has a built-in ECCx08 crypto chip
  • Board does not have a hardware random number generator

randomSeed(analogRead(0));

This analog read puts the pin into INPUT mode.

In addition to use as a ADC, analog pins may be used as digital output pins by setting the pin mode to OUTPUT.

🐛 If the user is using pin A0 as an output, the library will break their sketch by changing the pin mode.

To reproduce

  1. Add the following code to the setup function of an ArduinoIoTCloud sketch that calls ArduinoCloud.getInternalTime() or ArduinoCloud.update():
    pinMode(A0, OUTPUT);
  2. Add the following code to the loop function of the sketch:
    static unsigned long blinkTimestamp;
    static byte pinState;
    if (millis() - blinkTimestamp >= 1000) {
      blinkTimestamp = millis();
      if (pinState == LOW) {
        pinState = HIGH;
      } else {
        pinState = LOW;
      }
      digitalWrite(A0, pinState);
    }
  3. Upload the sketch to one of the following boards (tested with UNO R4 WiFi):
    • MKR WiFi 1010
    • NANO 33 IoT
    • Nicla Vision
    • Portenta C33
    • UNO R4 WiFi
  4. Monitor the state of pin A0 with an LED or meter.

🐛 The pin state stops toggling.

Expected behavior

Library does not interfere with user's ability to use pin A0 as an output.

- OR -

Library's impact on pin A0 is clearly documented.

Library version

d4ae0a3

Additional context

Originally reported at https://forum.arduino.cc/t/a0-pin-behavior-uno-r4-wifi/1238998

Related

Workaround

Add the following code to the loop function of your sketch:

  static bool pinModeRestored = false;  // Track state to avoid unnecessary calls to the fairly slow ArduinoCloud.connected()
  if (!pinModeRestored && ArduinoCloud.connected()) {
    pinMode(ledPin, OUTPUT);
    pinModeRestored = true;
  }