Battery monitoring using an ATTiny25/45/85

Various user-contributed guides for software-related things
Post Reply
User avatar
Robbi_Blechdose
Posts: 2
Joined: Sun Feb 11, 2018 10:34 am
Been thanked: 3 times

Battery monitoring using an ATTiny25/45/85

Post by Robbi_Blechdose » Wed Feb 14, 2018 1:17 am

A while ago, I added battery monitoring via an ATTiny to my pi, and this guide is for others who want to do the same.
For this, you do not need an AIO board of any kind.
Required parts are:
- Your build with at least a pi zero, lipo battery and a charger/booster
- An ATTiny25/45/85
- two resistors, size should be between 150K and 1M Ohm each (and they should be the same size)
- an IC socket if you want your tiny re-flashable and safe from being fried by your soldering iron
- some wires

First, the hardware part:
You'll need to make sure the Serial/UART interface pins on the pi are free (RX and TX).
Image
The ATTiny is wired up as following (you should probably solder on the socket and just stick the ATTiny in for easy reflashing):

Tiny
RX -> Pi TX
TX -> Pi RX
VCC -> Pi 3.3V (important, DO NOT USE 5V, RX/TX won't like that)
GND -> Pi GND

Now for the most important part here: The input voltage of the battery is too high for the ATTiny to measure, as it measures relative to its VCC. Batteries can go up to 4V, which means the area from 3.3V-4V is not measurable.
To solve this, we'll use a voltage divider (many thanks to bitbank for this one!).

I'm using pin 3 (Analog Input 3) on the ATTiny:
Image
You'll have to connect that pin as follows:
Tiny GND -> Resistor -> Tiny Pin 3 <- Resistor <- Tiny VCC

Now we can measure the battery voltage on the ATTiny, here's the sketch you can flash onto it:

Code: Select all

#include <SoftwareSerial.h>

char i;

SoftwareSerial mySerial(0, 1); // RX, TX

void setup() 
{
  mySerial.begin(115200);
  delay(100);
}

void loop() 
{
  int sensorValue = analogRead(A3); // read the input on analog pin 0:
  if(mySerial.available())
  {
    if(mySerial.read() != -1)
    {
      mySerial.println(sensorValue);
    }
  }
  delay(10);
}
It uses a software serial since the ATTiny does not have one in hardware.

Once it's connected to your Pi (see above), you can then install the software from here:
https://github.com/HoolyHoo/gbzbatterymonitor
Ignore the part about a hex file, that's for using Helders AIO board. Just run the install script like it says in the readme.
You probably will need to modify the script HHBatteryMonitor.py.
Replace every occurence of "/dev/ttyACM" with "/dev/ttyAMA" (without the quotation marks, of course).
Also, you may have to modify the x and y coordinates for pngview (it was offscreen for mine).
Once you're done, open raspi-config and disable the serial console, but enable the interface. If you want to make sure it's on, check "/boot/config.txt" for the line "enable_uart=1". In case it itsn't there, add it by hand.

Now you just have to reboot and you should have a battery monitor in the top right corner of your screen.

Here's how it looks in my build:
Image
Image
Last edited by Robbi_Blechdose on Thu Feb 15, 2018 5:38 am, edited 1 time in total.

McGyver
Posts: 105
Joined: Tue Jan 31, 2017 11:06 am
Location: Tianjin / China
Has thanked: 9 times
Been thanked: 19 times

Re: Battery monitoring using an ATTiny25/45/85

Post by McGyver » Wed Feb 14, 2018 1:40 am

Wow. Sounds very interresting and easy to follow. I looking forward to order some of the microcontroller.

User avatar
derebbe
Posts: 48
Joined: Wed Sep 28, 2016 12:30 am
Location: Germany
Has thanked: 59 times
Been thanked: 17 times
Contact:

Re: Battery monitoring using an ATTiny25/45/85

Post by derebbe » Wed Feb 14, 2018 2:39 am

This solution won't work together with a display which runs via DPI, right? ...since DPI uses the two needed GPIO (rx/tx) pins for SPI already and a "remapping" is not possible (as far as I could find out)...

User avatar
VeteranGamer
Posts: 1015
Joined: Thu Jan 26, 2017 11:12 am
Location: London, UK
Has thanked: 284 times
Been thanked: 543 times

Re: Battery monitoring using an ATTiny25/45/85

Post by VeteranGamer » Wed Feb 14, 2018 3:00 am

Robbi_Blechdose wrote:
Wed Feb 14, 2018 1:17 am
A while ago, I added battery monitoring via an ATTiny to my pi, and this guide is for others who want to do the same.
For this, you do not need an AIO board of any kind.
Required parts are:
- Your build with at least a pi zero, lipo battery and a charger/booster
- An ATTiny25/45/85
- two resistors, size should be between 150K and 1M Ohm each (and they should be the same size)
- an IC socket if you want your tiny re-flashable and safe from being fried by your soldering iron
- some wires

First, the hardware part:
You'll need to make sure the Serial/UART interface pins on the pi are free (RX and TX).
Image
The ATTiny is wired up as following (you should probably solder on the socket and just stick the ATTiny in for easy reflashing):

Tiny
RX -> Pi TX
TX -> Pi RX
VCC -> Pi 3.3V (important, DO NOT USE 5V, RX/TX won't like that)
GND -> Pi GND

Now for the most important part here: The input voltage of the battery is too high for the ATTiny to measure, as it measures relative to its VCC. Batteries can go up to 4V, which means the area from 3.3V-4V is not measurable.
To solve this, we'll use a voltage divider (many thanks to bitbank for this one!).

I'm using pin 3 (Analog Input 3) on the ATTiny:
Image
You'll have to connect that pin as follows:
Tiny GND -> Resistor -> Tiny Pin 3 <- Resistor <- Tiny VCC

Now we can measure the battery voltage on the ATTiny, here's the sketch you can flash onto it:

Code: Select all

#include <SoftwareSerial.h>

char i;

SoftwareSerial mySerial(0, 1); // RX, TX

void setup() 
{
  mySerial.begin(115200);
  delay(100);
}

void loop() 
{
  int sensorValue = analogRead(A3); // read the input on analog pin 0:
  if(mySerial.available())
  {
    if(mySerial.read() != -1)
    {
      mySerial.println(sensorValue);
    }
  }
  delay(10);
}
It uses a software serial since the ATTiny does not have one in hardware.

Once it's connected to your Pi (see above), you can then install the software from here:
https://github.com/HoolyHoo/gbzbatterymonitor
Ignore the part about a hex file, that's for using Helders AIO board. Just run the install script like it says in the readme.
You probably will need to modify the script HHBatteryMonitor.py.
Replace every occurence of "/dev/ttyACM" with "/dev/ttyAMA" (without the quotation marks, of course).
Also, you may have to modify the x and y coordinates for pngview (it was offscreen for mine).
Once you're done, open raspi-config and disable the serial console, but enable the interface. If you want to make sure it's on, check "/boot/config.txt" for the line "enable_uart=1". In case it itsn't there, add it by hand.

Now you just have to reboot and you should have a battery monitor in the top right corner of your screen.

thanks for the info....

can you show how you actually have it in your build/project (images would help)...


thanks....

User avatar
abrugsch
Posts: 956
Joined: Tue Aug 02, 2016 10:00 am
Has thanked: 355 times
Been thanked: 421 times
Contact:

Re: Battery monitoring using an ATTiny25/45/85

Post by abrugsch » Wed Feb 14, 2018 5:07 am

derebbe wrote:
Wed Feb 14, 2018 2:39 am
This solution won't work together with a display which runs via DPI, right? ...since DPI uses the two needed GPIO (rx/tx) pins for SPI already and a "remapping" is not possible (as far as I could find out)...
it's UART Serial not SPI, but apart from that, yes you are correct, DPI will use the TX/RX pins (GPIO 14/15) in all implementations of DPI:
Image
it doesn't look like the TXD/RXD can be remapped to an alt pin on a standard Pi (a compute module can though, as GPIO 32,33,36 and 37 are broken out)

User avatar
Robbi_Blechdose
Posts: 2
Joined: Sun Feb 11, 2018 10:34 am
Been thanked: 3 times

Re: Battery monitoring using an ATTiny25/45/85

Post by Robbi_Blechdose » Wed Feb 14, 2018 8:21 am

VeteranGamer wrote:
Wed Feb 14, 2018 3:00 am
-snip-

thanks for the info....

can you show how you actually have it in your build/project (images would help)...

thanks....
It'd be nice if you could remove the gigantic quote please. Anyways, I'll add some images of my build, but I'm afraid it won't be much help since my wiring's pretty messy.

User avatar
VeteranGamer
Posts: 1015
Joined: Thu Jan 26, 2017 11:12 am
Location: London, UK
Has thanked: 284 times
Been thanked: 543 times

Re: Battery monitoring using an ATTiny25/45/85

Post by VeteranGamer » Wed Feb 14, 2018 9:01 am

Robbi_Blechdose wrote:
Wed Feb 14, 2018 8:21 am
VeteranGamer wrote:
Wed Feb 14, 2018 3:00 am
-snip-

thanks for the info....

can you show how you actually have it in your build/project (images would help)...

thanks....
It'd be nice if you could remove the gigantic quote please. Anyways, I'll add some images of my build, but I'm afraid it won't be much help since my wiring's pretty messy.
dont be afraid.....

post if you have pics...
(it can still be of help to some to visualise it)


.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest