Monday 18 January 2016

Pocket PiGRRL - Battery Monitor

I recently made myself an Adafruit Pocket PiGRRL and I wanted to modify it so it would warn me when the battery was running low - there is a small red LED but its hidden inside the case!


The plan was to create a program which would sense the battery getting low and put a warning icon on the top left of screen giving me time to shutdown the Pi properly or plug it in.



TLDR - just scroll down to install Grrl Battery Monitor.

I started with the software as I, foolishly, thought this would be the hardest part, the problem with creating an icon is that is has to go over the top of everything regardless of what is on the screen (command prompt, emulators, emulation station, everything) or what hardware was rendering it.

My first plan was that I could use Picamera's overlay function which I knew used the GPU to output directly to the screen, and with a bit of help from Dave Jones who put together a prototype, it was looking good, but while the icon appeared on top of emulators and the command prompt, it didnt write over emulation station.

I came across Low Level Graphics on Raspberry Pi which walks you through writing graphics directly to the Linux framebuffer using C, this was a lot lower level than I hoped to get into but it would definitely write my icon over anything that was on the screen - using this I wrote a program to create an icon on the screen when a GPIO pin was triggered.

Next I needed to be able to read from the power booster when the battery was running low, my original plan was to use the LBO (low battery output) pin, but this proved to be way more difficult than I expected, read this post on Adafruit's forum if your really interested.

I ended up connecting a wire to the low battery warning led (red) on the power booster and using this to switch a transistor which connected a GPIO to ground.


Its been frustrating but I am really pleased with how it worked out - if you want to add the batter monitor to your own Pocket PiGRRL follow the instructions below.

Install Grrl Battery Monitor

You will need a few parts:
  • Some wire
  • 2N3904 NPN transistor
  • 47k resistor
  • Strip board
Note - if you are doing this on a PiGRRL 2 with a Pi 3, be sure to check out Christian's comments about the pin to use and wiring-pi install before starting.

1. Open up your Pi GRRL and connect a small length of wire to the red (low power) led on the power booster.



2. Solder the components to the strip board. including 2 lengths of wire which will connect to GPIO 19 and GND.




3. Flip over your Pi and solder the GPIO and GND wires to the underside of the Pi's GPIO header.


The yellow wire is for my mute / un-mute amp function.

4, Solder the wire from the low power (red) led to the strip board.


5. Stick the strip board to the case in-between the power booster and the amp with a bit of glue and put your PiGRRL back together.

6. Download the program from github.com/martinohanlon/grrl-bat-monitor
cd ~
git clone https://github.com/martinohanlon/grrl-bat-monitor
7. Make the program run at boot by editing /etc/rc.local
sudo nano /etc/rc.local
Scroll down and add the command under '/usr/local/bin/retrogame &' but before 'exit 0':
/home/pi/grrl-bat-monitor/grrl_bat_mon &
8. Reboot and test!

