From 7245d3c47e03f83571342c8e324aedf9f7146418 Mon Sep 17 00:00:00 2001 From: helgibbons <50950368+helgibbons@users.noreply.github.com> Date: Mon, 12 Apr 2021 17:22:42 +0100 Subject: [PATCH 1/2] Add Pico Lipo SHIM example --- .../examples/pico_lipo_shim/battery.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 micropython/examples/pico_lipo_shim/battery.py diff --git a/micropython/examples/pico_lipo_shim/battery.py b/micropython/examples/pico_lipo_shim/battery.py new file mode 100644 index 00000000..b4c8e58a --- /dev/null +++ b/micropython/examples/pico_lipo_shim/battery.py @@ -0,0 +1,46 @@ +# This example shows how to read the voltage from a lipo battery connected to a Raspberry Pi Pico via our Pico Lipo SHIM, and uses this reading to calculate how much charge is left in the battery. +# It then displays the info on the screen of Pico Display or Pico Explorer. +# Remember to save this code as main.py on your Pico if you want it to run automatically! + +from machine import ADC, Pin +import utime +import picodisplay as display # change "picodisplay" to "picoexplorer" if you're using a Pico Explorer + +# Set up and initialise display +buf = bytearray(display.get_width() * display.get_height() * 2) +display.init(buf) +display.set_backlight(0.8) # comment out this line if you have a Pico Explorer as it doesn't have a controllable backlight + +vsys = machine.ADC(29) # reads the system input voltage +charging = machine.Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected +conversion_factor = 3 * 3.3 / 65535 + +full_battery = 4.2 # these are our reference voltages for a full/empty battery, in volts +empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them + +while True: + voltage = vsys.read_u16() * conversion_factor + percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery)) + if percentage > 100: percentage = 100.00 + + # draws the battery + display.set_pen(0, 0, 0) + display.clear() + display.set_pen(190, 190, 190) + display.rectangle(0, 0, 220, 135) + display.rectangle(220, 40, 20, 55) + display.set_pen(0, 0, 0) + display.rectangle(3, 3, 214, 129) + display.set_pen(0, 255, 0) + display.rectangle(5, 5, round(210 / 100 * percentage), 125) + + # adding text + display.set_pen(255, 0, 0) + if charging.value() == 1: # if it's plugged into USB power... + display.text("Charging!", 15, 55, 240, 4) + else: # if not, display the battery stats + display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5) + display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5) + + display.update() + utime.sleep(1) \ No newline at end of file From f7977dc17c8b38d47711896f0553000efd093659 Mon Sep 17 00:00:00 2001 From: helgibbons <50950368+helgibbons@users.noreply.github.com> Date: Fri, 16 Apr 2021 10:09:40 +0100 Subject: [PATCH 2/2] Linting battery.py --- .../examples/pico_lipo_shim/battery.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/micropython/examples/pico_lipo_shim/battery.py b/micropython/examples/pico_lipo_shim/battery.py index b4c8e58a..0d6d5b7f 100644 --- a/micropython/examples/pico_lipo_shim/battery.py +++ b/micropython/examples/pico_lipo_shim/battery.py @@ -1,5 +1,5 @@ -# This example shows how to read the voltage from a lipo battery connected to a Raspberry Pi Pico via our Pico Lipo SHIM, and uses this reading to calculate how much charge is left in the battery. -# It then displays the info on the screen of Pico Display or Pico Explorer. +# This example shows how to read the voltage from a lipo battery connected to a Raspberry Pi Pico via our Pico Lipo SHIM, and uses this reading to calculate how much charge is left in the battery. +# It then displays the info on the screen of Pico Display or Pico Explorer. # Remember to save this code as main.py on your Pico if you want it to run automatically! from machine import ADC, Pin @@ -11,9 +11,9 @@ buf = bytearray(display.get_width() * display.get_height() * 2) display.init(buf) display.set_backlight(0.8) # comment out this line if you have a Pico Explorer as it doesn't have a controllable backlight -vsys = machine.ADC(29) # reads the system input voltage -charging = machine.Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected -conversion_factor = 3 * 3.3 / 65535 +vsys = ADC(29) # reads the system input voltage +charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected +conversion_factor = 3 * 3.3 / 65535 full_battery = 4.2 # these are our reference voltages for a full/empty battery, in volts empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them @@ -21,9 +21,10 @@ empty_battery = 2.8 # the values could vary by battery size/manu while True: voltage = vsys.read_u16() * conversion_factor percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery)) - if percentage > 100: percentage = 100.00 - - # draws the battery + if percentage > 100: + percentage = 100.00 + + # draws the battery display.set_pen(0, 0, 0) display.clear() display.set_pen(190, 190, 190) @@ -33,14 +34,14 @@ while True: display.rectangle(3, 3, 214, 129) display.set_pen(0, 255, 0) display.rectangle(5, 5, round(210 / 100 * percentage), 125) - + # adding text display.set_pen(255, 0, 0) if charging.value() == 1: # if it's plugged into USB power... display.text("Charging!", 15, 55, 240, 4) else: # if not, display the battery stats - display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5) + display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5) display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5) display.update() - utime.sleep(1) \ No newline at end of file + utime.sleep(1)