Convert voltage to Neg kg

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Convert voltage to Neg kg

Postby Desdewit » Thu Feb 02, 2012 2:55 pm

Hi guys

I'm using an AD620AN circuit to amplify a 0-10mV signal to 0 - 5V then I use a pic16F877 to convert the 0-5V to Kg.
Is it possible to display everything under 2.5V as negative kg and everything above 2.5V as Positive kg with 2.5V as my zero offset?

If there is a better way of doing this please let me know?

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Convert voltage to Neg kg

Postby Spanish_dude » Sun Feb 05, 2012 10:34 pm

Well, I suppose the algorithm to change your voltage to kg will be the same for the upper 2.5V and lower 2.5V except for the negative part.

If I remember well, the 16F877 has a 10 bit ADC, so this is what I'll do:
- Read analog signal with ADC
- Is the value higher than 512 ? (1024 possibilities / 5V Vcc * 2.5V analog input)
-- Substract the 10 bit ADC value from 1024 -> This should give you a value between 0 and 512 or 511.
-- Convert to kg
- If not higher than 512
-- Convert to kg and multiply by -1

Nicolas

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Mon Feb 06, 2012 10:52 am

Hi Nicolas

Everything is working now except for the neg value.

Will this work with the *1 as -1

Voltage = adc_level*5/(1023/100)*1

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Convert voltage to Neg kg

Postby Spanish_dude » Wed Feb 08, 2012 2:24 am

I think it will be easier to directly convert the ADC value to kg.
I recommend using floats for this so you don't lose too much info when doing the calculation. Anyways, once you have your voltage, you just need to check if it's higher or lower then 2.5V.
If it's higher then do the voltage to kg calculation.
If it's lower, do the voltage to kg calculation and multiply that value by -1.

Code: Select all

int adc_value = ReadADC();

if (adc_value >= 512)
    adc_value = adc_value - 512;

float voltage = adc_value * 5/ max_samples;
float kg = voltage * max_kg / max_voltage;

if (voltage < 2.5)
    kg = kg * -1;

max_voltage is your 2.5V and not Vcc, max_samples is 1024 and max_kg is your max weight.
You can't calculate your kg in one go.

Edit:
What I said isn't 100% correct. If you do like a said, the calculated kg for an adc_value of 0 and 512 will be the same (except for the negative sign).
What you want is at 2.5V being 0kg. A higher voltage (ex. 3V) means a higher weight (ex 1kg) and a lower voltage (ex 2V) means a lower weight (-1kg).

To correct this you'll need to add this:

Code: Select all

if (adc_value >= 512)
    adc_value = adc_value - 512;
else
    adc_value = 511 - adc_value;


An adc value of 0 or an analog voltage of 0V means you're reading a weight of (max_kg * -1). If you do the calculation "kg = 0V * max_kg / max_voltage" you'll get 0kg as answer and that is not correct.
By doing 511-adc_value you'll convert a 0V input to a (approx) 2.5V input. Doing the calculation know will give you "kg = 2.5V * max_kg / 2.5" or "kg = max_kg".
Multiplying this by -1 will give you the correct answer.
I hope it's a bit clear, if you have any questions feel free to ask.

Edit:
Another thing I saw in my little code isn't right.
You're converting you adc value into a 0 to 2.5V voltage so the last if statement, the one that decides whether or not the weight should be multiplied by -1, will always be executed.
To correct this add a variable that will tell you when to multiply by -1.

Code: Select all

int multiply = 0;
int adc_value = ReadADC();

if (adc_value >= 512)
    adc_value = adc_value - 512;
else
{
    adc_value = 511 - adc_value;
    multiply = 1;
}

float voltage = adc_value * 5/ max_samples;
float kg = voltage * max_kg / max_voltage;

if (multiply == 1)
    kg = kg * -1;


That should do it, I think :mrgreen: .

Nicolas

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sat Feb 11, 2012 11:30 am

Hi Nicolas

