element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Analog input 'Tare/Zero' Weight Function
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 17 replies
  • Answers 2 answers
  • Subscribers 418 subscribers
  • Views 2947 views
  • Users 0 members are here
Related

Analog input 'Tare/Zero' Weight Function

e14 Contributor
e14 Contributor over 13 years ago

Hi, I am currently working with a load cell amplifier, the output I have taken to Anal Input A0, I need to 'Tare/Zero' the low weight input value, and subtract that value from further

weighed values.

 

I would like to carry out the 'Tare/Zero' via a push button (one shot) which would store the zero value until the power supply is removed.

 

I would also like to display the zero weight via the 'Monitor' function, in order to proove the 'Tare' function is working every time.

 

I have completed the weighing code of the project. but I am completely stumped on the 'Tare/Zero' stage, I can't find an example of storing or one shot code.

 

I hope you can help to enable me to go forward with the project.

 

Regards,

 

Gordon

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 13 years ago in reply to e14 Contributor +1
    Gordon That result is a different problem than the impression you gave from you first post..... To prove the loadcell is working, try loading and running the example AnalogInOutSerial. This should prove…
  • e14 Contributor
    e14 Contributor over 13 years ago in reply to mcb1 +1
    Hi Mark, Hurray, I have found the problem!! I needed to create a new register 'int ZeroVal' as shown. then later the subtraction worked! I have improved the HMI monitor layout and it looks great, all I…
