Arduino voltage measurement

Wednesday, June 21, 2017

Analog input of Arduino can be used to measure voltage. Understand how it works will helps to design voltmeter can measure wide range of voltage, or even making an oscilloscope
What hardware need:
(1) Arduino Pro mini
(2) Volt reference circuit
(3) USB-COM converter
(4) Bread board with some electronics component

Step 1. Connection circuit
This circuit will have probe for measuring voltage. Probe connect with resistor 10MOhm to reduce current for A0 (Analog input 0) which also connect to ground, so when probe connect to no where, voltage at A0 will be 0V, otherwise, it will float with some values.
VR 5K is used for testing voltage, it can make voltage range from 5V to 0V depend on position of adjustment
Unfortunately, Arduino Pro mini doesn't have AREF pin out -> we have to make pin out by soldering



Step 2. Volt reference
Analog input needs voltage reference to measure (to understand how it works, find digital ADC subject) -> voltage reference must be very stable to make measuring result exactly
Inside Arduino Pro mini will have 2 kind of internal reference voltage 
   (a) DEFAULT: the default analog reference of VCC
   (b) INTERNAL: an built-in reference, equal to 1.1 volts on ATmega328 (Arduino Pro mini)
The problem is supply voltage for Arduino Pro mini not perfect, it will fluctuate in some range. So, this voltage will effect to DEFAULT or INTERNAL reference voltage -> effect to measuring result.

Good way is using EXTERNAL voltage reference which is supply by external voltage circuit. In market, there are some PCB which produces standard voltage. This case, we use 78L05 to make EXTERNAL voltage reference:
This "Voltage reference" will connect to AREF pin in circuit of step 1
Our "voltage reference" in real model, looks not so good, but works well. To know how much voltage, using VOM to measure -> this value will be used in Arduino code. Our case, it is 4.33V

Step 3. Code for arduino
unsigned int ADCValue;
double Voltage, error;


void setup() {
  analogReference(EXTERNAL);
  Serial.begin(9600);
}

void loop() {
  ADCValue = analogRead(0);
  Voltage = (ADCValue / 1024.0) * 4.33;
  error = Voltage*(-0.9198)-0.2349;
  error = 0; //comment this line after calibration step

  Serial.print(Voltage-error);
  
  for (int i = 0; i < Voltage*20; i++)
  {
    Serial.print("-");
  }
  Serial.println();
  delay(50);
  
}

Let's see how code work:
(a) analogReference(EXTERNAL);
In official website of arduino stated that: if using external voltage for reference, we have to set analogReference(EXTERNAL) before calling analogRead(0);
When analog reference is set to EXTERNAL, an internal switch for ADC Atmega328 will not close, then the command analogRead(0) will base on this External voltage
In another case, if analog reference is NOT set to EXTERNAL, when we call command analogRead(0), the internal switch for ADC Atmega328 will close (by default) -> this case will short External voltage with AVCC (power supply for Atmega328)

As in External voltage reference circuit in step 2, it is very safety when using diode and resistor (diode prevent voltage flow to 78L05, resistor to prevent short circuit)

(b) error calculation
Firstly, as code in step 3, arduino will show result reading, but it does not correct as real life. So we need VOM to measure to build error curve (called: calibration procedure)
Using VR (step 1- test voltage) to change voltage, make it step by step increasing voltage, as well as record "arduino reading at analog channel 0". Make the table value in Office Excel -> calculate error -> build curve with x axis, y axis -> using function in Office Excel to show curve (y=ax+b)
Next, use factor (a, b in curve y=ax+b) for code Arduino (step 3). Remember comment out command line:
//error = 0; //comment this line after calibration

Check it again, we will get measuring result show on Arduino very exactly


No comments:

Post a Comment

 

Search This Blog

Most Reading

Google+ Followers