analogRead works on nano but no the every

I have a strange issue when reading the input from the arduino nano every. When reading from pins A0 A1 and A2 (have not tried others), I get 1023. However, the exact same code on a nano returns the correct values (536, 60, 642). I stripped down the code to the bare minimum to get the oled to display the outputs and that's it. I have tried on multiple every boards.

#include <SPI.h>
#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 ); 
  
  analogReference( DEFAULT );

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
}

void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
  display.setCursor(0,0);

  display.print(analogRead( A2 ));
  display.print(F(" "));
  display.print(analogRead( A0 ));
  display.print(F(" "));
  display.println(analogRead( A1 ));
  display.display();
}

Hi guy.

What is connected to ports A0 and A2?
If nothing is attached to them, readings may have
undefined values and may vary according to each
arduino board.

RV mineirin

It works reliably with original nano boards. A0 is a voltage sense, A1 is the output of a current sensing IC, and A2 is on a voltage splitter.

This is what the board is on https://easyeda.com/julien_9465/universal_tester_v7_copy

Do all analog inputs work as intended if you read a simple 10KOhm potentiometer?
Maybe it's trivial and you've already tried it.
The code seems genuine.

Hi,
Can you run this code and see what the IDE monitor shows?

#include <SPI.h>
#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 );

  analogReference( DEFAULT );

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
}

void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
  display.setCursor(0, 0);

  display.print(analogRead(A2));
  Serial.print(" A2 = ");
  Serial.print(analogRead(A2));
  display.print(F(" "));
  display.print(analogRead(A0));
  Serial.print("    A0 = ");
  Serial.print(analogRead(A0));
  display.print(F(" "));
  display.println(analogRead(A1));
  Serial.print("    A1 = ");
  Serial.println(analogRead(A1));
  display.display();
}

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

The default reference voltage is different on Nano and Nano Every. You need to specify it with analogReference() to match

Are you sure? It is not mentioned in the link you provided nor in the getting started page:
https://www.arduino.cc/en/Guide/NANOEvery#please-read

Sorry, what not mentioned? Analog pins have comparators that compare your supply voltage to reference voltage. Say your maximum input is 3.3v and your reference voltage is set to 3.3v, then teh value will be 1024. However if your reference voltage is set to 5v then you will never get more than say 675. And Nano and Nano Every have different default reference voltage for some reason.

1 Like

Found it

Thanks! Fixed! I had read the page but didn't realize it overlapped with the UNO Wi-Fi board. I will be submitting a change request to explicitly mention the nano every :slight_smile:

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