搜索代码

How to Measure Any DC Voltage with an Arduino ARDVC-01

How to Measure Any DC Voltage with an Arduino ARDVC-01

Using this code and proper hardware, you will be able to measure any DC voltage using Arduino. You simply need two resistors to use them as a voltage divider, which has been explained in the video. To protect the Arduino against excess voltage at the analog pin, you need a 5.1V Zener diode. But if you are sure that the voltage never goes above your limit, you do not need a Zener diode.

317-Arduino code to measure any DC voltage
语言: C++
/*
 * Using this Arduino Code and proper resistor values
 * any DC voltage can be measured. Watch the video for details.
 
 * Written by Ahmad Shamshiri for Robojax Robojax.com
 * on April 24, 2020 at 16:55 in Ajax, Ontario, Canada
 * 
 Watch the video instructions for this sketch:https://youtu.be/t8xwrVj2aFs

 

Use the following resistors:
R1 5100 ohm 1% any power 1/4W or 1/8W
12V   R2 7200
for the rest watch the video 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can be my Patron and have early access to my videos here  http://robojax.com/L/?id=63
or make a donation using PayPal http://robojax.com/L/?id=64

* 
 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 

*/
const float arduinoVCC = 5.01;//Your Arduino voltage
unsigned long ValueR1 = 5070;
unsigned long ValueR2 = 7500;
double Voltage_Source = 12;
const int alanogPin = A0;//the pin connecting the voltage. 
const int inputResolution =1023;//works with most Arduino boards
const float average_of = 500;//Average of 500 readings

float voltage;
void setup() {
  Serial.begin(9600);
  Serial.println("Robojax: Reading Any voltage with Arduino");
  delay(500);

}

void loop() {
  //Robojax.com ARDVC-01 Measure any voltage with Arduino
  readVoltage();

  Serial.print("Vin: ");
  //Serial.print(voltage);
  //Serial.print("V Avg: ");
  Serial.print(getVoltageAverage());
  Serial.println("V");
  //delay(100);
  //Robojax.com ARDVC-01 Measure any voltage with Arduino
}//loop end


/*
 * @brief calculates the input voltage and updates the variable "voltage"
 * @param none
 * @return does not return anything
 * Written by Ahmad Shamshiri
 * www.Robojax.com code April 24 2020 at 17:49 in Ajax, Ontario, Canada
 */
void readVoltage(){
    //Robojax.com ARDVC-01 Measure any voltage with Arduino
    //This code is explained at my Arduino Course at udemy.com http://robojax.com/L/?id=62
    int A0Value = analogRead(alanogPin);
    float voltage_sensed = A0Value * (arduinoVCC / (float)inputResolution); 
//  Serial.print("voltage_sensed:");
//  Serial.print(voltage_sensed);       
  voltage = voltage_sensed * ( 1 + ( (float) ValueR2 /  (float) ValueR1) );
  //Robojax.com ARDVC-01 Measure any voltage with Arduino  
}//readVoltage()

/*
 * @brief calculates the average of input voltage and updates the variable "voltage"
 * @param none
 * @return returns average of "average_of" iteration of voltage
 * Written by Ahmad Shamshiri
 * www.Robojax.com code April 25 2020 at 13:43 in Ajax, Ontario, Canada
 */
float getVoltageAverage(){
  //Robojax.com ARDVC-01 Measure any voltage with Arduino
    //This code is explained at my Arduino Course at udemy.com http://robojax.com/L/?id=62  
    float voltage_temp_average=0;
    for(int i=0; i < average_of; i++)
    {
       readVoltage();
       voltage_temp_average +=voltage;
    }
      
  return voltage_temp_average / average_of;
    //Robojax.com ARDVC-01 Measure any voltage with Arduino
}//getVoltageAverage

资源与参考

尚无可用资源。

文件📁

没有可用的文件。