Pavel
Published

Onion Omega2: Battery Level and Current date Indicator

This project will allow you to display a battery level and current date and time on the OLED expansion.

IntermediateFull instructions provided15 minutes2,350
Onion Omega2: Battery Level and Current date Indicator

Things used in this project

Hardware components

Onion Corporation Power Dock 2
×1
OLED Expansion
Onion Corporation OLED Expansion
×1
Omega2 Plus
Onion Corporation Omega2 Plus
×1
SparkFun Li-Ion 3.7V Battery
×1

Story

Read more

Code

oled-display-battery

Python
A python program to retrieve the battery's charge and output in on OLED Expansion as well as the current date and time
import os, datetime, time
from OmegaExpansion import oledExp
from subprocess import Popen, PIPE

# find current time
currentTime = datetime.datetime.now()

# measure and format battery voltage
program = Popen("power-dock2", stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err  = program.communicate()
battery_text = "Battery(V): " +  output[-7:]

# converting a string value of the battery to a float value
Vcurrent = float(output[-7:-2])

# math function to determine the battery percentage
Vmax = 4.2
Vmin = 3.5
battery_percentage = ((Vcurrent-Vmin)/(Vmax-Vmin))*100
battery_percentage =int(battery_percentage)


# initialize the oled
status = oledExp.driverInit()

# write content to oled
oledExp.write("Current date and time ")
oledExp.write(currentTime.strftime("%Y-%m-%d %H:%M:%S"))

oledExp.setCursor(3, 0)
oledExp.write(battery_text)

oledExp.setCursor(5, 0)
oledExp.write('Battery(%): ' + str(battery_percentage) + '%')

Credits

Pavel

Pavel

5 projects • 5 followers

Comments