Auto range voltmeter FYI

Here is what I did recently in a small project.
A method to have an auto range voltmeter.

     //   M O D E  # 4   V O L T M E T E R
  case 4:  
    {
            int temp = 0;
            byte scaleFactor = 3;

            digitalWrite(range,LOW);     //Turn OFF the auto range Opto FET  0-15 volts
            temp = analogRead(voltageIn);
            delay (100);
            temp = analogRead(voltageIn);
  
            //are we below the threshold (0-5 volts)?
            if (temp <= 350)
            { 
              //we are  in the range of 0-5 volts
              //turn ON the auto range Opto FET  
              digitalWrite(range,HIGH); 
              scaleFactor = 1;
            }

      // subtract the last reading:
      total = total - readings[index];         

      // read twice the voltage on Pin A0:  
      analogRead(voltageIn); 
      delay(100);
      readings[index] = analogRead(voltageIn); 

      // add the reading to the total:
      total = total + readings[index];       

      // advance to the next position in the array:  
      index = index + 1;                    

      // if we're at the end of the array...
      if (index >= numReadings)
      {      
        // ...wrap around to the beginning: 
        index = 0;                           
      }
      // calculate the average:
      average = total / numReadings;         

      //use 5060 to compensate for the 1K resistor     
       i = map(average, 0, 1023, 0,5060 ); 
       i = i * scaleFactor;
      
      break; 

    }

2014-09-05_22-06-56.jpg

That's cool.
Can you do auto positive and negative voltages too ?

Neat trick - are there higher voltage opto-isolated FETs I wonder? That H11F2 is
rated +/-30V breakdown.

For bipolar use you'd reference to a 2.5V and add protection diodes to the ground rail too.

Adding a 10nF cap to the output would remove high frequency noise and give more
reliable analogRead() output.

Can you do auto positive and negative voltages too ?

See: Kelivin contact polarity diagram on the data sheet. Maybe one could modify it.
https://www.fairchildsemi.com/datasheets/H1/H11F3M.pdf

2.5/1.1 volt reference might be next.
Actually there is a capacitor off sheet.
30 volt break down is a problem, but all my applications are less that 24v 15v.

LarryD:

Can you do auto positive and negative voltages too ?

See: Kelivin contact polarity diagram on the data sheet. Maybe one could modify it.

Thanks ! I have never seen something like that. The "kelvin contact polarity" is like a cross-over switch with 4 of those optocouplers.

You can use DPDT relays but then there is a click click click thing to deal with.

You don't need to polarity switch, just read a bipolar voltage by referencing to
mid-rail... Loses one bit though.

You don't need to polarity switch, just read a bipolar voltage by referencing to
mid-rail...

Yes, that too.