[SOLVED] Analog Input Voltage crashing program

Hello,

I am trying to write a simple program to measure RMS AC voltage directly from one of the analog input pins on an Arduino. This is all limited to <3.5VrmsAC, for now just simple sine waves. I was able to get the following code to run on an Arduino UNO r3 successfully, but when I try to upload the same code to an Arduino Nano Every, i'm getting a strange behavior. For input voltage less than 700mVrms, I am getting the output I am expecting, but as soon as I bump up to 750mVrms, the code appears to fail. The TX light stops flashing and the Serial monitor stops updating. This is not a behavior that was observed on the UNO. I was trying to play around with the analogReference value, but without any luck.

Any help is appreciated!

int sensorPin = 0;
int Vmax = 5;
int nbits = 1023;
int Vfact = 1;
int Afact = 1;

#include <LiquidCrystal_I2C.h>  // include Arduino LCD library
#include <Wire.h>

// LiquidCrystal_I2C lcd(0x23,16,2); // Uno
LiquidCrystal_I2C lcd(0x27,16,2); // Nano

void setup() {
  // Set up serial connection
  Serial.begin(9600);
  //lcd.init();
  //lcd.backlight();
  analogReference(VDD);
}

// get maximum reading value
int get_max() {
  int max_v = 0;
  for(int i=0; i<250; i++) {
    int r = analogRead(sensorPin); // read from analog channel 0
    if(max_v < r) max_v = r;
    delayMicroseconds(300);
  }
  return max_v;
}

// main loop
void loop() {

  char buf[10];

  // get amplitude (maximum - or peak value)
  sensorPin = 0;
  int v = get_max();
  sensorPin = 2;
  int a = get_max();

  float V = v;
  V = ((V*Vmax*Vfact)/nbits)/sqrt(2);
  
  float A = a;
  A = ((A*Vmax*Afact)/nbits)/sqrt(2);

  // LCD Output
  char Vbuf[16], Abuf[16];
  char Vf[7], Af[7];
  
  dtostrf(V,6,4,Vf);
  dtostrf(A,6,4,Af);

  snprintf(Vbuf,sizeof(Vbuf),"Vrms: %s V",Vf);
  //lcd.setCursor(0,0);
  //lcd.print(Vbuf);
  Serial.println(Vbuf);

  snprintf(Abuf,sizeof(Abuf),"Arms: %s A",Af);
  //lcd.setCursor(0,1);
  //lcd.print(Abuf);
  Serial.println(Abuf);

  delay(250);

}

Negative voltages at an input to an Arduino, be it Nano, Mega, Uno, or Every, will quickly damage the processor.

So are you saying that this would just not be possible with an Arduino? At least in my current configuration.

Any idea why it was working on the UNO but not the Nano Every?

Inputs are protected by diodes to prevent over/under voltage. Best guess, you weren’t running a large enough signal to cause forward conduction of the diode-to-ground, but someone with more detailed knowledge of the particular processor construction would know better. I simply know the signal range is from 0 to +5, nothing more or less.

@camsysca is saying "Negative voltages at an input to an Arduino, be it Nano, Mega, Uno, or Every, will quickly damage the processor." Period. How that damage manifests itself may not be consistent. But it will be there.

I can say with a high degree of confidence that it is not working on the Uno. You are not reading negative voltages on an analog input. I invite you to maka a minimal sketch and demonstrate for yourself that negative numbers are not returned from analogRead.

You just haven't damaged the Uno in a way that you have noticed yet.

1 Like

From the ATMega328 processor (Uno) datasheet:

Absolute Maximum Ratings Table 33-1. Absolute Maximum Ratings

Voltage on any Pin except RESET with respect to Ground -0.5V to VCC+0.5V

They don’t say why, what will happen, or anything similar, but those ratings are to be respected.

With a big series resistor (10kOhm) you might get away with negative voltages...
As you will not blow the diodes...

What is the frequency of the AC waveform?

but

a) the OP said nothing about resistance, and

b) he’d still not read anything on the negative side of 0.

c) could easily use a level shifter, and see the whole thing, but nothing was said about such a circuit.

Meh.

There is information about reading an AC voltage at:

That works out to almost 10volt peak to peak.

AC voltages must be presented to a pin mid-voltage (2.5volt).
Please show your connection diagram.
Leo..

The sketch worked on UNO R3 you say. The Nano Every is a different beast. The processor is ATmega4809 instead of ATmega328. Arduino IDE will use a different compiler, and (probably) different LiquidCrystal_I2C.h and Wire.h, intended invisible for the user.

Your signal goes negative. The ATmega328 will not be damaged by currents in it's protection diodes lower than 40mA. For the ATmega4809 the current induced into the pins should be limited to less than 1mA.

It is possible that while your UNO survived the input signal, your Nano Every did not.

Which would depend upon the characteristics of an unrevealed input circuit. The OP is long gone, I’m afraid, so we’re probably beating a dead horse.

2 Likes

Why not post an annotated schematic showing exactly how you have your project wired. Be sure to show all components, power sources etc. This will take us from the guessing to the real possibility of solving your problem.

2 Likes

I appreciate everyone's responses. I misunderstood the function of the analog inputs which was brought to light by @camsysca 's first reply. I will revise my project to try to address this problem

1 Like

Do you know what to do?