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);
}
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.
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.
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.
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