Strange AnalogRead Behaviour

TLDR: Arduino Nano RP2040 Connect outputs high varying values on analogRead on Pins A6 and A7 without any change of the analog input voltage.

I've got an Arduino Nano RP2040 Connect and try to measure the analog Voltage from an Sharp IR Distance Sensor.

I'm using an LiPo Battery to power the setup and boost it's voltage to 5V.
Those 5V go to VIN from the Arduino and to the 5V Pin of the IR Sensor.

The Sharp GP2Y0A51SK0F that I'm using should take 5V as power Input and should output something between 1.35V to 1.95V as output depending on the measured distance (Values from the Datasheet).

When i use analogRead on pin A6 or A7 on the Arduino I'm getting pretty unstable values often jumping multiple 10 to 100 steps in both directions.

So I've hooked up my Multimeter and an rotary potentionmeter to the setup.
The Multimeter shows pretty stable Voltage values while the analogRead values still jump around. When my multimeter measures 2V Between GND and the output of the potentiometer the arduino measures 1023 as max analogRead Value.

On page 15 in the datasheet of the Arduino it's said to have an analog Reference Voltage of 3.3V. So analogRead(A6) should output 1023 if the potentiometer is dialed to 3.3V and not to 2V.

Has someone similar experience or some idea what i can do to fix this?

Using Arduino Mbed OS Nano Boards 2.5.2 and WiFiNINA Firmware 1.4.8

Processing: AnalogReadProblems.ino...
Processing: index.h...
Seems like new users are not allowed to upload files so here is my code.

-------index.h-----

const char MAIN_page[] PROGMEM = R"=====(
  <!DOCTYPE html>
<html>
<body>
<b>Smart DrinkingCup Laser Distance Test Page</b>
<h1>AnalogRead Value: <span id="ADCValue">0</span></h1><br>
<h1>Voltage Value: <span id="VOLTAGEValue">0</span>V</h1><br>
<h1>Distance Value: <span id="DISTANCeValue">0</span>mm</h1><br>
<script>

setInterval(function() {
  // Call a function repetatively with 2 Second interval
  getData();
}, 750); //2000mSeconds update rate



function getData() {
  const xhttp = new XMLHttpRequest();
  xhttp.open("GET", "readValues", true);
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      const json_obj = JSON.parse(this.responseText);
      document.getElementById("ADCValue").innerHTML = json_obj.analogValue;
      document.getElementById("VOLTAGEValue").innerHTML = json_obj.voltage;
      document.getElementById("DISTANCeValue").innerHTML = json_obj.distance;
    }
  };
  xhttp.send();
}
</script>
</body>
</html>
)=====";

---analogread.ino------

#include "arduino_secrets.h"
// WiFiNINA - Version: Latest
#include <WiFiNINA.h>

#include "index.h"
/*

*/
char ssid[] = SECRET_SSID;     //  your network SSID (name)
char pass[] = SECRET_PASSWORD;  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  if (WiFi.status() == WL_NO_MODULE) {
    while (true);
  }
  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }
  while (status != WL_CONNECTED) {
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected");
  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");

  Serial.println(ip);

  Serial.println(ip);
  // you're connected now, so print out the data:
  server.begin();

}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients
  while (client) {                             // if you get a client,
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {      // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          Serial.print("CurrentLine ");
          Serial.println(currentLine);
          if (currentLine.endsWith("GET / HTTP/1.1")) {
            Serial.println("Request Webpage");
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            // the content of the HTTP response follows the header:
            client.println(MAIN_page);
            // The HTTP response ends with another blank line:
            //client.println();
            // break out of the while loop:
            break;
          }
          if (currentLine.endsWith("GET /readValues HTTP/1.1")) {
            Serial.println("Request new Analog Values");
            int analogValue = analogRead(A6);
            float voltage = map(analogValue * 1.0, 0.0, 1024.0, 0.0, 3.3);
            float distance = map(voltage, 0.0, 3.3, 20, 150);
            String adcValue = String(analogValue, DEC);
            String voltageValue = String(voltage, 3);
            String distanceValue = String(distance, 3);
            String json = "{\"analogValue\": \"";
            json += adcValue;
            json += "\", \"voltage\": \"";
            json += voltageValue;
            json += "\", \"distance\": \"";
            json += distanceValue;
            json += "\"}";
            Serial.println(json);
            client.println("HTTP/1.1 200 OK");
            client.print("Content-Length: ");
            client.println(json.length());
            client.println("Content-type:text/plane");
            client.println("");
            client.println(json);
            client.println("");
            break;
          }
          if (currentLine.length() == 0) {

          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

      }
    }
    client.stop();
  }
  delay(70);
}

Use a scope to find out more about the true signals.

Pin A0 works fine and uses the whole range from 0 to 3.3V.

This is enough for my project now.

As soon as i get access to an scope I will check it out.

Thanks Dr Diettrich. :slight_smile:

The MT3608 has a 22uF cap. I'd be happier seeing some additional decoupling.