Display Battery level on LCD

Discussion in 'Embedded Systems and Microcontrollers' started by sarvanan, Oct 6, 2016.

  1. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    Hi members,

    I want to develop a C code on MPLABX ide for PIC16F 8-bit microcontroller which detects battery level of a battery connected to PIC and displays it on LCD screen with 3 levels like, High, medium and Low.

    Any idea on how to do it?

    Thanks & regards,
    Sarvanan.
     
  2. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    Hi Members,

    Here is some more info.

    Its 3 Alkaline batteries of 1.5 volt each(total 4.5 volt). Not rechargeable. When battery voltage goes down by 1.2 volt for each(total 3.6 volt), its considered dead.

    Please let me know how to program in C on mplabx using xc8 to show the battery level in 3 segments high, medium and low on lcd.
    Please let me know how to design it like Do we need ADC programming?

    Thanks & regards,
    Sarvanan.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    You will need more that just ADC programming. What powers the PIC? What gives it a stable reference voltage? Are the test batteries loaded or not? What is the LCD you want to use, alphanumeric or something else?

    The basic device is just the test batteries to an analog pin, but if the voltage is too high you will need some sort of a resistive divider which may draw more power than you want so it may need to be switched in and out.
     
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    The ADC on a PIC doesn't really measure voltage. It measures a ratio between an input voltage and a reference voltage, which can be fed in externally, or which could be the chip's power supply. You haven't said whether you plan to power the processor off the same batteries that it's measuring, but it would be easier if you used an external voltage input to drive the reference, either powering the PIC or not.

    However, there's another approach. Many PIC processors have an extra choice of ADC input, an internal voltage level which is fixed. You could set up the ADC to use its power supply as the ADC reference, and then measure this fixed voltage. As the battery voltage which powers the system drops in voltage, the fixed level will appear to get larger. You could figure out some thresholds to decide whether your battery is at "high", "medium" or "low" voltage.
     
  5. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,684
    2,747
    Extending upon @John P's comment:

    Use Vdd (battery supply) as Vref. Use a fixed reference (say, 1.225V bandgap) as one of your analog inputs.

    The actual battery voltage can be computed as Vbat = (Vref * 1024)/counts, assuming a 10 bit converter.
     
  6. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    Hi Ernie, John P, Joeyd999,

    I am trying to understand the suggestions given by you all and work accordingly.

    Joeyd999 First question, what is the count given in your equation to calculate Vbat?

    In my case, Battery used in device is max 4.5 volt. PIC16 used as controller needs max voltage of 3.6 Volt.

    Now one LCD is connected to this PIC and I want to show the main battery level from 3.6-3.9 as low, 3.9-4.2 as mid and 4.2-4.5 as high on LCD in terms of 1 or 2 or 3 horizontal bars. ADC is 10 bit. LCD shows horizontal bars.

    I think now my question is more clear. Please help how to write PIC 'C' code using mplabx xc8 to achieve this.

    Thanks & regards,
    Sarvanan.
     
  7. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,684
    2,747
    Oh. So you're not driving the MCU directly from the battery. Ignore my comment, then.

    If you are using a regulator, it should be sufficient to measure the battery voltage using Vdd (from the regulator) as reference. The A/D reading will be proportional to battery voltage.

    You will require a voltage divider across the batteries, the output of which attached to one of your analog inputs. This voltage divider will consume battery power, so it is advisable to use a MOSFET switch to disconnect it when not in use.
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    Fairly easy:

    Code (Text):
    1.  
    2.   battery_level = read_battery_level();
    3.   if (battery_level > BATTERY_MEDIUM) lcd_display("Battery level is high");
    4.   else if (battery_level < BATTERY_LOW) lcd_display("Battery level is low");
    5.   else lcd_display("Battery level is medium");
    6.  
    Implement each for your design and you are done.
     
  9. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    Thanks dannyf and Joeyd999.
     
Loading...