4.Displaying Weight and Force from an FSR on an I2C LCD using Arduino

 Displaying Weight and Force from an FSR on an I2C LCD using Arduino

Circuit:

  1. FSR Setup:
    • Connect one end of the FSR to 5V.
    • Connect the other end of the FSR to an analog pin (e.g., A0) and a 10kΩ resistor.
    • Connect the other side of the resistor to GND.
  2. I2C LCD Setup:
    • Connect the LCD's SDA and SCL pins to the corresponding pins on the Arduino (usually A4 and A5 for Arduino Uno).
    • Connect VCC to 5V and GND to GND.

Code:

Here's the code that reads the FSR value and displays the weight in grams and force in newtons on the LCD:

C++ Code

#include <Wire.h> #include <Adafruit_LiquidCrystal.h> Adafruit_LiquidCrystal lcd(0); // Initialize the I2C LCD (default I2C address 0x20) int fsrPin = A0; // Analog pin connected to FSR int fsrReading; // Variable to store the FSR reading float calibrationFactor = 0.5; // Calibration factor to convert FSR reading to weight in grams (example value) float weight; // Calculated weight in grams float force; // Calculated force in newtons void setup() { lcd.begin(16, 2); // Set up the LCD's number of columns and rows (16x2) lcd.print("FSR Reading:"); // Display static text } void loop() { fsrReading = analogRead(fsrPin); // Read the value from FSR // Convert the FSR reading to weight in grams using the calibration factor weight = fsrReading * calibrationFactor; // Convert weight (grams) to force (newtons) // 1 gram = 0.00981 newtons force = weight * 0.00981; // Display weight and force on the LCD lcd.setCursor(0, 1); // Set the cursor to the beginning of the second row lcd.print("g: "); lcd.print(weight); lcd.print(" N: "); lcd.print(force); lcd.print(" "); // Clear trailing characters delay(500); // Add delay for readability }

Explanation:

  1. Calibration Factor:

    • calibrationFactor is used to convert the FSR reading to a weight in grams.
    • You need to determine this value experimentally by placing known weights on the FSR, observing the fsrReading values, and calculating the calibration factor that provides an accurate weight.
  2. Weight to Force Conversion:

    • force = weight * 0.00981 converts the weight in grams to force in newtons, where 1 gram = 0.00981 newtons.
  3. LCD Display:

    • The LCD shows both the weight in grams and the force in newtons on the second row.
    • lcd.print("g: ") and lcd.print(" N: ") add labels to the display.
    • lcd.print(" ") clears any leftover characters when the readings change.



ChatGPT

Comments

Popular posts from this blog

2.Controlling LEDs with a Keypad and Arduino

Measuring and Displaying Solar Panel Voltage on an LCD using Arduino