Sorry for not getting back sooner.
I did like you said but when I load the program to the pic the display only shows 0.000.
Is it possible that a pic 16f876 might not be able to do this kind of maths?

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Sat Feb 11, 2012 1:08 pm

Hi Desdewit,
I also have an idea.
Would it be possible for you to post your flowchart, I can then modify it for you.

Have you got FlowcodeV3 or flowcodeV4 but can't post on V4 section?

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sat Feb 11, 2012 2:55 pm

Hi Martin

Yes my problem is the computer I'm working from is from a client of mine and they don't want me to use there registaration details on any forums.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Sat Feb 11, 2012 4:25 pm

Desdewit wrote:Hi Martin

Yes my problem is the computer I'm working from is from a client of mine and they don't want me to use there registaration details on any forums.


I Guess from this, and since you have not posted a flowchart that you are using V4?

So I have created a V4 flowchart that will go from -2.500Kg to 2.500Kg depending on analogue i/o of 0 to 5V

Calculations are separate so you can see how I have achieved it.

Since it was created for a 16F88, you will need to alter settings for your chosen target device.

Martin
Attachments
Weight1.fcf
(8 KiB) Downloaded 290 times
Last edited by medelec35 on Sat Feb 11, 2012 6:02 pm, edited 1 time in total.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sat Feb 11, 2012 5:59 pm

Hi Martin

Thanks for your time, The code are working really well.
I was thinking of changing "Read_W = fdiv(Read_W,1000.0)" to "Read_W = fdiv(Read_W,500.0)" to read 0-5V
Are there any other way the code can be changed for different weights like when I'm calibrating a scale.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Sat Feb 11, 2012 6:28 pm

Hi Desdewit,That's OK.
Desdewit wrote:I was thinking of changing "Read_W = fdiv(Read_W,1000.0)" to "Read_W = fdiv(Read_W,500.0)" to read 0-5V

You don't really need the calculations to do that, and besides by changing to Read_W = fdiv(Read_W,500.0) that would make the reading -5 to +5 not 0 to 5.
Easiest way to make 0 to 5V is either in the ADC component select ReadAsVoltage with a float variable which will then assigned with a value of 0.00 to 5.00
or if you dont wan tto use floats due to space they take up you can use integers just like :
viewtopic.php?f=29&t=7699&p=20326#p20326

Desdewit wrote:Are there any other way the code can be changed for different weights like when I'm calibrating a scale.

Only way I can think off the top of my head is have a offset variable so when the sales are calibrated, the offset is loaded with the difference between the reading and the actual weight.
When you also take into account tare that should 0 the scales you are talking more complex algorithm.
I'm sure people on this forum who are much cleverer than I am will find the answer far easier than I could.
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sat Feb 11, 2012 6:53 pm

Hi Martin

I think I can make it work, but just one last question.
Will the scale be more accurate using 0 - 5V for 0 - fullscale kg or 2.5V - 5V for 0 - fullscale kg?

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Convert voltage to Neg kg

Postby Spanish_dude » Mon Feb 13, 2012 3:32 am

To calibrate the weight, just use like a pushbutton to store the current voltage read by the adc. Then subtract that value from your next adc readings.
Let's say you have 1kg on your weight but you want it to be 0kg, press the button and it will store the voltage of the 1kg and subtract that from the actual adc readings so now you should be reading 0kg.

Your scale will be more accurate with 0 to 5V and I'm not sure you would be able to negative with 2.5 to 5V one.
Anyways, this is just a bit of tweaking the code so it won't be too difficult for you to try out.

Nicolas

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Mon Feb 13, 2012 3:41 pm

Hi Nicolas

The scale is working at the moment with a zero & calibration button to capture the full load AtoD and the zero AtoD values.
Then I have a range from 102 - 921 AtoD counts to work with as 102=0kg and 921="load cell full load capacity in kg"
My problem is how I make the software work out the new ratio after calibration if it was calibrated for 50-1013 counts
50 = 0kg and 1013 ="load cell full load capacity in kg"
Doing the zero is not that difficult but doing the full scale is what gets me. :oops:

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sat Feb 18, 2012 7:25 am

