Nicla battery level

Hello,

I am a bit further with my Nicla Sense ME board, and have written a sketch that will let me read the sensors and send the data to my desktop PC over BLE. The idea is to eventually carry the Nicla on a 3D printed wristwatch and record the IMU data in real time and record on my pc. The thing is, I have no idea how to measure the battery level on this board.

I have built a JST connector for the LiPo battery and connected it that way, so I don't see a way how to get the information of the voltage?

The other option is to just connect the other VBAT pin to an analog input pin, and measure is that way, but that seems a bit weird, if there is actually a way to measure the Pin directly. I have searched all the *.h files I could find for the nicla, and didn't find anything relating to battery voltage, am I missing something?

Thanks in advance.

The battery controller (BQ25120) is connected to I2C0. According to the datasheet you should get a not so detailed reading of the voltage in register 0x0A.

I see, great tip!

I found the BQ25120.h file:

#ifndef BQ25120A_h
#define BQ25120A_h

#define BQ25120A_ADDRESS           0x6A

// Regsiter Map
// https://www.ti.com/lit/ds/symlink/bq25120a.pdf?ts=1610608851953&ref_url=https%253A%252F%252Fwww.startpage.com%252F
#define BQ25120A_STATUS             0x00
#define BQ25120A_FAULTS             0x01
#define BQ25120A_TS_CONTROL         0x02
#define BQ25120A_FAST_CHG           0x03
#define BQ25120A_TERMINATION_CURR   0x04
#define BQ25120A_BATTERY_CTRL       0x05
#define BQ25120A_SYS_VOUT_CTRL      0x06
#define BQ25120A_LDO_CTRL           0x07
#define BQ25120A_PUSH_BUTT_CTRL     0x08
#define BQ25120A_ILIM_UVLO_CTRL     0x09
#define BQ25120A_BATT_MON           0x0A
#define BQ25120A_VIN_DPM            0x0B

#include "Wire.h"

class BQ25120A
{
  public:
  BQ25120A() {};

  uint8_t getStatus();
  void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
  uint8_t readByte(uint8_t address, uint8_t subAddress);

};

#endif

But I still have some trouble reading the register. I simply tried using this:

int value = BQ25120A().getStatus()

Maybe my programming skills are rusty, or I need to use some functions from wire.h to properly communicate over I2C?

I get the following error through the serial port:

++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x0, Parameter error
Location: 0x1AA47
Error Value: 0x0
Current Thread: main Id: 0x20003D54 Entry: 0x1A973 StackSize: 0xC00 StackMem: 0x20003130 SP: 0x2000FF44 
For more info, visit: https://mbed.com/s/error?error=0x80010133&tgt=NICLA
-- MbedOS Error Info --

I didn't even try to access the bits, and I wouldn't even know what subAddress means?

Edit: seems like I just forgot nicla::begin() in setup(). The error doesn't happen now, but I get a reading of 129 without a connected battery, and 65 with. No idea what that means, but it"s a start!

Edit2: I guess it is a simple way to read all addresses at the same time, and 65 is probably 01000001, however, if I am reading this correctly in the datasheet, this will only tell me the battery percentage above 60%, correct?

Edit3: yeah, should read the datasheet more, I kinda get it now.
In the end I used:
uint8_t value = BQ25120A().readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON );

While the battery is full, I get 108, or 1101100, which according to the datasheet is a 94% of VBATREG:

Looking at this table in the datasheet, that would mean a full battery and probably my VBATREG is 4.35V ( I measure only 4V, but otherwise I would have gotten more than 94% for a full battery, right?)

Looking at this, I would be at almost an empty battery (which is the only thing I need) when I have a VBMON of <=82%, so 1001000 , or decimal 72.

Please tell me if this is correct, and I hope this helps other people trying to do this.

Last edit: even though it works, it seems like reading out the register stops the battery from working, kinda... if I try to use the Nicla only with the battery, it doesn't do anything if I read the register. Even my simple green LED doesn't work. However, if I start the Nicla with the USB cable connected, and then disconnect it, the program on it doesn't work, but the green LED stays lit, so I guess the battery works, only the program just stops. If someone has a tip, maybe I need to do something else after I read the status of the register?

I was able to read all the registers as explained above, and the values make sense. I have also charged the battery over night and the reading I get from the 0x0A bit is actually 98%, so I can assume that my VBATREG ist actually 4.2V, whatever this means.

However, there is still a huge issue, when I read the registers, any register from the BQ25120A, the board just stops, but only if it's powered by the battery.

So in short, I can read the values while the USB is connected, and the program does it's thing. As soon as I read the registers while the board is connected to the battery, everything just stops. The Board is still getting power (the LED is still on), but the software has completely stopped working. I tested this with changing LED colors which should occur after the register read-out, and it doesn't happen while connected to the battery. BLE doesn't work as well obviously.

Any ideas?

A link to the location where you found it would allow us to see the complete library.

That probably returns the value of register 0x00.

My wild guess: the mbed OS of the board does also read some values from the chip (that's probably why the I2C connection is there) and if you do the same that collides.

The code in the NiclaSystem library seems to show that you need to flag your request to the chip using pin p25. Try to use the same process in your code.

1 Like

I think it should be this

There isn't anything more than I posted up there already. The only information inside, besides the functions, is that in includes wire.h. The wire.h and wire.cpp I can't find on github exactly like I have it installed with my nicla board in arduino, but I can add it here to the post:
Wire.cpp (4.6 KB)
Wire.h (2.6 KB)

Yes, I managed to read out all of the registers and compared to the datasheet. Everything seems to be fine, and as I said, the LED is still on, so I don't think the battery controller is at fault, I must be missing something with mbed OS, as you said.

I tried using nicla::enableCD() and nicla::disableCD(), but it is private. However, using the code in those functions, pinMode(p25, OUTPUT); and digitalWrite(p25, HIGH); before, and LOW after the readout, seems to do the trick for now, the board doesn't get stuck anymore, even battery powerded. My battery is still almost empty now and the board doesn't communicate over BLE while on battery, so I will try later when it's charged a bit, maybe the nicla disables everything when battery is at a certain level.

I will report later, thanks a lot!

Hello,

did you managed to read values from BQ25120A while running on battery?
I have same issue, on external power everything runds well.

In battery the bq25120A becomse unresponsile, I cant read nor write any tegister...

Yes, this is a correct solution.
Let me add more detailed explanation.

When on the battery only, then the big part of bq25120 (including I2C) is disabled (do save power) unless you specifically enable it by

pinMode(p25, OUTPUT);
digitalWrite(p25, HIGH);

freezing is due the glitch/feature od Arduino Wire for Nicla - it waits indefinitely to read from the device (which is disabled).

After your finish I2C communication with bq25120, you can switch p25 back to LOW. The chip will retain all the changes regarding LDO, charging etc. which you made over I2C.

Hi, @anon14675764
Welcome to the forum.

Thanks for that solution, it helps to complete the thread, especially as the cause appears to be little known or un documented.

Tom... :smiley: :+1: :australia: :coffee:

Yes, I am creating battery powered smart sensor, so I have had to learn complete bq25120 programing.

Still I have a little issue with charging current, which should be programmable up to 300mA for fast charging, but I cant get above 100mA.

So still some things to solve :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.