LCD Bar Graph and Progress Bar

Hi everyone,
I was interested in creating a Bar Graph on my LCD (using the standard Library)... Googling and searching on the forum i found some interesting stuff, including a fantastic library.

Anyway i found them too complicated and I just decided to write a simple and user-friendly function which can be implemented in the sketch code simply copying and pasting it, without having to include a whole library, you just need to initialize the LCD library with the pins connected to the display.

It has four parameters:
LCD_progress_bar (int row, int var, int minVal, int maxVal)
The Row to use, the Variable to adapt to, the Minimum and the Maximum value of the variable.

void LCD_progress_bar (int row, int var, int minVal, int maxVal)
{
  int block = map(var, minVal, maxVal, 0, 16);   // Block represent the current LCD space (modify the map setting to fit your LCD)
  int line = map(var, minVal, maxVal, 0, 80);     // Line represent the theoretical lines that should be printed 
  int bar = (line-(block*5));                             // Bar represent the actual lines that will be printed
  
  /* LCD Progress Bar Characters, create your custom bars */

  byte bar1[8] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
  byte bar2[8] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18};
  byte bar3[8] = { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C};
  byte bar4[8] = { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E};
  byte bar5[8] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
  lcd.createChar(1, bar1);
  lcd.createChar(2, bar2);
  lcd.createChar(3, bar3);
  lcd.createChar(4, bar4);
  lcd.createChar(5, bar5);
  
  for (int x = 0; x < block; x++)                        // Print all the filled blocks
  {
    lcd.setCursor (x, row);
    lcd.write (1023);
  }
  
  lcd.setCursor (block, row);                            // Set the cursor at the current block and print the numbers of line needed
  if (bar != 0) lcd.write (bar);
  if (block == 0 && line == 0) lcd.write (1022);   // Unless there is nothing to print, in this case show blank
  
  for (int x = 16; x > block; x--)                       // Print all the blank blocks
  {
    lcd.setCursor (x, row);
    lcd.write (1022);
  }
}

I know it is very simple, but it works fine and smoothly.
I hope this topic will help someone (like me) who is searching for an easy solution to use in his project.
It's not necessary to say that any advices, improvements or critics will be well accepted.

(remember to use a little delay in the loop (e.g. delay (40):wink: to avoid a poor quality printing on the display)

I haven't tried it but, thank you for sharing.

Not bad. These are some I made that work too. It looks like a lot is there but there really isn't. Lcd bargraph help? - Programming Questions - Arduino Forum

Hi there. I am new into arduino and C. I`m trying to run this program.
here is the code I implemented:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


void LCD_progress_bar (int row, int var, int minVal, int maxVal)

  int block = map(var, minVal, maxVal, 0, 20);   // Block represent the current LCD space (modify the map setting to fit your LCD)
  int line = map(var, minVal, maxVal, 0, 8);     // Line represent the theoretical lines that should be printed 
  int bar = (line-(block*5));                             // Bar represent the actual lines that will be printed

  /* LCD Progress Bar Characters, create your custom bars */

  byte bar1[8] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
  byte bar2[8] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18};
  byte bar3[8] = { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C};
  byte bar4[8] = { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E};
  byte bar5[8] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};

  void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // Used to type in characters
  lcd.begin(20,4); // Initialize the lcd for 20 chars 4 lines, turn on backlight

  
  lcd.createChar(1, bar1);
  lcd.createChar(2, bar2);
  lcd.createChar(3, bar3);
  lcd.createChar(4, bar4);
  lcd.createChar(5, bar5);
  }
 void loop() { 
  
  for (int x = 0; x < block; x++)                        // Print all the filled blocks
  {
    lcd.setCursor (x, row);
    lcd.write (1023);
  }
  
  lcd.setCursor (block, row);                            // Set the cursor at the current block and print the numbers of line needed
  if (bar != 0) lcd.write (bar);
  if (block == 0 && line == 0) lcd.write (1022);   // Unless there is nothing to print, in this case show blank
  
  for (int x = 20; x > block; x--)                       // Print all the blank blocks
  {
    lcd.setCursor (x, row);
    lcd.write (1022);
  }
}

of course, the erors are here:

Arduino: 1.6.3 (Windows 7), Board: "Arduino Uno"

LcdBarGraf.ino:12:3: error: expected initializer before 'int'

LcdBarGraf.ino:13:18: error: 'var' was not declared in this scope

LcdBarGraf.ino:13:23: error: 'minVal' was not declared in this scope

LcdBarGraf.ino:13:31: error: 'maxVal' was not declared in this scope

LcdBarGraf.ino:14:20: error: 'block' was not declared in this scope

LcdBarGraf.ino: In function 'void loop()':

LcdBarGraf.ino:38:23: error: 'block' was not declared in this scope

LcdBarGraf.ino:40:23: error: 'row' was not declared in this scope

LcdBarGraf.ino:44:18: error: 'block' was not declared in this scope

LcdBarGraf.ino:44:25: error: 'row' was not declared in this scope

Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

I would appreciate some help. i know I am but what I`m doing wrong?

You're missing the "{" on the function...

void LCD_progress_bar (int row, int var, int minVal, int maxVal)

Thank's a lot sharing it. Great work.

Can this code be modified so it starts from center?
eample.. Range 0-100
0 is filled from left to center
50 is only one line in the middle
100 id filled from center to right

thank you

Thank you for this, very helpful

I needed to change all the lcd.write(x) statement to lcd.write(byte(x)) to avoid a compiler error, and I could not work out how lcd.write(byte(1022)) and lcd.write(byte(1023)) worked - they printed a blank character or full character but why 1022 and 1023??

Regards

Chris

Great work, but could post some examples describing the variables.

Thanks

Harry