Hi Guys

How can I make the AtoD read 1-4V as 0 - 1023 counts instead of 0-5V without using the Vref+ and Vref-.
Or can you set the Vref+ and Vref- with the software.

kersing
Valued Contributor
Valued Contributor
Posts: 1878
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 534 times
Been thanked: 1043 times
Contact:

Re: Convert voltage to Neg kg

Postby kersing » Sat Feb 18, 2012 11:55 am

Desdewit wrote:How can I make the AtoD read 1-4V as 0 - 1023 counts instead of 0-5V without using the Vref+ and Vref-.
Or can you set the Vref+ and Vref- with the software.


If you do not supply a reference voltage to Vref- and Vref+ the voltages used are the voltages used to power the chip (usually 0V and 5V). The 1024 steps will be spread over that range. Vref-/+ can not be set by software, these are hardware input pins.

If your sensor only provides voltages between 0V and 4V you could consider a simple amplifier between the sensor and the PIC. This amplifier should amplify the values 0V - 4V linearly to 0V - 5V. Some googling should provide examples (search for non inverting op amp amplifier)

Another way to gain more precision would be to use a part with 12 bit A/D. That way the 0-5V range would have 4096 steps, so a 0-4V has 3276 steps. Parts matching this specification can easily be found using the microchip product selector.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sat Feb 18, 2012 1:28 pm

Hi Kersing
Thanks to everybody that helped me so far with my project
At the moment I'm using an AD620AN amplifier to amplify 0 to 30mV signal from a load cell to 0-5V & two 10k pots on Vref + & - to adjust zero & span.
The reason I asked was to try and set the zero & span with software instead of pots so that I can tare out any offset and also read a little “+-1mV” negative if I need to.
So far the scale are working pretty well except for the part of the load cell with the -130µV offset that I cannot display.
If I take the weights to 0 & press the call button it zero’s the instrument & if I take the weights to 500kg and press the call button it sets the full scale. The only problem is that if I return the scale to zero and the load cell goes a little negative
“-1mV” the scale only shows 0 and not the negative value, So I thought of putting zero at 1V so that I can use the 0-1V to display the negative zero Return value.

kersing
Valued Contributor
Valued Contributor
Posts: 1878
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 534 times
Been thanked: 1043 times
Contact:

Re: Convert voltage to Neg kg

Postby kersing » Sat Feb 18, 2012 5:10 pm

Desdewit wrote:The only problem is that if I return the scale to zero and the load cell goes a little negative
“-1mV” the scale only shows 0 and not the negative value, So I thought of putting zero at 1V so that I can use the 0-1V to display the negative zero Return value.


So your zero weight is at 1V and max weight is at 4V. And you are using 0V to 1V for negative values. That means the full scale is still 0V to 5V, right? So the total resolution of the ADC (10 bit = 1024 steps) still needs to be spread over 0-5V.

If you want to set the span with software you could look into using D/A components to generate Vref- and Vref+. Just keep in mind the device has limits on the allowable values for Vref. From the datasheet:

Code: Select all

Sym       Characteristic                     Min              Typ        Max           Units                                         
Vref     Reference Voltage (Vref+ – Vref-)   2.0               —        Vdd + 0.3        V
Vref+    Reference Voltage High             AVdd – 2.5V                 AVdd + 0.3V      V
Vref-    Reference Voltage Low              AVss – 0.3V                 Vref+ – 2.0V     V
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Sat Feb 18, 2012 7:31 pm

Desdewit wrote:How can I make the AtoD read 1-4V as 0 - 1023 counts instead of 0-5V without using the Vref+ and Vref-.


Hi
here is float formula to do that:

Code: Select all

Read_W = fmul(Read_W,1.668841762)
Read_W = fsub(Read_W,342.1125612)



