GeneTE:
I 'd never realized that I can not apply voltages to Arduino pins when the board is off. Fortunately I never did it!
Yes you can.
There is an unwritten rule that <= 1mA pin current should be safe.
GeneTE:
I need to read the voltages in the range of 0_150V. So, I am using a proper voltage divider. But there is always a possibility of the voltage surge and I am going to use zener for protection.
Zeners are useless for protection for the same reason as mentioned in post#3.
A clamping diode (schottky) to the 5volt rail COULD work, but schottky diodes leak (zeners also do).
That could make your measurements temp dependent.
There is another/better solution.
Use a 150k:1k divider.
That will bring 0-151volt down to 0-1volt.
That can be measured with (the more stable) 1.1volt Aref.
That will protect the pin, even if the Arduino is off and/or the 1k resistor is disconnected, or -150volt is connected.
That ratio will protect the pin (stay under 5.5volt) to at least 800volt if the Arduino is on.
It might be wise to use a 68k and 82k resistor in series to make a 150k resistor.
Small resistors are not made to have >100volt across.
Some code to try.
It uses 1.1volt Aref and averaging.
Leo..
/*
0 to ~160volt voltmeter for 3.3volt and 5volt Arduinos
uses the stable internal 1.1volt reference
1k resistor from A0 to ground, and 150k resistor from A0 to +supply
100n capacitor from A0 to ground for stable readings
(150k + 1k) / 1k = 151.0 | used in formula
*/
float Aref = 1.075; // ***calibrate here*** | change this to the actual Aref voltage of ---YOUR--- Arduino
unsigned int total; // can hold max 64 readings
float voltage; // converted to volt
void setup() {
analogReference(INTERNAL); // use the internal ~1.1volt reference | change (INTERNAL) to (INTERNAL1V1) for a Mega
Serial.begin(9600); // set serial monitor to this value
}
void loop() {
for (int x = 0; x < 64; x++) { // multiple analogue readings for averaging
total = total + analogRead(A0); // add each value to a total
}
voltage = (total / 64.0) * 151.0 * Aref / 1024 ; // convert readings to volt
// print to serial monitor
if (total == (1023 * 64)) { // if overflow
Serial.println("voltage too high");
}
else {
Serial.print("The supply is ");
Serial.print(voltage, 1); // one decimal place
Serial.println(" volt");
}
total = 0; // reset value
delay(1000); // one second between measurements
}