/ * ------------------------------------------------ --------------
Four-channel voltmeter 0-50V, with averaging,
one digit after the decimal point, with display on the LCD-indicator
16 characters, 2 lines,
Used expansion card from a set of Master Kit
NR05 "Digital Laboratory"; 4 dividers for 10 on resistors
1M, 100k connected with analog inputs A0, A4, A5, A7
Voltage Reference Calibration
Measure the voltage of 5V and change the values ​​of the constant V_REF
according to the measured value.
Measure Voltage with LCD Connected
and with a running sketch.
Determination of the calibration values ​​of the dividers (held
for each divider)
Connect the stable voltage Vin to the input of the divider and
measure it.
Measure the voltage Vout at the output of the divider.
The calibration DIV_ * value will be Vin / Vout.
The project is based on
startingelectronics.com-------------------------------------------------- ------------ * /
#include <LiquidCrystal.h>
// built-in LED (used to indicate the measurement process)
#define LED 13
// number of samples per measurement
#define NUM_SAMPLES 20
// divider calibration values
#define DIV_1 11.186
#define DIV_2 11.186
#define DIV_3 11.186
#define DIV_4 11.186
// calibration value of the reference voltage
#define V_REF 4.575
// number of buttons on the board
#define NUM_KEYS 5
// calibration values ​​for each button (derived experimentally)
int adcKeyVal [NUM_KEYS] = {30, 150, 360, 535, 760};
LiquidCrystal lcd (A1, A2, A3, 2, 4, 7);
unsigned long sum [4] = {0}; // sum of samples in each channel
unsigned char sample_count = 0; // current sample number
float voltage [4] = {0.0}; // calculated voltage
int cnt = 0; // service variable
int keyIsPressed = 0; // flag of pressing the “freezing” button of measurements
void setup ()
{
lcd.begin (16, 2);
pinMode (LED, OUTPUT);
digitalWrite (LED, LOW);
}
void loop ()
{
// if button 3 is pressed, we invert the flag of pressing the “freezing” button
if (get_key () == 3) {
keyIsPressed =! keyIsPressed;
delay (500);
}
// if the flag is set (1), the information on the display is not updated
if (keyIsPressed == 0) {
digitalWrite (LED, LOW);
// take samples in each channel and summarize them
while (sample_count <NUM_SAMPLES) {
// sample channel A0, A4, A5, A7
sum [0] + = analogRead (A0);
sum [1] + = analogRead (A4);
sum [2] + = analogRead (A5);
sum [3] + = analogRead (A7);
sample_count ++;
delay (10);
}
digitalWrite (LED, HIGH);
// calculate the voltage in each channel by averaging the samples
for (cnt = 0; cnt <4; cnt ++) {
voltage [cnt] = ((float) sum [cnt] / (float) NUM_SAMPLES * V_REF) / 1024.0;
}
// display the values ​​on the idicator
lcd.setCursor (0, 0);
lcd.print (“A„);
lcd.print (voltage [0] * DIV_1, 1);
lcd.print (“V„);
// voltage 2 - B (A4 pin)
lcd.setCursor (8, 0);
lcd.print (“B„);
lcd.print (voltage [1] * DIV_2, 1);
lcd.print (“V„);
// voltge 3 - C (pin A5)
lcd.setCursor (0, 1);
lcd.print (“C„);
lcd.print (voltage [2] * DIV_3, 1);
lcd.print (“V„);
// voltage 4 - D (pin A7)
lcd.setCursor (8, 1);
lcd.print (“D„);
lcd.print (voltage [3] * DIV_4, 1);
lcd.print (“V„);
// reset the counter and the amount
sample_count = 0;
for (cnt = 0; cnt <4; cnt ++) sum [cnt] = 0;
delay (20);
}
}
// function returns the number of the button pressed
int get_key ()
{
int input = analogRead (A6);
int k;
for (k = 0; k <NUM_KEYS; k ++)
if (input <adcKeyVal [k])
return k + 1;
return 0;
}