It's easy to use the Proto Tripler PiCowbell with CircuitPython to monitor the voltage of an attached lipoly battery with the analogio core module and connect to an external display with the EYESPI connector and displayio core module. These modules allow you to easily write Python code for accessing basic analog inputs and outputs and writing to a display.
Raspberry Pi Pico ADC3 and VSYS
The Raspberry Pi Pico can use its internal ADC3 pin (GPIO29) to monitor the voltage on VSYS. As a result, you can read the voltage currently being supplied to VSYS (aka the voltage from your battery) with this calculation in CircuitPython:
((ADC3 value * 3) * 3.3) / 65535
CircuitPython Microcontroller Wiring
Plug a Pico or Pico W into your Proto Tripler PiCowbell exactly as shown below. Then, plug in a supported lipoly battery to the PiCowbell JST 2-PH port. Connect an external display with an EYESPI connector to the EYESPI connector on the PiCowbell with an EYESPI cable. Here's an example of connecting a Pico to the PiCowbell with a lipoly battery and 1.54" 240x240 display.
Connect the Pico with plug headers into the Proto Tripler PiCowbell. It should be plugged in with the Pico USB port pointing towards the STEMMA QT port.
Then, plug in a lipoly battery with matching polarity to the JST 2-PH port on the PiCowbell.
Finally, attach the EYESPI display to the EYESPI connector with an EYESPI cable. For reference on plugging in an EYESPI cable, you can follow along with this Learn Guide page.
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_ST7789 library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder, font folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and files:
- adafruit_bitmap_font/
- adafruit_display_text/
- adafruit_pixelbuf.mpy
- adafruit_st7789.mpy
- adafruit_ticks.mpy
- neopixel.mpy
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import busio import board from analogio import AnalogIn import adafruit_st7789 import displayio import neopixel from rainbowio import colorwheel from adafruit_display_text import label from adafruit_ticks import ticks_ms, ticks_add, ticks_diff from font_orbitron_bold_webfont_36 import FONT as orbitron_font displayio.release_displays() spi = busio.SPI(clock=board.GP18, MOSI=board.GP19) display_bus = displayio.FourWire(spi, command=board.GP20, chip_select=board.GP21, reset=None) display = adafruit_st7789.ST7789(display_bus, width=240, height=240, rowstart=80, rotation=0) group = displayio.Group() text = label.Label(orbitron_font, text="0V", color=0xFF0000) text.anchor_point = (0.5, 0.5) text.anchored_position = (display.width / 2, display.height / 2) group.append(text) display.root_group = group analog_in = AnalogIn(board.A3) def get_vsys(pin): return ((pin.value * 3) * 3.3) / 65535 pixel_pin = board.A2 num_pixels = 1 pixel = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.1, auto_write=True) hue = 0 pixel.fill(colorwheel(hue)) bat_clock = ticks_ms() bat_timer = 5000 neo_clock = ticks_ms() neo_timer = 100 while True: if ticks_diff(ticks_ms(), bat_clock) >= bat_timer: print(f"The battery level is: {get_vsys(analog_in):.1f}V") text.text = f"{get_vsys(analog_in):.1f}V" text.color = colorwheel(hue) bat_clock = ticks_add(bat_clock, bat_timer) if ticks_diff(ticks_ms(), neo_clock) >= neo_timer: hue = (hue + 7) % 256 pixel.fill(colorwheel(hue)) neo_clock = ticks_add(neo_clock, neo_timer)
Every 5 seconds, the analog reading from pin A3 on the Pico is passed to the get_vsys()
function. This function calculates the voltage on VSYS and prints the reading to the serial monitor. The screenshot below shows a battery initially plugged in at 4.6V. The battery was unplugged, reading 4.8V from the USB power and then plugged back in, reading 4.6V.
The display will show the voltage reading as well and will update every 5 seconds. The color of the font will change to match the current hue of the NeoPixel. The onboard NeoPixel will show a rainbow swirl animation.
Text editor powered by tinymce.