Hi folks,
Thanks to this forum and ressources found on the internet, I managed to work on my project that is to build a force gauge, or a dynaometer, print the data on a screen, and also save the data on an sd card.
Basically, it's just a scale with a S type loadcell.
I managed to make work the screen and the loadcell, however, as soon as I add the parts of code related to the SD card in the void loop or setup, one part of the program does not execute itself, and in addition, the effort measured by the sensor remains at 0. The code have no mistakes that is the strangest part.
Here is two pieces of code, one with the scale and screen only and another with the scale, the screen and the SD module.
In a nutshell, I would like to understand why once I add the SD module related code, my force gauge "does not work" anymore, and how to cure the issue.
Thanks for your answers
Code I Just the loadcell and the screen:
#include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
#include <SD.h> // Importe la librairie SD pour permettre la gestion de la carte SD
HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int led = 2;
int taree = 6;
int buzzer = 10;
int a = 0;
float b = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode (taree, INPUT_PULLUP);
LoadCell.begin(); // start connection to HX711
LoadCell.start(1000); // load cells gets 2000ms of time to stabilize
LoadCell.setCalFactor(42.5); // calibration factor for load cell => strongly dependent on your individual setup
lcd.begin(); // begins connection to the LCD module
lcd.backlight(); // turns on the backlight
lcd.setCursor(1, 0); // set cursor to first row
lcd.print("Dynamometre"); // print out to LCD
lcd.setCursor(0, 1); // set cursor to first row
lcd.print("Par Vincent M"); // print out to LCD
delay(3000);
lcd.clear();
}
void loop() {
lcd.setCursor(1, 0); // set cursor to first row
lcd.print("Valeur d'effort"); // print out to LCD
LoadCell.update(); // retrieves data from the load cell
float i = LoadCell.getData(); // get output value
if (i<0)
{
i = i * (-1);
lcd.setCursor(0, 1);
lcd.print("-");
}
else
{
lcd.setCursor(0, 1);
lcd.print(" ");
}
lcd.setCursor(1, 1); // set cursor to second row
lcd.print(i, 1); // print out the retrieved value to the second row
lcd.print("g ");
//if (digitalRead (taree) == HIGH)
//{
//lcd.setCursor(0, 1); // set cursor to secon row
//lcd.print(" Taring... ");
//LoadCell.start(1000);
//lcd.setCursor(0, 1);
//lcd.print(" ");
//}
}
Code of the dynamometer with the SD module
//-------------------------------------------------------------------------------------------
//Librairies
#include <LiquidCrystal_I2C.h> // Importe la librairie LiquidCrystal_I2C pour la gestion de l'écran LCD
#include <HX711_ADC.h> // Importe la librairie HX711_ADC pour la gestion de l'amplificateur HX711 et du capteur d'effort
#include <SD.h> // Importe la librairie SD pour permettre la gestion de la carte SD
HX711_ADC LoadCell(4, 5); // Allocation du capteur d'effort sur les broches 4 et 5, paramètres : dt pin, sck pin
LiquidCrystal_I2C lcd(0x27, 16, 2);// Set the LCD address to 0x27 for a 16 chars and 2 line display
File monFichier; // Génération du fichier de données
void setup() {
Serial.begin(115200);
Serial.print("Initialisation de la carte SD en cours..."); // Afficher sur le moniteur série le texte Initialisation de la carte SD en cours ...
if (SD.begin()) { // Si la carte SD est initialisée ...
Serial.println(" Initialisation terminee."); // Afficher sur le moniteur série le texte Initialisation terminée
} else { // Si l'initialisation ne fonctionne pas
Serial.println(" Echec de l'initialisation."); // Afficher sur le moniteur série le texte Echec de l'initialisation
while (true); // on s'arrête ici dans le cas d'un échec de l'intialisation de la carte SD
}
monFichier = SD.open("donnees.csv", FILE_WRITE); // on crée le fichier s'il faut, sinon écrit à la fin du fichier
if (monFichier) {
monFichier.println(F("Valeur d'effort")); // Affiche le texte valeur d'effort dans le fichier texte
Serial.println(F("Valeur d'effort")); // Affiche le texte valeur d'effort dans le moniteur série
monFichier.close(); // Ferme le fichier texte
} else { // Si l'écriture ne fonctionne pas
Serial.println(F(" Echec de l'écriture de l'en tête."));
while (true); // on s'arrête ici dans le cas d'un échec de l'écriture
}
LoadCell.begin(); // Connextion a l'amplificateur HX711
LoadCell.start(2000); // Le capteur d'effort a 2000ms pour se stabiliser
LoadCell.setCalFactor(43.5); // Mise en place du facteur de calibration, valable pour un unique montage
lcd.begin(); // démarrage de l'afficheur LCD, ça efface l'écran
lcd.backlight(); // Allumage du rétro-éclairage
lcd.print(F("*** PRET ***")); // Affichage sur l'écran LCD du texte Initialisation
}
void loop() {
LoadCell.update(); // retrieves data from the load cell
float effort = LoadCell.getData(); // get output value
lcd.setCursor(0, 0); // set cursor to first row
lcd.print(F("Poids[g]:")); // print out to LCD
lcd.setCursor(0, 1); // set cursor to second row
lcd.print(effort); // print out the retrieved value to the second row
// On enregistre la donnée
monFichier = SD.open("donnees.csv", FILE_WRITE); //Maximum 8 caractères avant le .csv
if (monFichier) {
monFichier.println(effort);
Serial.println(effort);
monFichier.close();
} else {
Serial.println(F("Impossible d'ouvrir le fichier"));
}
delay(5000);
}
Data sheets:
HX 711: HX711 Fiches technique, PDF - Alldatasheet
Loadcell: Amazon.com
SD module: SPI SD lecteur de carte voulait? – AZ-Delivery
Dynamom_tre_sans_fonction__SD.ino (1.87 KB)
Dyanmometre_avec_fonction_SD.ino (3.02 KB)