A simple loadcell calibration program based on a load cell and HX711 amp. Using HX711 by Bertrand Bouvier. Calibration code comes from SparkFun example at https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide?_ga=1.169212841.987765858.1442369657.

Dependencies:   HX711 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HX711.h"
00003 
00004 DigitalOut gpo(D0);
00005 DigitalOut led(LED_BLUE);
00006 HX711 scale(A0, A1);
00007 
00008 AnalogIn scaleRaw(A3);
00009 Serial pc(USBTX, USBRX);    // USB Serial Terminal
00010 float calibration_factor = 1000; //-7050 worked for my 440lb max scale setup
00011 int averageSamples = 100;
00012 
00013 int main(void)
00014 {
00015     pc.printf("Starting Scale");
00016     pc.printf("HX711 calibration sketch");
00017     pc.printf("Remove all weight from scale");
00018     pc.printf("After readings begin, place known weight on scale");
00019     pc.printf("Press + or a to increase calibration factor");
00020     pc.printf("Press - or z to decrease calibration factor");
00021     
00022       
00023     scale.setScale(0);
00024     scale.tare(); //Reset the scale to 0
00025     
00026     long zero_factor = scale.averageValue(averageSamples); //Get a baseline reading
00027     pc.printf("Zero factor: %.4f\n" , zero_factor); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
00028     
00029     while (true) {
00030         scale.setScale(calibration_factor); //Adjust to this calibration factor
00031         float weight = scale.getGram();
00032         //float raw = scaleRaw.read();
00033         pc.printf("Reading: %.2f\n", weight);
00034         //pc.printf("Raw Value: %.7f\n", raw);
00035         pc.printf(" calibration_factor: %.2f\n", calibration_factor);
00036 
00037 
00038        if(pc.readable()) {
00039             char temp = pc.getc();
00040             if(temp == '+' || temp == 'a')
00041                calibration_factor += 10;
00042             else if(temp == '-' || temp == 'z')
00043                 calibration_factor -= 10;
00044             }
00045         gpo = !gpo; // toggle pin
00046         led = !led; // toggle led
00047         wait(0.2f);
00048     }
00049 }