bobmarley12345

arduinovoltmetercode

Jun 6th, 2019
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float vout = 0.0;
  2. float vin = 0.0;
  3. float R1 = 20000.0;
  4. float R2 = 3300.0;
  5. int value = 0;
  6. void setup()
  7. {
  8.    pinMode(A2, INPUT);
  9.    Serial.begin(115200); //I use 115200 because it's fast ;) but you can use 9600 if you want. You'd have to change the baud rate in the c# program to 9600 if you do as serial ports' baud rates need to be the same
  10. }
  11. void loop()
  12. {
  13.    value = analogRead(A2);             //read the analogue voltage
  14.    vout = (value * 5.0) / 1024.0;      //math
  15.    vin = (vout / (R2 / (R1+R2)));      //more math, this also uses the resistor values too.
  16.    String msg = String(vin + 1) + "v"; //creates a string with the end voltage (e.g, 11 + 1 as it's a bit inaccurate) and adds "v" for volts
  17.    Serial.println(msg);                //sends it
  18.    
  19.    delay(250);
  20.    //this delay is used to avoid flooding the serial port. if you want faster updates, it'd change this to a minimum of 25. Put at 100 for updates every 1/10th of a second, or every 0.1 seconds
  21. }
RAW Paste Data