Вольтметр на Arduino
Код:
Скетч: /*-------------------------------------------------------------- Program: volt_measure Description: Reads value on analog input A2 and calculates the voltage assuming that a voltage divider network on the pin divides by 11. Hardware: Arduino Uno with voltage divider on A2. Software: Developed using Arduino 1.0.5 software Should be compatible with Arduino 1.0 + Date: 22 May 2013 Author: W.A. Smith, http://startingelectronics.com --------------------------------------------------------------*/ #include <LiquidCrystal.h> // R1 R2 // Vin ----/\/\/\----*----/\/\/\---- GND // | // | Vout // | // ANALOG PIN // * Vin : input voltage (the voltage we try to meter) // * Vmax : the maximum value of the input voltage // * Vout : the output voltage (the Vin remapped to 0 .. 1.1V) // Choose R1 and R2 CAREFULLY according to the following rule: // R1 = R2 x 1.1 / (Vmax - 1.1) // R2 = R1 x (Vmax - 1.1) / 1.1 // A typical configuration is R1 = 1k and R2 = 4k. It assumes Vmax = 5.5V. // number of analog samples to take per reading #define NUM_SAMPLES 10 int sum = 0; // sum of samples taken unsigned char sample_count = 0; // current sample number float voltage = 0.0; // calculated voltage LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { Serial.begin(9600); } void loop() { // take a number of analog samples and add them up while (sample_count < NUM_SAMPLES) { sum += analogRead(A2); sample_count++; delay(100); } // calculate the voltage // use 5.0 for a 5.0V ADC reference voltage // 5.015V is the calibrated reference voltage voltage = ((float)sum / (float)NUM_SAMPLES * 5.015) / 1024.0; // send voltage for display on Serial Monitor // voltage multiplied by 11 when using voltage divider that // divides by 11. 11.132 is the calibrated voltage divide // value lcd.begin(16, 2); lcd.print(" Volt= "); lcd.print(voltage * 9.1); lcd.print("V"); Serial.print(voltage * 9.1); Serial.println (" V"); sample_count = 0; sum = 0; }