1

I have been playing with this code for some time now, I am able to show the results in an 16X2 LCD display, but I am not able to do it in an OLED.

here is the code

    #include <SPI.h>
    #include <Wire.h>
    #include "U8glib.h"  //Library for display 

    U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C interface for OLED  

    int index = 0; //size of array
    const int number_of_readings = 200; //total number of readings arduino takes before it averages
    const int current_zero = 510; //initial value of current from arduino with 0 amps
    int current_reading[number_of_readings]; //create array for current_reading
    int voltage_reading[number_of_readings]; //create array for voltage_reading

    float R1=30000; //resistance of R1 in voltage sensor
    float R2=7500; //resistance of R2 in voltage sensor
    float current_average = current_zero; //initial value of current average
    float current_total = 0; //initial value of current_total
    float voltage_average = 0; //initial value of voltage_average
    float voltage_total = 0; //initial value of voltage_total

    int x = 0;
    int row = 0;
    float voltage = 0.1;  



      void setup() {
       Serial.begin(9600);
       pinMode(A4,INPUT_PULLUP);
       pinMode(A5, INPUT_PULLUP);
      }


      void draw(void) {
       u8g.setFont(u8g_font_fub14r); 
       {

       u8g.drawStr(0, 20, "V: ");  
   u8g.drawStr(0, 40, "A: ");
   u8g.drawStr(0, 60, "Watt: ");
   u8g.setPrintPos(58,20);       
   u8g.print( Bat_Volt,2);  
   u8g.println("V"); 
   u8g.setPrintPos(58,40);        
   u8g.print( current,0);  
   u8g.println("mA"); 
   u8g.setPrintPos(58, 60);       
   u8g.print( power ,1);    

    }
    }


     void loop() {
      voltage_reading[index]=analogRead(A0); //read voltage reading from pin A0 (value 0-1023)
      voltage_total = voltage_total + voltage_reading[index]; //add voltage to voltage_total
      current_reading[index]=analogRead(A3); //read current reading from pin A3 (value 0-1023)
      current_total = current_total + current_reading[index]; // add current to current_total
      index = index + 1;

      if (index >=number_of_readings) { //once you have read number_of_reading, create average value of all readings taken
        current_average=current_total/number_of_readings;
        voltage_average=voltage_total/number_of_readings;
        Serial.print("Current average=");
        Serial.print(current_average);
        Serial.print("   Voltage average=");
        Serial.println(voltage_average);
        current_total=0;
        voltage_total=0;
        index=0;

      }


      float voltage = ((5*(voltage_average)*(R1+R2))/(1023*R2)); //convert unit value of voltage_average (0-1023) to actual voltage, substract voltage average value when not voltage should be read
      float current_mV = (current_average-current_zero)*5; //convert unit value of current_average(0-1023) to mV reading, substract surrent_average value when no current should be read
      float current = current_mV/185; //sensor is rated for 185mV/amp according to pdf, may need to change depending on calibration
      float power = (voltage*current); //calculate power




     u8g.firstPage();  
      do {
        draw();
      } while( u8g.nextPage() );


    }

The function of the code is to read data from a "ACS712 - 30A Range Current Sensor Module" and a "Voltage Detection Sensor Module". Display is a "0.96 inch 128x64 OLED Display Module - SPI/I2C - 7 Pin ", it is converted to I2C, and working with other codes. Board : Arduino Uno Max input voltage will be 12.8V, DC. The code is supposed to display the voltage on top, ampere in the middle, and watt usage at the bottom. But it is not working when edited for OLED display.

Someone please make this a working code. I am new to C+ and coding , so need detailed help.

Here is the code that worked with 16X2 LCD

#include <LiquidCrystal.h>
#include <Wire.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

/*The circuits
 * LCD RS pin to digital pin 12
 * LCD enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4                                                             21333333
 * LCD D6 pin to digital pin 3
 * 
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 */

byte lightning[8] = {
  B00001,
  B00110,
  B01100,
  B11000,
  B00011,
  B00110,
  B01100,
  };


int index = 0; //size of array
const int number_of_readings = 200; //total number of readings arduino takes before it averages
const int current_zero = 510; //initial value of current from arduino with 0 amps
int current_reading[number_of_readings]; //create array for current_reading
int voltage_reading[number_of_readings]; //create array for voltage_reading

float R1=30000; //resistance of R1 in voltage sensor
float R2=7500; //resistance of R2 in voltage sensor
float current_average = current_zero; //initial value of current average
float current_total = 0; //initial value of current_total
float voltage_average = 0; //initial value of voltage_average
float voltage_total = 0; //initial value of voltage_total


void setup (){
  Serial.begin (9600);
  lcd.begin(16,2); // size of LCD
  lcd.createChar(6,lightning); //create charecter named lightning


  //STARTUP MESSAGE
  lcd.setCursor (3,0); //set cusror to row 0, column 3
  lcd.print(" "); //lcdprint
  lcd.print("Arduino"); //lcdprint
  lcd.setCursor(1,1); //set cursor to row 1, column 1
  lcd.write(6); //lcd write charecter 6 (lightning bolt)
  lcd.print("Power Meter"); //lcd.print
  lcd.write(6); //lcd write charecter 6 (lightning bolt)
  delay(5000); //delay by 5 seconds
  lcd.clear();  //clear lcd screen
  }

void loop() {
  voltage_reading[index]=analogRead(A0); //read voltage reading from pin A0 (value 0-1023)
  voltage_total = voltage_total + voltage_reading[index]; //add voltage to voltage_total
  current_reading[index]=analogRead(A3); //read current reading from pin A3 (value 0-1023)
  current_total = current_total + current_reading[index]; // add current to current_total
  index = index + 1;

  if (index >=number_of_readings) { //once you have read number_of_reading, create average value of all readings taken
    current_average=current_total/number_of_readings;
    voltage_average=voltage_total/number_of_readings;
    Serial.print("Current average=");
    Serial.print(current_average);
    Serial.print("   Voltage average=");
    Serial.println(voltage_average);
    current_total=0;
    voltage_total=0;
    index=0;

  }
  float voltage = ((5*(voltage_average)*(R1+R2))/(1023*R2)); //convert unit value of voltage_average (0-1023) to actual voltage, substract voltage average value when not voltage should be read
  float current_mV = (current_average-current_zero)*5; //convert unit value of current_average(0-1023) to mV reading, substract surrent_average value when no current should be read
  float current = current_mV/185; //sensor is rated for 185mV/amp according to pdf, may need to change depending on calibration
  float power = (voltage*current); //calculate power


  lcd.clear(); //print voltage, current, and power values to LCD screen
  lcd.setCursor(0,0);
  lcd.print(voltage);
  lcd.print("V");

  lcd.print (" ");
  lcd.print(current);
  lcd.print("A");

  lcd.setCursor(0,1);
  lcd.print(power);
  lcd.print("W");
  delay(1);


}
  • ` it is not working in that way.` what do you mean by this? – Vaibhav May 18 at 7:06
  • @Vaibhav, it was a mistake, It is not working, that is what I mean. – Shijil May 18 at 7:15
1

In your Sketch

void draw(void) 
{
   u8g.setFont(u8g_font_fub14r); 
   u8g.drawStr(0, 20, "V: ");  
   u8g.drawStr(0, 40, "A: ");
   u8g.drawStr(0, 60, "Watt: ");
   u8g.setPrintPos(58,20);       
   u8g.print( Bat_Volt,2);  
   u8g.println("V"); 
   u8g.setPrintPos(58,40);        
   u8g.print( current,0); 
   u8g.println("mA"); 
   u8g.setPrintPos(58, 60);       
   u8g.print( power ,1);   
}

current, power, Bat_Volt are local variables define in loop function. Bat_Volt in not even defined in your sketch that's why they are not printing anything in draw function.

Initialize then as global variable in your sketch and in place if Bat_Volt you had use voltage here float voltage = ((5*(voltage_average)*(R1+R2))/(1023*R2)); //convert unit value of voltage_average (0-1023) to actual voltage, substract voltage average value when not voltage should be read in your sketch.

Correct that to.

Your code after edit:

#include <SPI.h>
#include <Wire.h>
#include "U8glib.h"  //Library for display 

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C interface for OLED  

int index = 0; //size of array
const int number_of_readings = 200; //total number of readings arduino takes before it averages
const int current_zero = 510; //initial value of current from arduino with 0 amps
int current_reading[number_of_readings]; //create array for current_reading
int voltage_reading[number_of_readings]; //create array for voltage_reading

float R1=30000; //resistance of R1 in voltage sensor
float R2=7500; //resistance of R2 in voltage sensor
float current_average = current_zero; //initial value of current average
float current_total = 0; //initial value of current_total
float voltage_average = 0; //initial value of voltage_average
float voltage_total = 0; //initial value of voltage_total
float voltage = 0.0,current_mV = 0.0,current = 0.0,power = 0.0;

int x = 0;
int row = 0;
float voltage = 0.1;  



  void setup() {
   Serial.begin(9600);
   pinMode(A4,INPUT_PULLUP);
   pinMode(A5, INPUT_PULLUP);
  }


  void draw(void) {
   u8g.setFont(u8g_font_fub14r); 
   u8g.drawStr(0, 20, "V: ");  
   u8g.drawStr(0, 40, "A: ");
   u8g.drawStr(0, 60, "Watt: ");
   u8g.setPrintPos(58,20);       
   u8g.print( voltage,2);  
   u8g.println("V"); 
   u8g.setPrintPos(58,40);        
   u8g.print( current,0);  
   u8g.println("mA"); 
   u8g.setPrintPos(58, 60);       
   u8g.print( power ,1);    
}


 void loop() {
  voltage_reading[index]=analogRead(A0); //read voltage reading from pin A0 (value 0-1023)
  voltage_total = voltage_total + voltage_reading[index]; //add voltage to voltage_total
  current_reading[index]=analogRead(A3); //read current reading from pin A3 (value 0-1023)
  current_total = current_total + current_reading[index]; // add current to current_total
  index = index + 1;

  if (index >=number_of_readings) { //once you have read number_of_reading, create average value of all readings taken
    current_average=current_total/number_of_readings;
    voltage_average=voltage_total/number_of_readings;
    Serial.print("Current average=");
    Serial.print(current_average);
    Serial.print("   Voltage average=");
    Serial.println(voltage_average);
    current_total=0;
    voltage_total=0;
    index=0;

  }


  voltage = ((5*(voltage_average)*(R1+R2))/(1023*R2)); //convert unit value of voltage_average (0-1023) to actual voltage, substract voltage average value when not voltage should be read
  current_mV = (current_average-current_zero)*5; //convert unit value of current_average(0-1023) to mV reading, substract surrent_average value when no current should be read
  current = current_mV/185; //sensor is rated for 185mV/amp according to pdf, may need to change depending on calibration
  power = (voltage*current); //calculate power


 u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );


}
  • Thank you for the reply, I have corrected the "Bat_Volt" into "voltage", but while saving it is showing error "'current' was not declared in this scope". is possible please edit the code for me. – Shijil May 18 at 8:19
  • declare current , power, voltage globally. You will get this error removed. – Vaibhav May 18 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.