0

So we were asked to do something with a force sensitive resistor so naturally i thought of a project which takes the name of the main component which is force and applies it ,mine works by as you apply more and more pressure to the sensor it takes this value and displays it on an lcd but the display should be a bar graph which increases as the pressure increases. the problem is i dont really know how to do a bar graph on a 16x2 lcd. Please help. (my lcd has an I2c)

here is my code for the pressure sensor so far

int fsrAnalogPin = 0; // FSR is connected to analog 0
int fsrReading;      // the analog reading from the FSR resistor divider

void setup(void) {
  Serial.begin(9600);  
}

void loop(void) {
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
  delay(100);
}

ok so i improved my code and i finally got it this code displays the force detected by the fsr into a bar graph

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

byte lcdNumCols = 20; // -- number of columns in the LCD

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);   // -- creating LCD instance

int fsrAnalogPin = 0; // FSR is connected to A0

// -- creating a 4 chars wide bars
LcdBarGraphX lbg0(&lcd, 16, 0, 0); // -- First line at column 0 and row 0 with 16 columns

void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  lcd.begin(4, lcdNumCols); // start lcd with 2 rows and 16 columns
  lcd.clear();
  delay(100);
}

void loop(void) {
  lbg0.drawValue( analogRead(fsrAnalogPin), 9); // the 9  is the maximum range you want for pressure
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
  delay(100);
}
  • your question has nothing to do with a force sensor ... it is a question about drawing bar graphs on an LCD .... you should really edit your question and the title so that it reflects the actual question .... that way, people will find your question if they are researching graphing on an LCD – jsotola Oct 13 '18 at 19:13
1

There's two ways you can do it: the easy way, and the fancy way.

The easy way really is easy. Since the display can only display characters you just display characters - enough to represent a bar graph.

If you have a display with "Rom Code A00"* then character 255 is a solid block (▇) so you just print between 0 and 16 of them in a row. 0 is 0% and 16 is 100%. (If you have ROM Code A02 then you could just use #, or custom design a character - see below).

Force:▒▒▒▒▒▒▒▒▒▒
▇▇▇▇▇▇▇▇▇▒▒▒▒▒▒▒

Or with #:

Force:▒▒▒▒▒▒▒▒▒▒
#########▒▒▒▒▒▒▒

The fancy way is to use the programmable characters facility. You would create 4 new characters that partially fill a block. Each character is 5 pixels wide, so creating a character with 1 column of pixels set (▏), one with 2 set (▍), 3 set (▌) and 4 set (▋) (you already have all 5 set on in character 255 if you have ROM Code A00 - otherwise define a 5th with all 5 columns on). Now you can be clever and work out:

  1. How many full characters to print, and
  2. Which of the extra characters to print on the end.

That gives you effectively 16*5 (80) pixels of horizontal resolution (albeit with a small gap between each set of 5 pixels).

Exactly how you do whichever of those methods you choose is now up to you.


*: ROM Code A00 is the character ROM with Japanese characters in the characters 128-255. ROM Code A02 has European accented characters instead.

1

From the Library Manager, install the LcdBarGraphX library by Balazs Kelemen.

There are several examples there as well.

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.