Advertisement
learnelectronics

Arduino Ohm Meter v1.5

Oct 4th, 2021
2,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //************************************************
  2. //*          learnelectronics & Luke Barber      *
  3. //*              Arduino Ohm Meter  v1.5         *
  4. //*     original code by Iasonas Christoulakis   *
  5. //*                                              *
  6. //*      Modified by Luke Barber 10/2/21         *
  7. //*      Modified by learnelectronics 9/28/21    *
  8. //*        www.youtube.com/learnelectronics      *
  9. //*                                              *
  10. //*           email: [email protected]       *
  11. //************************************************
  12.  
  13.  
  14.  
  15. #include <Wire.h>             //library for I2C
  16. #include <Adafruit_SSD1306.h>//OLED driver
  17. #include <Adafruit_GFX.h>
  18. #define SCREEN_WIDTH 128   // OLED display width, in pixels
  19. #define SCREEN_HEIGHT 64  // OLED display height, in pixels
  20. #define OLED_RESET 4     //required by the OLED driver
  21. //Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET); //initiate library
  22. Adafruit_SSD1306 display(OLED_RESET);
  23.  
  24. int analogPin = 0;      //pin to read voltage divider
  25. int raw = 0;           //reading from pin
  26. int Vin = 5;          //voltage out from arduino
  27. float Vout = 0;      //initial value
  28. float R1 = 974;    //"Known" resistor connects between Gnd & Analog Reading point(between resistors)
  29. float R2 = 0;      //"Unknown" resistor connects between 5v & Analog Reading point(between resistors)
  30. float buffer = 0; //initial buffer value
  31. int tm=2000;     //the time it displays final result (which resistor you have entered)
  32.  
  33. void setup()   {
  34.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //startup the display
  35.   display.display();
  36.   delay(500);
  37.   display.setTextColor(WHITE);
  38.  
  39.   display.setTextSize(3);  
  40.   display.setCursor(20,16);
  41.   display.clearDisplay();
  42.   display.print("Basic");
  43.   display.display();
  44.   delay(1000);
  45.  
  46.   display.clearDisplay();
  47.   display.setTextSize(5);
  48.   display.setCursor(20,16);
  49.   display.println("Ohm");
  50.   display.display();
  51.   delay(1000);
  52.  
  53.   display.clearDisplay();
  54.   display.setTextSize(3);
  55.   display.setCursor(20,16);
  56.   display.print("Meter");
  57.   display.display();
  58.   delay(1000);
  59.   display.clearDisplay();
  60. }
  61. void loop() {
  62.   display.clearDisplay();
  63.   display.setCursor(0,0);
  64.   display.setTextSize(2);
  65.   display.setTextColor(WHITE);
  66.   delay(100);
  67.   raw = analogRead(analogPin);    //read voltage at analog pin 0
  68.   if (raw) {                     //if there is a reading, then lets begin
  69.     buffer = raw * Vin;         //multiply the reading by the value of vin
  70.     Vout = (buffer) / 1024.0;  //set value of Vout to the whats in the buffer divided by 1024, the max value of the ADC
  71.     buffer = (Vin / Vout) - 1;//now make the buffer voltage in divided by voltage out -1
  72.     R2 = R1 * buffer;        //the value of the unknown is the known multiplied by the buffer
  73.     display.clearDisplay();
  74.     display.println("Volts");
  75.     display.println("=");
  76.     display.print(Vout);
  77.     display.print("v");
  78.     display.display();
  79.     delay(1500);
  80.     display.clearDisplay();
  81.     delay(100);
  82.     display.setCursor(0,0);
  83.     display.println("Resistance");
  84.     display.println("=");
  85.     display.print(R2);
  86.     display.display();
  87.     delay(1500); //wait 3 seconds so it can be read
  88.   }
  89.   display.clearDisplay();
  90.   display.setCursor(25,20);
  91.   display.setTextSize(2);
  92.   display.setTextColor(WHITE);
  93.   if (R2 > 4.0 && R2 < 16.0) {
  94.     display.print("10Ohms");   //Ω
  95.     display.display();
  96.     delay(tm);
  97.   }
  98.   if (R2 > 75.0 && R2 < 125.0) {
  99.     display.print("100Ohms");
  100.     display.display();
  101.     delay(5000);
  102.   }
  103.   if (R2 > 200.0 && R2 < 240.0) {
  104.     display.print("220Ohms");  
  105.     display.display();
  106.     delay(tm);
  107.   }
  108.   if (R2 > 300.0 && R2 < 350.0) {
  109.     display.print("330Ohms");
  110.     display.display();
  111.     delay(tm);
  112.   }
  113.   if (R2 > 900.0 && R2 < 1100.0) {
  114.     display.print("1kOhms");
  115.     display.display();
  116.     delay(tm);
  117.   }
  118.   if (R2 > 1800.0 && R2 < 2200.0) {
  119.     display.print("2kOhms");
  120.     display.display();
  121.     delay(tm);
  122.   }
  123.   if (R2 > 4800.0 && R2 < 5200.0) {
  124.     display.print("5kOhms");
  125.     display.display();
  126.     delay(tm);
  127.   }
  128.   if (R2 > 9000.0 && R2 < 11000.0) {
  129.     display.print("10kOhms");
  130.     display.display();
  131.     delay(tm);
  132.   }
  133.   if (R2 > 12500.0) {
  134.     display.setCursor(0,0);
  135.     display.println("   Out");
  136.     display.println("   Of");
  137.     display.print("   Range");
  138.     display.display();
  139.     delay(2000);
  140.   }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement