lcd display code conversion into oled display code.

i want to make capacitance meter with i2c 128x32 oled display, but i found and that code was for LCD display.

Can anybody convert this code? please help me.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,20,4); //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.

#define analogPin 0
#define chargePin 13
#define dischargePin 8
#define resistorValue 10000.0F //Remember, we've used a 10K resistor to charge the capacitor

unsigned long startTime;
unsigned long elapsedTime;
float microFarads;
float nanoFarads;

void setup(){
pinMode(chargePin, OUTPUT);
digitalWrite(chargePin, LOW);
lcd.init();
lcd.backlight();
}

void loop(){
digitalWrite(chargePin, HIGH);
startTime = micros();
while(analogRead(analogPin) < 648){
}

elapsedTime= micros() - startTime;
microFarads = ((float)elapsedTime / resistorValue) ;

if (microFarads > 1){

lcd.clear();
lcd.setCursor(0,0);
lcd.print("SCALE: 0.1uF-4F");
lcd.setCursor(0,1);
lcd.print(microFarads);
lcd.setCursor(14,1);
lcd.print("uF");
delay(500);
}

else{
nanoFarads = microFarads * 1000.0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SCALE: 0.1uF-4F");
lcd.setCursor(0,1);
lcd.print(nanoFarads);
lcd.setCursor(14,1);
lcd.print("nF");
delay(500);
}

digitalWrite(chargePin, LOW);
pinMode(dischargePin, OUTPUT);
digitalWrite(dischargePin, LOW); //discharging the capacitor
while(analogRead(analogPin) > 0){
}//This while waits till the capaccitor is discharged

pinMode(dischargePin, INPUT); //this sets the pin to high impedance

lcd.setCursor(0,0);
lcd.print("DISCHARGING.....");
lcd.setCursor(0,1);

}

capacitance_meter..ino (1.7 KB)

Wiring instructions for your oled display are given here.

Finally, connect the pins to your Arduino
GND goes to ground
Vin goes to 5V
SDA to I2C Data (on the Uno, this is A4 on the Mega it is 20 and on the Leonardo digital 2)
SCL to I2C Clock(on the Uno, this is A5 on the Mega it is 21 and on the Leonardo digital 3)
RST to digital 4 (you can change this pin in the code, later)

Then you need a library to use if: try this one from Adafruit. You'll also have to install the Adafruit GFX graphics core library.

A code example is provided here

Reading it, the instructions are very similar to those of the LCD display, except that you need to tell it to effectively display what you want. This is what the following instruction does:

display.display();

Here is a possible replacement code (not compiled, not tested):

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define analogPin      0
#define chargePin      13
#define dischargePin   8
#define resistorValue  10000.0F  //Remember, we've used a 10K resistor to charge the capacitor

unsigned long startTime;
unsigned long elapsedTime;
float microFarads;
float nanoFarads;

void setup() {
  pinMode(chargePin, OUTPUT);
  pinMode(dischargePin, OUTPUT);
  digitalWrite(chargePin, LOW);
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);
}

void loop() {
  digitalWrite(chargePin, HIGH);
  startTime = micros();
  while (analogRead(analogPin) < 648);

  elapsedTime = micros() - startTime;
  microFarads = ((float)elapsedTime / resistorValue) ;

  if (microFarads > 1) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("SCALE:  0.1uF-4F");
    display.setCursor(90, 1);
    display.print(microFarads);
    display.setCursor(110, 1);
    display.print("uF");
    display.display();
    delay(500);
  }

  else {
    nanoFarads = microFarads * 1000.0;
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("SCALE:  0.1uF-4F");
    display.setCursor(90, 1);
    display.print(nanoFarads);
    display.setCursor(110, 1);
    display.print("nF");
    display.display();
    delay(500);
  }

  digitalWrite(chargePin, LOW);
  digitalWrite(dischargePin, LOW);     //discharging the capacitor
  while (analogRead(analogPin) > 0) {
  }//This while waits till the capacitor is discharged

  pinMode(dischargePin, INPUT);      //this sets the pin to high impedance
  display.setCursor(0, 20);
  display.print("DISCHARGING.....");
  display.display();
}

If it doesn't work, it may be due to the I2C address (0x3F in the constructor line). You should then run a I2C scanner to detect the correct value.

@Moudud,

You posted the same question in another thread here

You had several replies there.

It is probably a good idea to start your own thread. But PLEASE say when you are starting a duplicate thread. e.g. say you are starting a new thread from the old one. post a message in the old thread with a link to this new thread.

Then everyone knows that they should reply to your new thread.

David.

Your output seems to be text, and therefore you can get by without the weight of the graphics library by using

"SSD1306Ascii.h" "SSD1306AsciiAvrI2c.h"

a test example is at Getting something useful from the SSD1306 OLED - Displays - Arduino Forum