voltage and current sensor integration

Post Reply
scanman
Posts: 74
Joined: Thu Jun 21, 2012 9:26 am
Location: Durban, South Africa
Contact:

voltage and current sensor integration

Post by scanman »

I have searched all the other forum posts and tried numerous settings but cant figure out how to get the on-board voltage and power settings to display correctly (in LCD and in GUI).

I am using an extrenal power board (RCTimer Voltage & Current Sensor 90A (suit MultiWii, APM, etc))

from here: http://rctimer.com/index.php?gOo=goods_ ... oductname=
Image

The board is working well , i can put my voltmeter on it and the current sensor shows 30mV with no motors running , 100mV with motors running in idle, and 300mV full throttle .
Also full battery voltage is 700mV as output by the rctimer voltage divider.

When i connect it to the multiwii Analogue inputs, it gives strange readings in the multiwii gui and LCD , for the current it gives only a few millamps increase on full throttle. please can you help with some settings for:
PLEVELSCALE
PLEVELDIV
PSENSORNULL
PINT2mA

Note: i wrote the following program to check the AnalugueRead values and it gives from 1 to 300 on full power .

scanman
Posts: 74
Joined: Thu Jun 21, 2012 9:26 am
Location: Durban, South Africa
Contact:

Re: voltage and current sensor integration

Post by scanman »

// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogInPin1 = A1; // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A2; // Analog input pin that the potentiometer is attached to
const int analogInPin3 = A3; // Analog input pin that the potentiometer is attached to
//const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int sensorValue1 = 0; // value read from the pot
int sensorValue2 = 0; // value read from the pot
int sensorValue3 = 0; // value read from the pot


void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);

}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("sensor1 = " );
Serial.print(sensorValue1);
Serial.print("sensor2 = " );
Serial.print(sensorValue2);
Serial.print("sensor3 = " );
Serial.println(sensorValue3);


}

TextZombie
Posts: 24
Joined: Mon Nov 14, 2011 2:05 pm
Location: Sheffield, UK
Contact:

Re: voltage and current sensor integration

Post by TextZombie »

I'm trying to get this working too. So far I ended up changing the battery code in MultiWii.ino to get it working. I'm not really all that familiar with this type of coding so forgive me if it's an awful hack; it does seem to work though.

Code: Select all

    #if defined(VBAT)
      static uint8_t vbatTimer = 0;
      static uint8_t ind = 0;
      uint16_t vbatRaw = 0;
      static uint16_t vbatRawArray[8];
      if (! (++vbatTimer % VBATFREQ)) {
        //vbatRawArray[(ind++)%8] = analogRead(V_BATPIN);
        //for (uint8_t i=0;i<8;i++) vbatRaw += vbatRawArray[i];
        //vbat = (vbatRaw*2) / conf.vbatscale; // result is Vbatt in 0.1V steps
        vbatRaw = analogRead(V_BATPIN);
        vbat = (vbatRaw*(VBATSCALE/1024.0))*157; // This is vbatscale (set to supply minus 0.3v) divided by the ACD steps (1024) times (the Vsense scale of 15.7:1 times 10)
      }
    #endif

Then I hijacked the define of VBATSCALE in config.h to be my FC boards input voltage minus 0.3v (my multimeter shows 5.52 coming out of my BEC)

Code: Select all

    #define VBATSCALE       5.22

This is all just using calculations from the datasheet of the sensor I have (http://www.sparkfun.com/products/9028) and it's quite possible I'm making it way more complicated than needed, but the voltage displayed in MW is now correct when compared with my multimeter.

I've not tried to do the power meter side yet.

P

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: voltage and current sensor integration

Post by copterrichie »

I also had issues with the default VBAT routing getting my enhanced voltage readings to work. I modified the code here: viewtopic.php?f=6&t=2661&start=20#p30829

You may be able to get a better idea from it in getting yours situation to work.

User avatar
Hamburger
Posts: 2578
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: voltage and current sensor integration

Post by Hamburger »

Did you at least try and follow the description in the wiki? Which part there did you not understand or did not work for you?
I take it you use v2.2

scanman
Posts: 74
Joined: Thu Jun 21, 2012 9:26 am
Location: Durban, South Africa
Contact:

Re: voltage and current sensor integration

Post by scanman »

yes thanks hamburger i read the wiki about 6 times, plus all of the posts you have commented on, the wiki is extremely helpful up to a point then i got stuck. there are some definitions and formulas in the wiki which i was not familiar with, and it threw me a bit because i didn't know which values from my system to plug into the calculations, for example in the following formula i dont know what the "100" means and should i be changing that to a value from my system? :
#define PINT2mA 13 // used for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100 )

in this formula if you put it into Excel you get "11326" not "1133", plus i am not sure what the 4.9 is (should i change this to my max Vout of 3.3?),
#define PLEVELDIV 1133 // compute : 1133 = 37 / 4.9 * 10e6 / 24000 * 3600 / 1000

my PSENSORNULL is 0 because under no current the power sensor give 0 volts so thats an easy one.

However, even with 0 volts the power meter just climbs and climbs, even when i push full power - 0.3V - , it climbs as the same rate.

I will take copterrichie's forumla and write a quick arduino formula.

scanman
Posts: 74
Joined: Thu Jun 21, 2012 9:26 am
Location: Durban, South Africa
Contact:

Re: voltage and current sensor integration

Post by scanman »

i think i will do copertrichie's zener diode solution for the battery voltage, it makes sense to only measure the voltage range ( between 12 to 9 volts) that is of interest to us lipo users, that means more fine granularity across the arduino range.

However, i still need to get the power meter at least working within a reasonable range.

Yes hamburger i am working on dev version 2.2 R1391

User avatar
Hamburger
Posts: 2578
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: voltage and current sensor integration

Post by Hamburger »

scanman wrote:yes thanks hamburger i read the wiki about 6 times, plus all of the posts you have commented on, the wiki is extremely helpful up to a point then i got stuck. there are some definitions and formulas in the wiki which i was not familiar with, and it threw me a bit because i didn't know which values from my system to plug into the calculations, for example in the following formula i dont know what the "100" means and should i be changing that to a value from my system? :
#define PINT2mA 13 // used for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100 )

in this formula if you put it into Excel you get "11326" not "1133", plus i am not sure what the 4.9 is (should i change this to my max Vout of 3.3?),
#define PLEVELDIV 1133 // compute : 1133 = 37 / 4.9 * 10e6 / 24000 * 3600 / 1000

my PSENSORNULL is 0 because under no current the power sensor give 0 volts so thats an easy one.

However, even with 0 volts the power meter just climbs and climbs, even when i push full power - 0.3V - , it climbs as the same rate.

I will take copterrichie's forumla and write a quick arduino formula.

fromt the wiki 'promini arduino analog resolution is 4.9mV per unit', so you cannot change that, it is a constant, same goes for 100 (a scaling factor).
your excel math is beyond me - do it on paper then.
good luck.

User avatar
Hamburger
Posts: 2578
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: voltage and current sensor integration

Post by Hamburger »

scanman wrote:Yes hamburger i am working on dev version 2.2 R1391

calculation changed since r1375. Only documentation is in config.h comments.
That is the beauty and downside of using dev versions.

scanman
Posts: 74
Joined: Thu Jun 21, 2012 9:26 am
Location: Durban, South Africa
Contact:

Re: voltage and current sensor integration

Post by scanman »

oh good , so i was correct to ignore the wiki after a certain point! i noticed the "do not change" comments in the powermeter section of config.h so i fiddled with the other settings but couldnt get anything usable, I think the problem is the voltage range coming from that sensor board is too small: instead of going from 0V to 5V like any other voltage divider circuit, it is going from 0v to 0.3V at full power, that's not much resolution available. I might have to amplify the voltage through a transistor until i get a full 0-5V range.
Dammit, no such thing as plug and play, more like swearing and soldering

User avatar
Hamburger
Posts: 2578
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: voltage and current sensor integration

Post by Hamburger »

scanman wrote: I think the problem is the voltage range coming from that sensor board is too small: instead of going from 0V to 5V like any other voltage divider circuit, it is going from 0v to 0.3V at full power, that's not much resolution available.

yes, that is the problem. Especially so as the arduino analog inputs typically are jumpy/jittery - you can expect to loose 1 or 2 (with some boards 3) lowbits of resolution.
Then your 0.3V / (4.9mV/step) = 60 steps or roughly 6 bits; minus 2 lowbits of arduino noise yield 4 usable bits - not really good.

psensorsnull = 0 should be correct for your sensor. You can always try or get another variety of the sensor bob you have, I understand those come in different amperage ranges. Amplifying might be easier...
good luck.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: voltage and current sensor integration

Post by QuadBow »

scanman wrote: think the problem is the voltage range coming from that sensor board is too small


I guess you are running a mini-copter with a maximum consumption of 9A. Thus, the sensor is oversized with a maximum of 90A.
I recommend to use this sensor for bigger copters. (I built it into a X-650 one consuming up to 40A, the results are fine.)
For the littlelun I recommend an ACS712 or ACS714 based sensor with a range of +/-20A range.

sisco211
Posts: 3
Joined: Sun Jan 06, 2013 1:17 pm

Re: voltage and current sensor integration

Post by sisco211 »

Hi,

I bought RCTIMER sensor too. Did you find a solution to increase the reading value. What is your VBAT SCALE?

sisco211
Posts: 3
Joined: Sun Jan 06, 2013 1:17 pm

Re: voltage and current sensor integration

Post by sisco211 »

Hi,

I bought RCTIMER sensor too. Did you find a solution to increase the reading value. What is your VBAT SCALE?

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: voltage and current sensor integration

Post by QuadBow »

sisco211 wrote:I bought RCTIMER sensor too. Did you find a solution to increase the reading value. What is your VBAT SCALE?

Here are my values for the rctimer voltage&current sensor:

Code: Select all

  /********************************************************************/
  /****           battery voltage monitoring                       ****/
  /********************************************************************/
    #define VBAT                // uncomment this line to activate the vbat code
    #define VBATSCALE        80 // (*) change this value if readed Battery voltage is different than real voltage
    #define VBATNOMINAL     126 // 12,6V full battery nominal voltage - only used for lcd.telemetry
    #define VBATLEVEL_WARN1 105 // (*) 10,5V
    #define VBATLEVEL_WARN2  99 // (*) 9.9V
    #define VBATLEVEL_CRIT   94 // (*) 9.4V - critical condition: if vbat ever goes below this value, permanent alarm is triggered
    #define NO_VBAT          16  // (*) Avoid beeping without any battery

  /********************************************************************/
  /****           powermeter (battery capacity monitoring)         ****/
  /********************************************************************/
    //#define POWERMETER_SOFT
    #define POWERMETER_HARD
    /* PLEVELSCALE is the step size you can use to set alarm */
    #define PLEVELSCALE 50 // if you change this value for other granularity, you must search for comments in code to change accordingly
    /* larger PLEVELDIV will get you smaller value for power (mAh equivalent) */
    #define PLEVELDIV 1510 // (*) default for soft - if you lower PLEVELDIV, beware of overrun in uint32 pMeter
    #define PLEVELDIVSOFT PLEVELDIV // for soft always equal to PLEVELDIV; for hard set to 5000
    #define PSENSORNULL 0 // (*) set to analogRead() value for zero current; for I=0A my sensor gives 1/2 Vss; that is approx 2.49Volt;
    #define PINT2mA 13 // (*) for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100
    #define I_MAX   500 // maximal range of current sensor

VBATSCALE is related to the voltage measurement only. But,it depends on the voltage devider (I have a 3 cell battery with 12.6V and a 1:4 devider resulting to 3.15V which suits well to the range of the sensor of 3.3V)
As to the POWERMETER (look at http://www.multiwii.com/wiki/index.php?title=Powermeter ) the range is not used that good since the copter takes 48A maximum current which is roughly half of the 89A provided at 3.3V.
But, in total, I don't see any issue or need to increase the values for my mid-size copter. This assessment might change when you are talking about a small copter like I mentioned before.

teslahed
Posts: 84
Joined: Wed Jun 27, 2012 2:51 pm

Re: voltage and current sensor integration

Post by teslahed »

I've got myself an rctimer current and voltage sensor which i am trying to get working on my FPV hexacopter with the latest dec code of Multiwii ( MultiWii_dev_2013_04_08_r1391 ).

I have the current and voltage sensor wired into my crius all in one Pro such that the current pin goes into Analog PIN 2 and the voltage pin goes into Analog PIN 3 (not tuned yet) and the earth pin just goes to one of the motor earth pins on the board - i assume i need to do that?

edit - just realised my voltage sensor is wired up totally wrong. I need to learn about resistive voltage devider circuits and construct one with the V pin and earth pin and Analog PIN 3 - i think???

So far i am trying to tune the current sensor to give sensible readings via bluetooth on my android phone. I have looked through the code and found the relevent section;

Code: Select all

/********************************************************************/
  /****           powermeter (battery capacity monitoring)         ****/
  /********************************************************************/

    /* enable monitoring of the power consumption from battery (think of mAh)
       allows to set alarm value in GUI or via LCD
      Full description and howto here http://www.multiwii.com/wiki/index.php?title=Powermeter
       Two options:
       1 - hard: - (uses hardware sensor, after configuration gives very good results)
       2 - soft: - (good results +-5% for plush and mystery ESCs @ 2S and 3S, not good with SuperSimple ESC)    */
    //#define POWERMETER_SOFT
    #define POWERMETER_HARD
    /* PLEVELSCALE is the step size you can use to set alarm */
    #define PLEVELSCALE 50 // if you change this value for other granularity, you must search for comments in code to change accordingly
    /* larger PLEVELDIV will get you smaller value for power (mAh equivalent) */
    #define PLEVELDIV 1510 // (*) default for soft - if you lower PLEVELDIV, beware of overrun in uint32 pMeter
    #define PLEVELDIVSOFT 5000 // for soft always equal to PLEVELDIV; for hard set to 5000
    #define PSENSORNULL 0 // (*) set to analogRead() value for zero current; for I=0A my sensor gives 1/2 Vss; that is approx 2.49Volt;
    #define PINT2mA 3  // (*) for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100
    #define I_MAX   500 // maximal range of current sensor


But i am finding this hard to make sense of. There is a wikipedia article;

http://www.multiwii.com/wiki/index.php?title=Powermeter

from which i managed to understand that;

Code: Select all

#define PINT2mA 13 // used for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100 


appears to be the variable to alter for tuning the reading the power meter gives in use. Is this correct?

I haven't played with #define PSENSORNULL 0 because when the quadcopter is resting the current drain shows as zero so i guess that's right?

To tune my current sensor i have been hovering the hexacopter over my bed for approximately 1 min with a full battery, measuring the reported mAh drained with my smartphone over bluetooth, then recharging the battery back to full and using my charger to report the mAh put back in. I realise this may not be perfectly accurate but i think it will be good enough for my purposes.

Does this sound reasonable so far or am i missing anything? I haven't found all much information online regarding this so any help will be greatly appreciated.

The end goal is to get this working with a MinimOSD so that the current and voltage readouts display when i'm flying on video.

http://www.youtube.com/watch?v=0WhrNXmZawA

I'm slowly building up a database of results;

Phone reports mAh,Charger reports mAh, #define PINT2mA value
162, 221,2.98
180,232,2.98
276,237,3.00
?,?,2.99 (looks promising)

If i managed to get my current and voltage sensor working i will come back and write a simplified how to along the lines of;
http://www.multi-rotor.co.uk/index.php?topic=1079.0

teslahed
Posts: 84
Joined: Wed Jun 27, 2012 2:51 pm

Re: voltage and current sensor integration

Post by teslahed »

I have tried precisely following the wikipedia instructions;

http://www.multiwii.com/wiki/index.php?title=Powermeter

* get the sensor and the datasheet of your sensor.
50V/90A = 63.69mV / Volt 36.60mV / Amp
http://dlnmh9ip6v2uc.cloudfront.net/dat ... Output.pdf

* get the battery voltage reading to MWii right first. Yes, even though strictly speaking it is not needed when using a current sensor, do it. Get the battery voltage reading to MWii right first
I have skipped this step because it seems more complex than what i am trying to do and i don't see any relationship between the two tasks.

* attach the sensor output to analog pin 2 (promini). Be prepared to install low path filter set to about 25 Hz to sensor input. That is a low pass RC filter. It is needed if the value seen by MWii is too jumpy.

attached but no low pass filter - the readings aren't jumpy

check your average cycle time or for better accuracy use the new feature to fixate the cycle time to a given value.
used 4000uS average

#define POWERMETER_HARD
Done

#define PINT2mA 13 // used for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100
so 4.9 / 36.6 * 100 = 13.388

#define PLEVELDIV 1133 // compute : 1133 = 37 / 4.9 * 10e6 / 24000 * 3600 / 1000
1120.408

define PSENSORNULL 510 // to start with - what the arduino analog input sees for zero current; depends on sensor. Find the output voltage for zero current in the datasheet, then convert it to arduino value [0Volt ; 5Volt] -> [0 ; 1023]
the counter goes up very slowly until i start the motors - i am assuming the consumption measured at idle is about right for now.

I have set these variables in the code and flown for 1 min above my bed using a full 3 cell 1800mAh battery so that afterwards i can recharge the battery and see how many mAh go back in.

The mAh used is given as '1559' on my bluetooth smartphone.

The battery was charged back to full with 210 mAh

There seems to be no relationship at all between these two values.

Can anyone explain to me what i'm doing wrong here?

I have tried again with a better cycle time value - 3000 instead of 4000

I still get '1219' on my phone and 215mAh put back in - so the values still make no sense?

teslahed
Posts: 84
Joined: Wed Jun 27, 2012 2:51 pm

Re: voltage and current sensor integration

Post by teslahed »

QuadBow wrote:Here are my values for the rctimer voltage&current sensor:


I have tried using your values in my code and when i do i get;

Phone says - used '1135'

Charger says - battery requires 210mAh to recharge

would you have any idea why these values are working for you and not for me? thanks!

teslahed
Posts: 84
Joined: Wed Jun 27, 2012 2:51 pm

Re: voltage and current sensor integration

Post by teslahed »

Hamburger wrote:
scanman wrote:Yes hamburger i am working on dev version 2.2 R1391

calculation changed since r1375. Only documentation is in config.h comments.
That is the beauty and downside of using dev versions.


Just spotted this so i have tried again using 2.2 and QuadBow's settings as his quad is a similar spec to mine;

Code: Select all

/********************************************************************/
  /****           powermeter (battery capacity monitoring)         ****/
  /********************************************************************/
    //#define POWERMETER_SOFT
    #define POWERMETER_HARD
    /* PLEVELSCALE is the step size you can use to set alarm */
    #define PLEVELSCALE 50 // if you change this value for other granularity, you must search for comments in code to change accordingly
    /* larger PLEVELDIV will get you smaller value for power (mAh equivalent) */
    #define PLEVELDIV 1510 // (*) default for soft - if you lower PLEVELDIV, beware of overrun in uint32 pMeter
    #define PLEVELDIVSOFT PLEVELDIV // for soft always equal to PLEVELDIV; for hard set to 5000
    #define PSENSORNULL 0 // (*) set to analogRead() value for zero current; for I=0A my sensor gives 1/2 Vss; that is approx 2.49Volt;
    #define PINT2mA 13 // (*) for telemtry display: one integer step on arduino analog translates to mA (example 4.9 / 37 * 100
    #define I_MAX   500 // maximal range of current sensor


I'm still getting totally nonsensical values;

My phone reports '1190'

My charger recharges the battery back to full with '210mAh'

I would really appreciate some help with this.

teslahed
Posts: 84
Joined: Wed Jun 27, 2012 2:51 pm

Re: voltage and current sensor integration

Post by teslahed »

Following through the wiki;

'attach the sensor output to analog pin 2 (promini)'

Do i use analog pin 2 for my crius all in one pro - mega 2560 based?

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: voltage and current sensor integration

Post by haydent »

any one want to chat about this, im trying to set mine up too

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: voltage and current sensor integration

Post by haydent »

i now have good accuracy by locking my cycle time to 4000 to match the wiki and then using the other values shown in the wiki

i had a flight that reported used 2360 and the charger put back in 2460 so only 100mah diff which im sure i could tune closer

Post Reply