1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// DC Voltage Module without LCD Display Shield By Solarduino // Note Summary // Note : Safety is very important when dealing with electricity. We take no responsibilities while you do it at your own risk. // Note : This DC Votlage Sensor Code is for Voltage Divider Method or related DC voltage module use. // Note : The value shown in Serial Monitor is refreshed every second. // Note : The voltage measured is direct measurement based on 1 sample only and is not an average value. // Note : No calibration is needed. // Note : The unit provides reasonable accuracy and may not be comparable with other expensive branded and commercial product. // Note : All credit shall be given to Solarduino. /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/////////////*/ /* 1 - DC Voltage Measurement */ int analogInputPin = A2; // This is the analog input pin number in arduino for voltage sensing. float vArduino = 0.0; /* This is the value voltage sensed by arduino in 0-5 volt */ float vActual = 0.0; /* This is the actual voltage that being measured or monitored*/ float R1 = 30000.0; // This is the resistance (in ohm) of R1. You may change this value based on the resistor you use. float R2 = 7500.0; // This is the resistance (in ohm) of R2. You may change this value based on the resistor you use. int rawValueRead= 0; /* This is the raw value / analog value detected by Arduino ranges 0 - 1023*/ void setup() { /* 1 - DC Voltage Measurement */ pinMode(analogInputPin, INPUT); /* Declare analog pin as an input*/ Serial.begin(9600); /* Initialise the Serial Monitor function. The Serial Monitor available at 9600 baud rate*/ Serial.println("DC VOLTMETER"); /* Describe the function code displayed in Serial Monitor*/ } void loop() { /* 1 - DC Voltage Measurement */ rawValueRead = analogRead(analogInputPin); /* Read and collect sensor from analog input pin in raw data (0-1023) values */ vArduino = (rawValueRead * 5.0) / 1024.0; /* Convert the raw data value into 0 - 5V measurable voltage at analog input pin*/ vActual = vArduino / (R2/(R1+R2)); /* Calculate the expected monitoring voltage in full voltage*/ Serial.print("Vdc = "); Serial.println(vActual,2); /* Print out the result in Serial Monitor, in 2 decimal places*/ delay(1000); /* Delay or pause for 1.000 seconds*/ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
// DC Voltage Module with LCD Display Shield By Solarduino // Note Summary // Note : Safety is very important when dealing with electricity. We take no responsibilities while you do it at your own risk. // Note : This DC Votlage Sensor Code is for Voltage Divider Method or related DC voltage module use. // Note : The value shown in Serial Monitor / LCD Display is refreshed every second. // Note : The voltage measured is direct measurement based on 1 sample only and is not an average value. // Note : No calibration is needed. // Note : The unit provides reasonable accuracy and may not be comparable with other expensive branded and commercial product. // Note : All credit shall be given to Solarduino. /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/////////////*/ /* 1 - DC Voltage Measurement */ int analogInputPin = A2; // This is the analog input pin number in arduino for voltage sensing. (A0 is reserved for LCD Shield buttons function) float vArduino = 0.0; /* This is the value voltage sensed by arduino in 0-5 volt */ float vActual = 0.0; /* This is the actual voltage that being measured or monitored*/ float R1 = 30000.0; // This is the resistance (in ohm) of R1. You may change this value based on the resistor you use. float R2 = 7500.0; // This is the resistance (in ohm) of R2. You may change this value based on the resistor you use. int rawValueRead= 0; /* This is the raw value / analog value detected by Arduino ranges 0 - 1023*/ /* 2 - LCD Display */ #include<LiquidCrystal.h> /*Load the liquid Crystal Library (by default already built-it with arduino solftware)*/ LiquidCrystal LCD(8,9,4,5,6,7); /*Creating the LiquidCrystal object named LCD */ unsigned long startMillisLCD; /* start counting time for LCD Display */ unsigned long currentMillisLCD; /* current counting time for LCD Display */ const unsigned long periodLCD = 1000; // refresh every X seconds (in seconds) in LED Display. Default 1000 = 1 second void setup() { /* 1 - DC Voltage Measurement */ pinMode(analogInputPin, INPUT); /* Declare analog pin as an input*/ Serial.begin(9600); /* Initialise the Serial Monitor function. The Serial Monitor available at 9600 baud rate*/ Serial.println("DC VOLTMETER"); /* Describe the function code displayed in Serial Monitor*/ /* 2 - LCD Display */ LCD.begin(16,2); /*Tell Arduino that our LCD has 16 columns and 2 rows*/ LCD.setCursor(0,0); /*Set LCD to upper left corner of display*/ startMillisLCD = millis(); } void loop() { /* 1 - DC Voltage Measurement */ rawValueRead = analogRead(analogInputPin); /* Read and collect sensor from analog input pin in raw data (0-1023) values */ vArduino = (rawValueRead * 5.0) / 1024.0; /* Convert the raw data value into 0 - 5V measurable voltage at analog input pin*/ vActual = vArduino / (R2/(R1+R2)); /* Calculate the expected monitoring voltage in full voltage*/ Serial.print("Vdc = "); Serial.println(vActual,2); /* Print out the result in Serial Monitor, in 2 decimal places*/ delay(1000); /* Delay or pause for 1.000 seconds*/ /* 2 - LCD Display */ currentMillisLCD = millis(); if (currentMillisLCD - startMillisLCD >= periodLCD) { LCD.setCursor(0,0); /* Set cursor to first colum 0 and second row 1 */ LCD.print(vActual,2); /* display voltage value in LCD in first row */ LCD.print(" Vdc "); startMillisLCD = currentMillisLCD ; /* Set the starting point again for next counting time */ } } |