Advertisement
Braulio777

Arduino Voltmeter Code

Jun 17th, 2015
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. //Arduino Voltmeter
  2. //4-Channels Voltmeter
  3. //Measure Voltages from 0V to 50V per channel
  4. //4 resistors of 100K connected to GND and the remaining terminals
  5. //to their respective resistors of 1M forming the 4-common points
  6. //connected to A2, A3, A4, and A5 respectively while the remaining
  7. //terminals of these last resistors are the outputs of Vr, Vw, Vy, and Vg  
  8. #include <LiquidCrystal.h>
  9. #define NUM_SAMPLES 10
  10. #define DIV_1    11.1346
  11. #define DIV_2    11.1969
  12. #define DIV_3    11.0718
  13. #define DIV_4    11.0718
  14. #define V_REF    4.991
  15. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  16. int sum[4] = {0};                
  17. unsigned char sample_count = 0;
  18. float voltage[4] = {0.0};        
  19. char l_cnt = 0;                  
  20.  
  21. void setup()
  22. {
  23. lcd.begin(16, 2);
  24. }
  25.  
  26. void loop()
  27. {
  28.     while (sample_count < NUM_SAMPLES) {
  29.         for (l_cnt = 0; l_cnt < 4; l_cnt++) {
  30.             sum[l_cnt] += analogRead(A2 + l_cnt);
  31.         }
  32.         sample_count++;
  33.         delay(10);
  34.     }
  35.     for (l_cnt = 0; l_cnt < 4; l_cnt++) {
  36.         voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
  37.     }
  38.     //the common point of a set of resistors connect to A2
  39.     lcd.setCursor(0, 0);
  40.     lcd.print("Vr=");
  41.     lcd.print(voltage[0] * DIV_1, 1);
  42.     lcd.print("V ");
  43.     //the common point of the next set of resistors connect to A3
  44.     lcd.setCursor(8, 0);
  45.     lcd.print("Vw=");
  46.     lcd.print(voltage[1] * DIV_2, 1);
  47.     lcd.print("V ");
  48.     //the common point of the next set of resistors connect to A4  
  49.     lcd.setCursor(0, 1);
  50.     lcd.print("Vy=");
  51.     lcd.print(voltage[2] * DIV_3, 1);
  52.     lcd.print("V ");
  53.     //the common point of the next set of resistors connect to A5
  54.     lcd.setCursor(8, 1);
  55.     lcd.print("Vg= ");
  56.     lcd.print(voltage[3] * DIV_4, 1);
  57.     lcd.print("V ");
  58.      
  59.     sample_count = 0;
  60.     for (l_cnt = 0; l_cnt < 4; l_cnt++) {
  61.         sum[l_cnt] = 0;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement