Advertisement
learnelectronics

Arduino Ohm Meter

Sep 28th, 2021
2,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //************************************************
  2. //*                learnelectronics              *
  3. //*                Arduino Ohm Meter             *
  4. //*     original code by Iasonas Christoulakis   *
  5. //*                                              *
  6. //*      Modified by learnelectronics 9/28/21    *
  7. //*        www.youtube.com/learnelectronics      *
  8. //*                                              *
  9. //*           email: arduino0169@gmail.com       *
  10. //************************************************
  11.  
  12.  
  13.  
  14. #include <Wire.h>                                         //library for I2C
  15. #include <Adafruit_SSD1306.h>                             //OLED driver
  16. #define OLED_RESET 4                                      //required by the OLED driver
  17. Adafruit_SSD1306 display(OLED_RESET);                     //initiate library
  18.  
  19. int analogPin= 0;                                         //pin to read voltage divider
  20. int raw= 0;                                               //reading from pin
  21. int Vin= 5;                                               //voltage out from arduino
  22. float Vout= 0;                                            //initial value
  23. float R1= 974;                                            //actual reading of known resistor
  24. float R2= 0;                                              //initial value for UNKNOWN resistor
  25. float buffer= 0;                                          //initail buffer value
  26.  
  27. void setup()   {                
  28.  
  29.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);              //startup the display  
  30.   display.display();                                      //show display buffer
  31.   delay(2);                                               //wait very briefly
  32.   display.clearDisplay();                                 //clear the display
  33.  
  34.  
  35. }
  36.  
  37.  
  38. void loop() {
  39.  
  40. raw= analogRead(analogPin);                                //read voltage at analog pin 0
  41. if(raw)                                                    //if there is a reading, then
  42. {
  43. buffer= raw * Vin;                                         //multiply the reading by the value of vin
  44. Vout= (buffer)/1024.0;                                     //set value of Vout to the whats in the buffer divided by 1024, the max value of the ADC
  45. buffer= (Vin/Vout) -1;                                     //now make the buffer voltage in divided by voltage out -1
  46. R2= R1 * buffer;                                           //and the value of the unknown is the known divided by the buffer
  47.  
  48. }
  49.   display.clearDisplay();                                 //show the answer
  50.   display.setCursor(0,0);
  51.   display.setTextSize(1);
  52.   display.setTextColor(WHITE);
  53.  
  54. display.print("Vout: ");
  55. display.println(Vout);
  56. display.print("R2: ");
  57. display.println(R2);
  58. display.display();
  59.  
  60. delay(1000);
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement