# .';:cc;. # .,',;lol::c. # ;';lddddlclo # lcloxxoddodxdool:,. # cxdddxdodxdkOkkkkkkkd:. # .ldxkkOOOOkkOO000Okkxkkkkx:. # .lddxkkOkOOO0OOO0000Okxxxxkkkk: # 'ooddkkkxxkO0000KK00Okxdoodxkkkko # .ooodxkkxxxOO000kkkO0KOxolooxkkxxkl # lolodxkkxxkOx,. .lkdolodkkxxxO. # doloodxkkkOk .... .,cxO; # ddoodddxkkkk: ,oxxxkOdc'..o' # :kdddxxxxd, ,lolccldxxxkkOOOkkkko, # lOkxkkk; :xkkkkkkkkOOO000OOkkOOk. # ;00Ok' 'O000OO0000000000OOOO0Od. # .l0l.;OOO000000OOOOOO000000x, # .'OKKKK00000000000000kc. # .:ox0KKKKKKK0kdc,. # ... # # Author: peppe8o # Date: Sep 24th, 2022 # Version: 1.0 # blog: https://peppe8o.com # Please rename to "main.py" this script if you want to run it witout a connected PC # Requires the ssd1306 library from machine import Pin, I2C, ADC from utime import sleep import ssd1306 # setup the I2C communication with the OLED display i2c = I2C(0, sda=Pin(16), scl=Pin(17)) display = ssd1306.SSD1306_I2C(128, 64, i2c) # setup the ADC reading pin Vin=ADC(26) # the following function will run when no batteries are detected def open_circ(): display.text('-BATTERY TESTER-', 0, 0) display.text('Ready to test...', 0, 16) display.show() # the following function return the read voltage if a battery is detected def measuring(adc_16_read): display.text('-BATTERY TESTER-', 0, 0) display.text('Measuring...', 0, 16) read_voltage=3.3*adc_16_read/65535 read_voltage=round(read_voltage,2) display.text('Vin = '+str(read_voltage)+'V', 0, 32) display.show() # main program # defines the board noise threshold. Under this value, no batteries are detected noise = 2000 while True: try: reading = Vin.read_u16() # for debug purposes, you can uncomment the following print statement #print(str(reading)) display.fill(0) if reading < noise: open_circ() else: measuring(reading) sleep(1) except OSError as e: machine.reset()