19 comments:

  1. I had trouble running this program on a piGRRL 2 with a Pi 3 and RetroPie 4.0.2 installed. It kept erroring out saying that libwiring.so file was missing or something to the effect. I was able to resolve this by installing WiringPi using a modified set of instructions from http://wiringpi.com/download-and-install. Below are the commands I ran via SSH into the PiGRRL 2. Please note that the PiGRRL unit must be able to access the internet for these steps to work:

    sudo apt-get update
    cd ~
    git clone git://git.drogon.net/wiringPi
    cd wiringPi
    git pull origin
    sudo ./build

    Once the build process completed, I rebooted my PiGRRL 2 and tested and it works. Hopefully this helps someone else. Thanks to Martin for this wonderful solution.

    ReplyDelete
    Replies
    1. Okay so if i want to install an on screen battery monitor without the low battery light, What would the steps be for someone with the pigrrl 2.0 with a RPI3 and the newest version of retropie?
      You could always email me too, i need this monitor badly!! Please help
      (jokerswild1234567@gmail.com)

      Delete
    2. Hello. I had the same "libwiring.so missing" issue. So I followed Christian's steps. Now, this error is gone, but I still can't make this program to run : it always says "Error : cannot open framebuffer device"... Any clue ? Thanks !

      Delete
    3. The framebuffer is /dev/fb1, and is the buffer for the LCD screen. Is it attached? Check /dev to see if it's there. If you running straight from the command line, you probably need to run as root using sudo, try that too.

      Delete
    4. Thanks for your answer ! I forgot to tell you my configuration... I don't own a pigrrl, I'm making a gameboy zero from an original case (but it should be the same). I use a raspberry pi zero, I got display through the composite to a 3.5'' lcd tft.
      I already checked yesterday, I only have fb0 in my /dev directory. And yes, I'm calling the script directly from command line (not by ssh, directly from the pi) using sudo.

      Delete
    5. Ok, in which case you won't have a fb1 which is the framebuffer used by the TFT screen. You should be able to write directly 'main screen' I.e. fb0 by changing the c program and recompiling it, but I have never tried.

      Delete
    6. No, it didn't work. I changed the value, then recompiled. I first tried to launch manually, I had no error. So I added the script call into rc.local, resulting in a freeze of the pi on each boot... (fortunatly, I did a sdcard image before trying it !)

      Delete
    7. Ok, when you say it didnt work, do you mean that the low battery icon wasnt displayed on the screen? The icon only appears if the gpio pin is set to ground, otherwise it just runs in the background checking the pin.

      Its really important when you set the script to run at boot you include the & at the end of the command, otherwise the boot sequence will wait for it to finish, which as it is designed to run all the time it never will.

      Delete
  2. I forgot to add that I needed to modify this setup to use GPIO 9 (PIN 21) instead of GPIO 19 (PIN 35) as shown in the pics above because GPIO 19 is used in the PiGRRL 2 as the right directional button. This program then needs to be slightly modified by editing the grrl_bat_mon.c file and modifying the line that reads:

    int lbo_gpio = 19;

    to read as

    int lbo_gpio = 9;

    After doing this, do a sudo ./compile.sh. Then sudo reboot. The program then works as it should.

    ReplyDelete
    Replies
    1. Thanks for the info on the PiGrrl2 Christian.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. Okay so if i want to install an on screen battery monitor without the low battery light, What would the steps be for someone with the pigrrl 2.0 with a RPI3 and the newest version of retropie?
    You could always email me too, i need this monitor badly!! Please help
    (jokerswild1234567@gmail.com)

    ReplyDelete
    Replies
    1. Im pretty sure the install on a PiGrrl 2 is the same, it has the same PowerBoost 1000 charger board.

      Have a look at the comment above from Christian Haitian, he installed it and just had to make one small change to use a different gpio pin.

      Delete
  4. What power rating for the 47K resistor did you use? Or does it matter?

    ReplyDelete
  5. Hi,

    Thanks for this!

    I'm pretty sure I did everything correct... I get the icon on the screen, but rather than only show when it's low... the icon will flash a few times, then go out for a few seconds, maybe 10 then flash a few times, and repeat like this.

    I just now got it all hooked up... so I haven't ran down the battery yet.

    Is this just how it behaves with a medium battery? PLZ advise.

    ReplyDelete
    Replies
    1. The icon should only appear when the battery low led on the power board is on. If it's not on, and the icon is I would check your wiring / soldering.

      Delete
    2. Ok, so I'm building a PiGRRL Zero Plus w/ Audio - Pin 35 was in use, and Pin 21 is SPI, I think the audio is causing a false positive on this pin (it will show the icon even without wiring it).

      I simply changed to pin 15 (GPIO 22)

      int lbo_gpio = 19;

      to read as

      int lbo_gpio = 22;

      Delete

Note: only a member of this blog may post a comment.