if would be easier for you, If you state 0 ADC = -xx.xxKg
and 1023 = xx.xxKg then I could create a new formula for you to do a direct conversion....hopefully :P

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Sun Feb 19, 2012 12:15 pm

Hi Martin

If you state 0 ADC = -xx.xxKg
and 1023 = xx.xxKg then I could create a new formula for you to do a direct conversion


My 0 ADC = -25kg
and 1023 = 525kg

The scale will be going up in 25kg increments up to 500kg stopping at each 25kg weight to take a reading from a unit under test.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Sun Feb 19, 2012 2:11 pm

Desdewit wrote:My 0 ADC = -25kg
and 1023 = 525kg
The scale will be going up in 25kg increments up to 500kg stopping at each 25kg weight to take a reading from a unit under test.

Hi Desdewit,
Since you have only use whole numbers, I have converted the float result into an integer.
At least integers are easier to apply calibration offset to.
Result will be round down, and not up since rounded up will take up much more ROM in the chip.
This is untested on hardware so can't guarantee it will work without any alterations.
Attachments
Weight3.fcf
(7 KiB) Downloaded 222 times
These users thanked the author medelec35 for the post:
Desdewit (Sun Feb 19, 2012 5:49 pm)
Rating: 5.26%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Mon Feb 20, 2012 7:22 pm

Hi Martin

I've uploaded a sample of the scale I'm trying to build.
This is a short description of how the scale should work.

1: Press start and then calibrate button within 2 sec from start up the scale will go into calibration mode.
2: Set ADC value were the instrument should read 0kg. Say "10%" and press Zero button (Zero Value is then captured)
3: Set ADC value were the instrument should read 500kg. Say "90% " and press Span button (Span Value is then captured)
4: Press calibrate to exit calibration and watch the display, it should read 500kg when at 90% or whatever it was set to and 0 when at 10% but for some reason the scale subtract the zero from the full scale. :?
5: There are also an unwanted decimal point when the scale read 0kg to 100kg
Please see if you can see what I'm doing wrong? :oops:
Attachments
Weighing Scale temp.fcf
(20.26 KiB) Downloaded 229 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Mon Feb 20, 2012 11:06 pm

Try this version.
I have hopefully corrected issues for you.

I must say after looking at your flowchart, you was so close, and it is a very clever routine!

You could have the word Calibration Mode during calibration routine etc
I would recommend an indication on LCD that calibration, zero and span keys detected, since display stays blank.

Martin
Attachments
Weighing Scale V3.fcf
(20.27 KiB) Downloaded 246 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Tue Feb 21, 2012 11:33 am

Hi Martin

Are there any way to stop the scale from subtracting the zero from the full load?
If I set my zero at 10% and my 500kg at 90% the scale shows 445kg instead of 500kg because of the zero subtraction.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8476
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2433 times
Been thanked: 3487 times
Contact:

Re: Convert voltage to Neg kg

Postby medelec35 » Tue Feb 21, 2012 12:07 pm

Hi Desdewit,

With the flowchart I posted, if zeroed at 10%, then span is at 90% then
Display will show 0 at 10% = 102 = 0.5V
Display will show 500 at 90% = 921 = 4.5V
(well it does that on Flowcode simulator)
Scales 10 & 90.png
Scales 10 & 90.png (55.75 KiB) Viewed 11557 times

Which is what I thought you wanted?

Are you saying that hardware is showing different results to simulator?

Or I have just got the idea wrong?

Martin
These users thanked the author medelec35 for the post (total 2):
Benj (Tue Feb 21, 2012 12:27 pm) • Desdewit (Tue Feb 21, 2012 12:51 pm)
Rating: 10.53%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Re: Convert voltage to Neg kg

Postby Desdewit » Tue Feb 21, 2012 1:00 pm

Hi Martin

Sorry for the inconvenience.
I had an IO error on my side “Idiot Operator", I've loaded the wrong software version. :oops:
The program is working just fine.
At last now I can buy flowcode 5 :D