Parents
  • e14 Contributor
    0 e14 Contributor over 13 years ago

    Just paste the sketch into www.pastebin.com and return the link. Its makes it clean for looking at. If you want to get real fancy, select c++ highlighting.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to e14 Contributor

    Hi,

    I have joined Pastebin and pasted the code but how do you save it?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to e14 Contributor

    Hi,

    I have joined Pastebin and pasted the code but how do you save it?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • mcb1
    0 mcb1 over 13 years ago in reply to e14 Contributor

    Gordon

    You can just paste it here

    There are various options if you click the Advanced Editor in top right, and then select the double chevron

    However none of them do much except the quote.

     

    image

     

    Most people just dump it here, but he developers are meant to be working on a more eligant method.

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to mcb1

     

    #include <SD.h>

    int CS_pin = 10;
    int pow_pin = 8;
    const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
    float sensorValue = 0;       // value read from the pot
    int switchState = 0;         // Dat enter push button.
    int led = 9;                // Data transfered LED
    const int buttonPin = 3;     /// the number of the Tare/Zero pushbutton pin
    int buttonState = 0;         /// variable for reading the pushbutton status
    int TareValue;
    int FinalWt;

    void setup() {
        // initialize serial communications at 9600 bps:
      Serial.begin(9600);
      Serial.println("Initializing Card");
      //CS Pin is an output
      pinMode(CS_pin, OUTPUT);
     
      //Card will draw power from Pin 8, so set High
      pinMode(pow_pin, OUTPUT);
      digitalWrite(pow_pin, HIGH);
     
      //Check card is ready
      if(SD.begin(CS_pin))
    {
      Serial.println("Card Failed");
      return;
    }
    Serial.println("Card Ready");

    }
    void loop() {
    // read the Tare input pin:
      buttonState = digitalRead(buttonPin);
      if (buttonState == HIGH){
      int TareValue = analogRead(A0);
      Serial.println (TareValue);
      }
    {
      String dataString = "Results";
     
      //Open a file to write to, only one at a time.
      File dataFile = SD.open("DATAFILE.TXT", FILE_WRITE);
      if(dataFile)
     
      {
        dataFile.println(dataString);
        dataFile.close();
        Serial.println(dataString);
      }
      //else
      {
        // Serial.println("Couldn't access file");
      }
      delay(1000);
      //pinMode(buttonPin, INPUT);
      //int TareValue = analogRead(A0);
      //int FinalWt = sensorValue - TareValue;

      // read the analog in value:
      sensorValue = analogRead(0) / 4.26;(analogInPin);
      sensorValue = sensorValue / 16;
      //sensorValue = analogRead(0) / 0.1024;(analogInPin);
      //sensorValue = sensorValue / 1000;
      pinMode(led, OUTPUT);
     
      switchState = digitalRead(2);
     
      if (switchState == HIGH){
     
      FinalWt = (analogRead(0) - TareValue);
      //int FinalWt = 100 - 25;
      // print the results to the serial monitor:
      Serial.println(FinalWt);
      Serial.print(analogRead(0));
      Serial.print(" weight = " ); 
      Serial.print(sensorValue);
      Serial.print(" lbs/oz");
      ///Serial.print(" K/g");
      Serial.println ();
      digitalWrite(led, HIGH);
      delay(100);               // wait for a 100ms
      digitalWrite(led, LOW);

      // wait 400ms before the next loop
      // for the analog-to-digital converter to settle
      // after the last reading:
      delay(400);                    
      }
      }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to mcb1

    Hi Mark,

     

    I hope you have received the code at last, don't forget it is my first attempt so there may be some unnecessary code.

     

    Can you let me know if you have received it please.

     

    Regards,

     

    Gordon

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 13 years ago in reply to e14 Contributor

    Gordon

     

    Seen your message.

    Give me some time with it, and I'll respond.

     

    In the meantime feel free to try yourself, you'll never know until you try.

     

    I always find that sitting down with a piece of paper and block representation of what I'm trying to achieve helps ...(or not).

    Worrying about what the code is, doesn't always help since there are often multiple ways to achieve it.

     

    Personally I don't worry about how 'tight' my code is, since the micro is mostly just waiting around, as in your case.

    The areas to concentrate on is the HMI (Human 'micro' Interface) since pushing the button and wondering is never a good look.

     

     

    cheers

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 13 years ago in reply to mcb1

    Gordon

    I've made some changes and added comments to make them a little more obvious

    I think one of the stumbling blocks was not resetting the buttonState after you had done what was necessary.

    Each time it looped back it assummed the button was pressed and did the Tare/Zero thing.

     

    #include <SD.h>

     

    int CS_pin = 10;

    int pow_pin = 8;

    const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

    float sensorValue = 0;       // value read from the pot

     

    // ** changed

    int switchState = 2;         // Dat enter push button.

    // *******

     

    int led = 9;                // Data transfered LED

    const int buttonPin = 3;     /// the number of the Tare/Zero pushbutton pin

    int buttonState = 0;         /// variable for reading the pushbutton status

    int TareValue;

    int FinalWt;

     

    void setup() {

        // initialize serial communications at 9600 bps:

      Serial.begin(9600);

      Serial.println("Initializing Card");

     

      // ****Added these pinModes *****

      //inputs

      pinMode(switchState, INPUT);    //digital input 2

      pinMode(buttonPin, INPUT);      //digital input 3

     

      //outputs

      pinMode (led, OUTPUT);    //led on pin 9  ...needs to be set otherwise its very dull.

     

      // ***********

     

      //CS Pin is an output

      pinMode(CS_pin, OUTPUT);

     

      //Card will draw power from Pin 8, so set High

      pinMode(pow_pin, OUTPUT);

      digitalWrite(pow_pin, HIGH);

     

      //Check card is ready

      if(SD.begin(CS_pin))

    {

      Serial.println("Card Failed");

      return;

    }

    Serial.println("Card Ready");

     

    }

    void loop()

    {

    // read the Tare input pin:

      buttonState = digitalRead(buttonPin);

     

      if (buttonState == HIGH)    // Tare/Zero button pressed

      {

        int TareValue = analogRead(A0);

        Serial.println (TareValue);

        buttonState = LOW; // *** need to reset this once you've done whats needed ****

      }

     

      String dataString = "Results";

     

      //Open a file to write to, only one at a time.

      File dataFile = SD.open("DATAFILE.TXT", FILE_WRITE);

      if(dataFile)

       {

          dataFile.println(dataString);

          dataFile.close();

          Serial.println(dataString);

       }

      //else

      {

        // Serial.println("Couldn't access file");

      }

      delay(1000);

      //pinMode(buttonPin, INPUT);

      //int TareValue = analogRead(A0);

      //int FinalWt = sensorValue - TareValue;

     

      // read the analog in value:

      sensorValue = analogRead(0) / 4.26;(analogInPin);

      sensorValue = sensorValue / 16;

      //sensorValue = analogRead(0) / 0.1024;(analogInPin);

      //sensorValue = sensorValue / 1000;

      //pinMode(led, OUTPUT); // *** moved this to the setup. ***

     

     

    // *** removed below and changed the if statement since we set switchState to D2 in the declarations ****

      //switchState = digitalRead(2);

     

      if (digitalRead(switchState) == HIGH)

      {

          // *********

          digitalWrite(led, HIGH);    //shows switchstate is HIGH *****

     

          FinalWt = (analogRead(0) - TareValue);

          //int FinalWt = 100 - 25;

          // print the results to the serial monitor:

          Serial.println(FinalWt);

          Serial.print(analogRead(0));

          Serial.print(" weight = " );

     

         // *** This is a value that has had some calculations but not had the Tare/Zero removed. ****

          Serial.print(sensorValue);

          // perhaps the value here should be FinalWt with the necessary calculations performed.

     

          Serial.print(" lbs/oz");

          ///Serial.print(" K/g");

          Serial.println ();

        //  digitalWrite(led, HIGH);

        //  delay(100);               // wait for a 100ms  ****** You might want to increase this as its a very short duration ***

        //  digitalWrite(led, LOW);

     

          // wait 400ms before the next loop

          // for the analog-to-digital converter to settle

          // after the last reading:

          delay(400);

          digitalWrite(led, LOW); // shows the process is finished and is on for approx 400-410mS give or take. 

      }

    }

     

    I also changed the LED to be on for longer than 100mS, since you would struggle to see that pulse.

     

    I noted that your calculations of the actual weight aren't done, and put that down to the troubles you were having.

    I'm sure you can tidy that up when you start getting sensible readings.

     

    I've done some stuff with reading the contents of SD cards, but not a lot with writing to them.

    One thing to make sure is that strings end with a null character.

    Often they write a zero onto it if the original data wasn't a string.

     

    The example Datatlogger converts it to a string

    int sensor = analogRead(analogPin);

    dataString += String(sensor);

    Currently you are just writing a generic string ...

     

    It compiles but other than that I have no promises.

    Give it a whirl and see if it resolves the problems.

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to mcb1

    Hi Mark,

     

    Still the same I'm afraid, it still seems to be around the subtraction function, FinalWt = (AnalogRead(A0) - TareValue); I have monitored the function and the result

    is always the same, the value of eg; A0 = 500 and the TarValue = 100 the FinalWt result = 500?

     

    if I change the TareValue  to a Constant say 200 then the result = 300?

     

    If I change the TareValue to (int TareValue) the code will not compile asking for "expected primary-expression before 'int' could this be the problem?

     

    Regards,

     

    Gordon

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 13 years ago in reply to e14 Contributor

    Gordon

    That result is a different problem than the impression you gave from you first post.....

     

    To prove the loadcell is working, try loading and running the example AnalogInOutSerial.

    This should prove the connections are working as they should be, as it uses Ao.

     

    As you change the weight, you should be seeing a change in the output.

     

    Some details of the actual loadcell in use would also help complete the picture.

     

     

    Changing int settings or the like is not the problem.

    Look at Help-reference and int for an explanation on what it means.

    Constant int is one that the program can't and doesn't change.

    Without seeing the code, its hard to judge why its reporting that, but I suspect its an extra or missing bracket or semicolon.

     

     

    A copy and paste of the output of the Serial Monitor might also help, as you already output the TareValue when the Tare/Zero button is pressed.

     

    Can you confirm that you have a resistor from the pin to ground as shown in this http://arduino.cc/en/Tutorial/Button

    If not the pin will be floating and could be either HIGH or LOW, which will also stuff up your Tare/Zero reading.

    If pin 2 is floating the whole sketch should be cycling and the led flashing at about 2Hz rate.

     

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to mcb1

    Hi Mark,

     

    Hurray, I have found the problem!! I needed to create a new register 'int ZeroVal' as shown. then later the subtraction worked!

     

    I have improved the HMI monitor layout  and it looks great, all I need to do now is work on the send to the SD card.

     

    If you don't mind I'll keep in touch, thanks again for your help.

     

    Regards,

     

    Gordon

     

     

    int TareValue = analogRead(A0);

        ZeroVal = TareValue;

        Serial.print("Tare Value = ");

        Serial.println (TareValue);


    ____________________________________________

     

    if (digitalRead(switchState) == HIGH)

      {

          // *********
         
          digitalWrite(led, HIGH);    //shows switchstate is HIGH *****
         
          FinalWt = (analogRead(0) - ZeroVal);
         
          Serial.print("Raw AnalogValue = ");
          Serial.println(analogRead(0));
        
          Serial.print ("RawVal-Tare = FinalWt = ");
          Serial.println(FinalWt);

          sensorValue = FinalWt / 4.26;(analogInPin);

          sensorValue = sensorValue / 16;

        
          Serial.print("(FinalWt/4.26) / 16 = weight = " ); 
         
          Serial.print(sensorValue);
              
          Serial.print(" lbs/oz");

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to e14 Contributor

    Hi Mark,

     

    The monitor screen looks like this,

     

    Gordon

     

     

    Initializing Card

    Card Ready

     

    Tare Value = 107

    Raw Analog Value = 321

    Raw Val – Tare = Final Wt = 214

    (Final Wt / 4.26) / 16 = Weight = 3.14 lbs/oz

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to e14 Contributor

    Hello J,Gordon.

     

    You may want to a to put a little more polish on that output especially if other people are going to use the application.

     

    (Final Wt / 4.26) / 16 = Weight = 3.14 lbs/oz

     

    may possibly be of more value if the output displayed:

     

    (Final Wt / 4.26) / 16 = Weight = 3.14 lbs =  3 lbs  2.24 oz

     

    Just a thought.  Congrats on taming your Arduino.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube