[Tinyos-help] Mica2: Reading Battery voltage

Michael Schippling schip at santafe.edu
Fri Apr 23 15:34:26 PDT 2010


I beg to differ....hardwiring will do sort of what you think you
want, but it won't do what you actually want...

I would be very surprised if you got any reading other than 0x3ff
since VCC is also used as the Vref on the mica2. If you find
the schematic in the Users Guide it shows a very simple but
nearly impossible to understand circuit for battery monitoring.
That circuit is turned on by toggling PortA pin5 ON. Once that
is done you should get a reading from ADC7 that is inversely
proportional to the battery voltage. I think all the mica2
Voltage components handle the switch on/off and attempt to
do some kind of reading to milli-volt conversion.

I believe I posted an explanation of this long ago...wow! here:
http://mail.millennium.berkeley.edu/pipermail/tinyos-help/2005-December/013591.html

MS


Debasmit Banerjee wrote:
> Hi,
> 
> I actually figured out the problem. I didn't see this mail of yours, so
> sorry for replying so late! The problem was that I was not aware that you
> have to connect the ADC 7 pin with VCC to get correct readings in the MICA2
> mote. It's a hard connection that you need to make with a wire! Earlier, I
> thought that the connection is already there.
> 
> Anyways, the problem is solved now and I am getting correct readings - raw
> values in the order of 450+!
> 
> Thanks a lot for your help!
> 
> - Debasmit
> 
> -----Original Message-----
> From: Michael Schippling [mailto:schip at santafe.edu] 
> Sent: Thursday, April 01, 2010 1:31 PM
> To: Debasmit Banerjee
> Cc: tinyos-help at millennium.berkeley.edu
> Subject: Re: [Tinyos-help] Mica2: Reading Battery voltage
> 
> If you are running your calculation on top of a Voltage component
> that is already doing some sort of calculation you might expect
> to get wrong results, no?
> 
> The Tos1.1.15 version of VoltageM for mica2 does do a calculation
> which looks to be about correct and returns a int16 value in milli-
> volts. However they rely on an explicit cast from int32 to int16
> when returning the value, so bets may be off...
> 
> If you are really getting raw VoltageM values of 158, it would seem
> that your bets should be placed elsewhere. I would drop down a level
> and use the ADC directly with your macros for turning on the sensor,
> just to see what one actually gets from the converter.
> 
> Also there may be some truncation happening someplace as a 158 value
> could just be the low byte of a two byte int...
> 
> MS
> 
> 
> Debasmit Banerjee wrote:
>> Hi,
>>
>> Thanks for your advice! I did that, but the results still look
> unreliable(in
>> the order of 7.46,7.92,etc). I tried reading the raw ADC value being sent
> to
>> the variable datas in my code(in the ADCBATT dataready() event). It seems
>> that the values are of the order 158,159,etc. I am not sure whether these
>> values are correct. I looked in VoltageM and they seem to use the same
>> technique. They have used uint32_t for tempVoltage(probably to avoid
>> floating point calculations) and then they convert that to a 16-bit
> voltage
>> value. I am still not sure where the problem is. Can you tell anything
> from
>> the values?
>>
>> - Debasmit
>>
>>
>> -----Original Message-----
>> From: Michael Schippling [mailto:schip at santafe.edu] 
>> Sent: Tuesday, March 30, 2010 1:41 PM
>> To: Debasmit Banerjee; Tinyos-Help
>> Subject: Re: [Tinyos-help] Mica2: Reading Battery volatge
>>
>> please send questions to the help list in order to keep
>> a record of my mistakes...
>>
>> Your code is pretty good, but you are sending the previous
>> conversion value at the beginning of the next cycle in the
>> Timer.fired(), so at least the first send() is garbage. I
>> would make a task to do the calculation, format, and send
>> and post it from the dataReady()
>>
>> As far as data and types, as I said I would avoid floating
>> point if possible. Either just send the raw ADC data or
>> use fixed point arithmetic. Depending on the platform
>> you are using, the value is 10 or 12 bits and fits nicely
>> in a short int. What you are getting now is probably the
>> integer value of the first two bytes of a four byte float.
>> Would have to look at the IEEE f.p. specs to see what that
>> means.
>>
>> Also some Voltage components attempt to do the calculation
>> for you and I think all of them turn the sensor on and off
>> as you do with your macros. Look at the actual code, if you
>> can find it, to see... If you use the underlying ADC directly
>> you need to do the on/off switching yourself.
>>
>> MS
>>
>>
>> Debasmit Banerjee wrote:
>>> Hi Mr.Schippling,
>>>
>>> Thanks a lot for your reply! I also wanted to know whether the data in
> the
>>> VBatt can be stored in an uint? And also, is my code correct in general -
>>> apart from the part that the floating point conversion is incorrect? What
>> I
>>> mean is- do you think the values that I am getting (7,8,9...) are
> actually
>>> VBatt values, but after conversion, they are producing unreliable
> numbers?
>>> Thanks
>>> Debasmit
>>>
>>> -----Original Message-----
>>> From: Michael Schippling [mailto:schip at santafe.edu] 
>>> Sent: Monday, March 29, 2010 7:04 PM
>>> To: Debasmit Banerjee
>>> Cc: tinyos-help at millennium.berkeley.edu
>>> Subject: Re: [Tinyos-help] Mica2: Reading Battery volatge
>>>
>>> Since itoa stands for "Integer to Alpha" you are correct
>>> that you are using the wrong function. You want an ftoa()
>>> which usually doesn't exist. You might be able to use
>>> sprintf(), but it will drag a lot of code into your
>>> module, if it exists.
>>>
>>> I generally advise avoiding floating point on our
>>> controllers because they have only minimal integer
>>> math support.
>>>
>>> MS
>>>
>>> Debasmit Banerjee wrote:
>>>> Hi,
>>>>
>>>>  
>>>>
>>>> I am fairly new to TinyOS. I want to read the battery voltage for my 
>>>> application. The values I am getting for VBatt vary like 7,8,9,10,11 for
> 
>>>> different batteries I use. I am storing the VBatt in a float. And I am 
>>>> sending the value through the serial port to display in a terminal. I am
> 
>>>> using itoa() to convert the float value to string  which is sent to the 
>>>> serial port using Serial.send() - this might be incorrect because I am 
>>>> using itoa() for a float value. I don't know how else to check the value
> 
>>>> of the voltage, but I don't know how to convert float to string.
>>>>
>>>> I don't know whether the problem might be this or something else.
>>>>
>>>>  
>>>>
>>>> The code I use is this:
>>>>
>>>>  
>>>>
>>>> Sensorboard.h:
>>>>
>>>>  
>>>>
>>>> #define MAKE_BAT_MONITOR_OUTPUT() sbi(DDRA, 5)
>>>>
>>>> #define MAKE_BAT_ADC_INPUT() cbi(DDRF, 7)
>>>>
>>>> #define SET_BAT_MONITOR() sbi(PORTA, 5)
>>>>
>>>> #define CLEAR_BAT_MONITOR() cbi(PORTA, 5)
>>>>
>>>>  
>>>>
>>>>  
>>>>
>>>> Test.nc:
>>>>
>>>>  
>>>>
>>>> TestM.BattControl->Voltage;
>>>>
>>>> TestM.ADCBATT ->Voltage;
>>>>
>>>>  
>>>>
>>>>  
>>>>
>>>>  
>>>>
>>>> TestM.nc:
>>>>
>>>>  
>>>>
>>>> uses
>>>>
>>>> {
>>>>
>>>>                 interface ADC as ADCBATT;
>>>>
>>>>                 interface StdControl as BattControl;
>>>>
>>>> }
>>>>
>>>>  
>>>>
>>>> Implementation
>>>>
>>>> {
>>>>
>>>>                 float vBatt;
>>>>
>>>>                
>>>>
>>>>                 command result_t StdControl.init()
>>>>
>>>>                 {
>>>>
>>>>                
>>>>
>>>>                     call BattControl.init();
>>>>
>>>>                    return SUCCESS;
>>>>
>>>>                 }
>>>>
>>>>  
>>>>
>>>>                 command result_t StdControl.start()
>>>>
>>>>                 {
>>>>
>>>>                    call BattControl.start();
>>>>
>>>>                    return SUCCESS;
>>>>
>>>>                 }
>>>>
>>>>                
>>>>
>>>>                 command result_t StdControl.stop()
>>>>
>>>>                 {
>>>>
>>>>                   call BattControl.stop();
>>>>
>>>>                   return SUCCESS;
>>>>
>>>>                 }
>>>>
>>>>  
>>>>
>>>>                 async event result_t ADCBATT.dataReady(uint16_t datas)
>>>>
>>>>                 {
>>>>
>>>>                                 vBatt = 1.223*1024/datas;
>>>>
>>>>                                
>>>>
>>>>                                 return SUCCESS;
>>>>
>>>>                 }
>>>>
>>>>  
>>>>
>>>>                 event result_t MyTimer.fired()
>>>>
>>>>                 {
>>>>
>>>>                                 SET_BAT_MONITOR();
>>>>
>>>>                                 call ADCBATT.getData();
>>>>
>>>>                                 CLEAR_BAT_MONITOR();
>>>>
>>>>                                
>>>>
>>>> itoa(vBatt, temp, 10);
>>>>
>>>>                                 call Serial.Send(temp, strlen(temp));
>>>>
>>>>  
>>>>
>>>> }
>>>>
>>>>  
>>>>
>>>> }
>>>>
>>>>
>>>> ------------------------------------------------------------------------
>>>>
>>>> _______________________________________________
>>>> Tinyos-help mailing list
>>>> Tinyos-help at millennium.berkeley.edu
>>>> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
> 


More information about the Tinyos-help mailing list