Introduction: Battery Monitor - LinkIt One Code Bite 1

This is the first of what may be many, or few, Code Bites. Quickly, Code Bites are going to be small plug-and-play code segments for various platforms that are intended to add quick and easy functionality to your projects. Typically they will be functions I wrote as part of a larger sketch that I think would be useful for a wide range of projects. If that's confusing just get through this one and it will probably make more sense.

This time I'm working with the LinkIt One (LIO) from Mediatek, which was kindly provided by Instructables and Mediatek. One of the neat features of the LIO is the included rechargeable battery. While working on a bigger project I needed a way to check the batteries status without loading a new sketch each time. Luckily another feature of the LIO is the built in Grove kit compatibility. Using these two things we're going to create a function for checking the battery charge level and displaying it on the Grove RGB LCD screen that can be integrated into almost any project.

Step 1: Components

The minimum components needed are:

  • LinkIt One - $60, Seeed Studio - An all in one dev board aimed for Internet of Things or wearable projects.
    • Rechargable Battery - Included - A lithium-ion cell designed with a plug for the LIO board.
  • Grove RGB Backlight LCD (16x2) - $14, Seeed Studio - This is also included in several of the Grove starter kits. Technically you can use any screen the LIO will interface with, which should be most of them, but you will have to substitute that screens libraries and code syntax.

That's everything you will need besides the obvious connection wires, usb cord, computer, etc. that go along with any microcontroller project.

Step 2: Libraries

If you already use the LinkIt One and Grove LCD then you should be set for this step, feel free to move on. I'm also going to assume you've followed the LIO's setup guide, which can be found HERE if you haven't, and have it ready to go with Arduino.

That just leaves the LCD's library. Start by downloading the .zip HERE. Then simply import it using the Arduino IDE's library importer. Under Sketch..., Include Library... slect Add .ZIP Library... Browse to the location you downloaded the file and select it. As easy as that, you're ready to go and can use the included functions by adding #include "rgb_lcd.h" to your list of libraries at the top of your sketch.

Step 3: The Bite

I should point out again that you can upload and run the attached code by itself and it will just constantly loop, displaying the current battery level, but that is not the intended usage. Instead it's parts should be copied into a larger sketch, adding it's functionality to a larger project.

There are four sections to the code: what I'll call Initializations (the stuff at the top, outside of any function), the Setup() function, the Loop() function, and the user function that is actually running. You will be including something in all four sections for this code. Lets start at the top (having the code open will make it easier to follow along here. Some browsers can open the .ino as text in a new window or tab, FYI).

Initializations

There are three libraries at work here. #include<Wire.h> is a standard Arduino library. #include<LBattery.h> is from the LIO's library set and includes the battery monitoring functionality. #include "rgb_lcd.h" is the Grove LCD's library that we just installed.

Finally the LCD must be initialized with rgb_lcd lcd; and then you're ready to move to...

void setup()

Only one entry here: lcd.begin(16, 2);. This sets the size of the LCD in columns then rows and should go somewhere near the top of the block, but it doesn't matter too much.

void loop()

This is vaguest section, for lack of a better word. My code only has the function call batteryLevel();. Placed alone in the loop function it just gets called in an infinite loop but when implemented in a larger sketch it will probably be called as the result of some input. It doesn't even have to be in the loop function, can call it in your own user-defined functions. Basically, anytime you want to display the battery charge level, call this function.

void batteryLevel()

This is the actual workhorse of the sketch, it's where everything is actually done.

First there's some housekeeping: setting a holding variable, prepping the the display, and taking the reading from the battery.

Once that's done it switches based on the four possible cases for the battery reading: 100, 66, 33, or 0. Each case sets its own back light color (green, yellow, orange, red, respectively) and displays the value on the bottom line.

Finally it adds "Charging" if the board is plugged in and a for loop fills the remaining open spaces on the bottom row with dots and the function returns.


If you are already using this LCD in a sketch then obviously you don't need to double up the library and other standard items and if you're using a different screen then it should go without saying you will have to put in a little work substituting the appropriate setup and syntax throughout.

Step 4: Implament

With the code in hand the final step is up to you! Tweak, massage, or rebuild as you see fit and then plug it in and off you go.

I'd love to see the results of any implementations of this function. I might even have an extra Pro membership or two lying around. *wink*

Make It Glow! Contest

Participated in the
Make It Glow! Contest

Epilog Contest VII

Participated in the
Epilog Contest VII