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 2940 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…
  • mcb1
    0 mcb1 over 13 years ago

    Gordon

    Without seeing your code I can't offer the exact code but something along these lines should work.

     

    Detect 'zero' button

    Read analog and store in a variable (ie int ZeroValue = 0; // value to store zero reading)

     

    Whenever you read the load cell, you subtract the ZeroValue from the reading

     

    Detect the 'monitor' button

    display or print the ZeroValue rather than the load cell reading

     

    This will remain until the micro is reset, either by power, reset button or upload.

     

    Good luck

    Mark

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

    Hi Mark,

     

    Thank you for your early feedback, you described it exactly how I would do it in PLC terms, I am new to Arduino so I am struggling with the code structure, I feel your suggestion will work, I will need to trial a few sketches and then import the working code into my current project.

     

    Best wishes,

     

    Gordon

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

    int loadCellPin = 1;

    int tareButtonPin = 8;

    int currentWeight = 0;

    int taredWeight = 0;

     

    void loop() {

      //do some math to convert anaglogRead(loadCellPin) to a weight

      currentWeight = //results of that math;

     

      //check if the tare button is pushed, to see if it needs to store a value.

      if (digitalRead(tareButtonPin) == HIGH) {

        taredAmt = anaglogRead(loadCellPin);

      }

     

      //do the math to convert taredWeight from 0-1023 to a weight

     

      //i assume you'll have some sort of lcd code setup

      lcd.print("Weight: ");

      lcd.print(currentWeight - taredWeight);

    }

     

    This is what you need on a very basic level, but without any specifics I cant help too much more.

    • 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,

     

    Thanks for the assistance, I would like to send my code to you to check it over, I am only a beginner and it has been trial and error so far.

     

    Basically I want to store a weight to an SD card shield which I have setup ok, but the tare function has stumped me!

    I have 2 buttons, button 1 is the Tare/Zero, Button 2 is the 'take weigh sample' button.

     

    I have setup to write to monitor and I can display the Tare weight when button 1 is pressed, when Button 2 is pressed it displays a scaled value in Pounds/Ounces, my problem is I cant get the subtraction of the Tare weight to work.

     

    I would appreciate any help you could give me.

     

    How can I send you the code?

     

    Regards,

     

    Gordon

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • 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
  • 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
>
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