Current meter using 128X64 LCD display

Hello everyone,
I am making one current meter project for myself and i am using ARDUINO UNO, ADS1115 for (16-bit resolution for ADC), sensor and 128X64 LCD display.
i have used digital inputs for range selection switch purpose and i have also attached the schematic.

i have also tried to upload the given code but after uploading it is just displaying "Welcome to the Current meter project" and not proceeding further for the measurement purpose.

Can somebody help me where the issue present. please help.
Thank you in advance

#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include <U8g2lib.h>

// ADS1115 and LCD config
const int ADS1115_ADDR = 0x48;
const int switchPin1 = 2;
const int switchPin2 = 3;

Adafruit_ADS1115 ads;
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, 13, 11, 10, 8); // CLK, DATA, CS, RESET

void setup() {
  Wire.begin();

  ads.setGain(GAIN_ONE); // ±4.096V, 0.125 mV/bit
  ads.begin(ADS1115_ADDR);

  u8g2.begin();
  u8g2.setFont(u8g2_font_ncenB08_tr);

  // Splash screen
  u8g2.clearBuffer();
  u8g2.drawStr(20, 28, "Welcome to the");
  u8g2.drawStr(5, 40, "Current Meter Project");
  u8g2.sendBuffer();
  delay(2000);
  
  u8g2.clearBuffer();
  
  pinMode(switchPin1, INPUT_PULLUP);
  pinMode(switchPin2, INPUT_PULLUP);
}

void loop() {
  int switchState1 = digitalRead(switchPin1);
  int switchState2 = digitalRead(switchPin2);

  int16_t sensorvalue = ads.readADC_SingleEnded(0);
  float sensorvalue_1 = sensorvalue * 1.640; // To convert sensor value to desired sensor value; this multiplication factor used.
  float voltage = sensorvalue_1 * 5.0 / 65535.0; // Convert to voltage (V)

  if (switchState1 == LOW && switchState2 == HIGH) {
    // 5A Range
    if (voltage < 0.0072) {
      u8g2.drawStr(0, 16, "Under Range");
      u8g2.drawStr(0, 32, "Current (A): N/A");
    } else if (voltage >= 0.0072 && voltage <= 0.72) {
      float current_mA = (voltage / 0.72) * 1000.0;
      u8g2.drawStr(0, 16, "Current-5A (mA):");
      char buffer[20];
      sprintf(buffer, "%.2f", current_mA);
      u8g2.drawStr(0, 32, buffer);     
    } else if (voltage > 3.6) {
      u8g2.drawStr(0, 16, "Change Range");
      u8g2.drawStr(0, 32, "Current (A): N/A");   
    } else {
      float current = voltage / 0.72;
      u8g2.drawStr(0, 16, "Unknown State");
      char buffer[20];
      sprintf(buffer, "%.3f", current);
      u8g2.drawStr(0, 32, buffer);  
    }

  } else if (switchState1 == HIGH && switchState2 == LOW) {
    // 50A Range
    if (voltage < 0.0011) {
      u8g2.drawStr(0, 16, "Under Range");
      u8g2.drawStr(0, 32, "Current (A): N/A");  
    } else if (voltage >= 0.0011 && voltage <= 3.6) {
      float current = (voltage / 0.72) * 10.0;
      u8g2.drawStr(0, 16, "Current-50A (A):");
      char buffer[20];
      sprintf(buffer, "%.2f", current);
      u8g2.drawStr(0, 32, buffer); 
    } else {
      u8g2.drawStr(0, 16, "OVERLOAD");
      u8g2.drawStr(0, 32, "Current (A): N/A"); 
    }

  } else {
    // Both switches HIGH or LOW: invalid or no sensor connected
    u8g2.drawStr(0, 16, "Connect & Switch");
    u8g2.drawStr(0, 32, "On the Sensor");
  }

  u8g2.sendBuffer();
  delay(1000); // Refresh every second
}
![Display Image|690x423](upload://bNXEyQKMC7wvl8gbDu8Yj9lpUb8.jpeg)
![Schematic|690x280](upload://y0N5rA3VXidR6h9LCRWtdUmnZSm.png)

You forgot this?


Yes, i just uploaded. thank you for drawing my attention.

That is not a schematic. It is a block diagram with very little detail. The forum may need a real schematic showing part numbers, pin numbers, power supplies and so on.

What happens when you sprinkle in some Serial.print() statements in your code and view the result on Serial Monitor? That is the most common way to debug a program.

Who has given you this code? Have you asked them for help?

My early impression of this code is that it is not the best. Software SPI is used for the display when hardware could have been used. Also, no error checking is done when the display and the external ADC are initialised.

Now I see what happened. You included the images in your original post, but you put them inside the code tags. That is why the images did not appear in the post. Put only code (or error messages) inside your code tags.

Try this change to setup() and tell us if anything is printed in serial monitor.

void setup() {
  ...
  Serial.begin(115200);
  ...
  if (!ads.begin(ADS1115_ADDR)) {
    Serial.println("Failed to initialize ADS.");
    while (1);
  }
  ...

Why is the display buffer never cleared in loop()?

Maybe there are other cases then the two switch states you have implemented? Perhaps do a drawStr at start of loop()?

Actually i made this project with 16X2 LCD display but i want to create the same in more interactive manner so i tried to change the code for 128 X 64 LCD display. i will not lie but yes i have taken AI help for 128 X 64 LCD display.

i will try your solution and check if this will work or not

i will try your solution and check to debug the program

I'm surprised that your sketch prints the first two string...
The constructor that you use ( U8G2_ST7920_128X64_F_SW_SPI) try to allocate a full buffer for the display, in you case 128x64 = 8192 bytes, far over the arduino uno ram capacity (2048 bytes).
The successive clearBuffer() probably crash the arduino.
If your program does not use graphics (as it seems), consider using u8x8 instead, this one use no buffer at all.

Ciao, Ale.

Not quite. The display is 128x64 pixels, which is 8192 bits of buffer, not bytes. Divide by 8 to get the actual buffer size of 1024 bytes.
The U8g2 library allocates the buffer at compile-time, so it is included in the dynamic memory usage shown by the compiler.
Instead of drawStr(), U8g2 can use print(), which allows the use of the F() macro so that text literals are not stored in ram.

That constructor uses software SPI, not sure why anyone would use that with the hardware SPI pins, since hardware SPI would be faster.

sprintf on an UNO does not support float, use dtostrf() instead if you want to use leading spaces to keep the decimal point in a fixed position, otherwise print the value directly using print() instead of drawStr().

Hello Everyone,
Thank you so much for all of your support. i have planned to shift the program from 128X64 display to 20X4 character LCD display. i am little bit in hurry so i am shifting on that.

once again thank you very much for all of your support, in future if i will with 128X64 display then i will revert my comments on this forum.

Best regards