Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //************************************************
- //* learnelectronics & Luke Barber *
- //* Arduino Ohm Meter v1.5 *
- //* original code by Iasonas Christoulakis *
- //* *
- //* Modified by Luke Barber 10/2/21 *
- //* Modified by learnelectronics 9/28/21 *
- //* www.youtube.com/learnelectronics *
- //* *
- //* email: [email protected] *
- //************************************************
- #include <Wire.h> //library for I2C
- #include <Adafruit_SSD1306.h>//OLED driver
- #include <Adafruit_GFX.h>
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET 4 //required by the OLED driver
- //Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET); //initiate library
- Adafruit_SSD1306 display(OLED_RESET);
- int analogPin = 0; //pin to read voltage divider
- int raw = 0; //reading from pin
- int Vin = 5; //voltage out from arduino
- float Vout = 0; //initial value
- float R1 = 974; //"Known" resistor connects between Gnd & Analog Reading point(between resistors)
- float R2 = 0; //"Unknown" resistor connects between 5v & Analog Reading point(between resistors)
- float buffer = 0; //initial buffer value
- int tm=2000; //the time it displays final result (which resistor you have entered)
- void setup() {
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //startup the display
- display.display();
- delay(500);
- display.setTextColor(WHITE);
- display.setTextSize(3);
- display.setCursor(20,16);
- display.clearDisplay();
- display.print("Basic");
- display.display();
- delay(1000);
- display.clearDisplay();
- display.setTextSize(5);
- display.setCursor(20,16);
- display.println("Ohm");
- display.display();
- delay(1000);
- display.clearDisplay();
- display.setTextSize(3);
- display.setCursor(20,16);
- display.print("Meter");
- display.display();
- delay(1000);
- display.clearDisplay();
- }
- void loop() {
- display.clearDisplay();
- display.setCursor(0,0);
- display.setTextSize(2);
- display.setTextColor(WHITE);
- delay(100);
- raw = analogRead(analogPin); //read voltage at analog pin 0
- if (raw) { //if there is a reading, then lets begin
- buffer = raw * Vin; //multiply the reading by the value of vin
- Vout = (buffer) / 1024.0; //set value of Vout to the whats in the buffer divided by 1024, the max value of the ADC
- buffer = (Vin / Vout) - 1;//now make the buffer voltage in divided by voltage out -1
- R2 = R1 * buffer; //the value of the unknown is the known multiplied by the buffer
- display.clearDisplay();
- display.println("Volts");
- display.println("=");
- display.print(Vout);
- display.print("v");
- display.display();
- delay(1500);
- display.clearDisplay();
- delay(100);
- display.setCursor(0,0);
- display.println("Resistance");
- display.println("=");
- display.print(R2);
- display.display();
- delay(1500); //wait 3 seconds so it can be read
- }
- display.clearDisplay();
- display.setCursor(25,20);
- display.setTextSize(2);
- display.setTextColor(WHITE);
- if (R2 > 4.0 && R2 < 16.0) {
- display.print("10Ohms"); //Ω
- display.display();
- delay(tm);
- }
- if (R2 > 75.0 && R2 < 125.0) {
- display.print("100Ohms");
- display.display();
- delay(5000);
- }
- if (R2 > 200.0 && R2 < 240.0) {
- display.print("220Ohms");
- display.display();
- delay(tm);
- }
- if (R2 > 300.0 && R2 < 350.0) {
- display.print("330Ohms");
- display.display();
- delay(tm);
- }
- if (R2 > 900.0 && R2 < 1100.0) {
- display.print("1kOhms");
- display.display();
- delay(tm);
- }
- if (R2 > 1800.0 && R2 < 2200.0) {
- display.print("2kOhms");
- display.display();
- delay(tm);
- }
- if (R2 > 4800.0 && R2 < 5200.0) {
- display.print("5kOhms");
- display.display();
- delay(tm);
- }
- if (R2 > 9000.0 && R2 < 11000.0) {
- display.print("10kOhms");
- display.display();
- delay(tm);
- }
- if (R2 > 12500.0) {
- display.setCursor(0,0);
- display.println(" Out");
- display.println(" Of");
- display.print(" Range");
- display.display();
- delay(2000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement