The original video is here:
https://www.youtube.com/watch?v=y-_Pkw_GQ-cIn this tutorial we look at a digital voltmeter project and how it used the Arduino analog input. This involves understanding ADC or analog to digital converters and how they work. We also troubleshoot why the output reading on the LCD display is noisy.
We also gave a quick peek at the Arduino DUE:
http://arduino.cc/en/Main/arduinoBoardDueThanks to Clarence of "Clarence's Wicked Mind" for the original code that I used and updated for this project:
http://www.clarenceho.net:8123/blog/articles/search?q=voltmeterAlso read Measuring Stuff - The Arduino DAQ Chronicles:
https://sites.google.com/site/measuringstuff/the-arduinoThe Arduino Due is a microcontroller board based on the Atmel SAM3X8E ARM Cortex-M3 CPU (datasheet). It is the first Arduino board based on a 32-bit ARM core microcontroller. It has 54 digital input/output pins (of which 12 can be used as PWM outputs), 12 analog inputs, 4 UARTs (hardware serial ports), a 84 MHz clock, an USB OTG capable connection, 2 DAC (digital to analog), 2 TWI, a power jack, an SPI header, a JTAG header, a reset button and an erase button.
Buy the Arduino DUE here:http://astore.amazon.com/m0711-20/detail/B00A6C3JN2
The sketch / project code for the digital voltmeter:
/*
Voltmeter
Voltmeter base on voltage divider concept.
by Clarence's Wicked Mind - news from Clarence, stuff that doesn't matter
Code based on:
http://www.clarenceho.net:8123/blog/articles/2009/05/17/arduino-test-voltmeter Coded by: arduinoprojects101.com
Updated by Martin Lorton (
http://mjlorton.com and
http://www.youtube.com/mjlorton )for use in
Arduino tutorial series:
https://www.youtube.com/playlist?list=PLF86F263013F106C0*/
#include <SoftwareSerial.h>
#define txPin 2
SoftwareSerial LCD = SoftwareSerial(0, txPin);
// since the LCD does not send data back to the Arduino, we should only define the txPin
const int LCDdelay=10; // conservative, 2 actually works
int analogInput = 1;
int LEDpin = 13;
int prev = LOW;
int refresh = 2000;
long TotalTime = 0;
int Samples = 0;
long TimeRun = 0;
float vout = 0.0;
float vin = 0.0;
float R1 = 4739.0; // !! resistance of R1 !!
float R2 = 988.0; // !! resistance of R2 !!
float vMax = 0.0;
float vMin = 10000.0;
float vAcc = 0.0;
float vAve = 0.0;
int value = 0;
int LCDpos = 0;
int vMaxFlag = 0;
int vMinFlag = 0;
int TMin = 0;
int TSec = 0;
void clearLCD(){
LCD.write(0xFE); //command flag
LCD.write(0x01); //clear command.
delay(LCDdelay);
}
void backlightOn() { //turns on the backlight
LCD.write(0x7C); //command flag for backlight stuff
LCD.write(157); //light level.
delay(LCDdelay);
}
void backlightOff(){ //turns off the backlight
LCD.write(0x7C); //command flag for backlight stuff
LCD.write(128); //light level for off.
delay(LCDdelay);
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
LCD.write(0xFE);
}
void setup()
{
// declaration of pin modes
pinMode(analogInput, INPUT);
pinMode(LEDpin, OUTPUT);
pinMode(txPin, OUTPUT);
// begin sending over serial port
Serial.begin(9600);
LCD.begin(9600);
delay(LCDdelay);
backlightOn();
}
void loop()
{
// read the value on analog input
value = analogRead(analogInput);
if (value >= 1023) {
Serial.println("MAX!!");
delay(refresh);
return;
}
else if (value <= 0) {
Serial.println("MIN!!");
delay(refresh);
return;
}
Samples = Samples + 1;
TotalTime = TotalTime + refresh;
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
vMinFlag = 0;
vMaxFlag = 0;
if (vin > vMax) {digitalWrite(LEDpin, HIGH);}
if (vin > vMax) {vMaxFlag=1;}
if (vin > vMax) {vMax=vin;}
if (vin < vMin) {digitalWrite(LEDpin, HIGH);}
if (vin < vMin) {vMinFlag=1;}
if (vin < vMin) {vMin=vin;}
vAcc = vin + vAcc;
vAve = vAcc / Samples;
TimeRun = TotalTime / 1000;
// Serial output to computer terminal
Serial.print(vin);
Serial.print(" volt * ");
Serial.print(vMax);
Serial.print(" volt Max * ");
Serial.print(vMin);
Serial.print(" volt Min * ");
Serial.print(vAve);
Serial.print(" volt Ave * ");
Serial.print(TimeRun);
Serial.println(" Time (Sec)");
// Martin Adding LCD Stuff
clearLCD();
serCommand();
if (vin < 10) {LCD.write(129);}
else {LCD.write(128);}
LCD.print(vin);
serCommand();
LCD.write(134);
LCD.print("Volts");
serCommand();
if (vMax < 10) {LCD.write(193);}
else {LCD.write(192);}
LCD.print(vMax);
serCommand();
LCD.write(198);
if (vMaxFlag==1) {LCD.print("Max Volts *");}
else {LCD.print("Max Volts");}
LCD.write(0xFE);
if (vMin < 10) {LCD.write(149);}
else {LCD.write(148);}
LCD.print(vMin);
serCommand();
LCD.write(154);
if (vMinFlag==1) {LCD.print("Min Volts *");}
else {LCD.print("Min Volts");}
serCommand();
if (vAve < 10) {LCD.write(213);}
else {LCD.write(212);}
LCD.print(vAve);
serCommand();
LCD.write(218);
LCD.print("Ave Volts ");
LCD.print(value);
serCommand();
LCD.write(141);
LCD.print(TimeRun);
LCD.print(" Sec");
// sleep...
delay(refresh);
digitalWrite(LEDpin, LOW);
}