├── requirements.txt ├── resource └── result.png ├── LICENSE ├── README.md ├── read_voltage.py └── .gitignore /requirements.txt: -------------------------------------------------------------------------------- 1 | smbus2 -------------------------------------------------------------------------------- /resource/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Seeed-Projects/Read-UPS-input-voltage/main/resource/result.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Seeed-Projects 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | This repository uses a Python script to read the UPS input voltage on the R1000. 4 | 5 | ## Hardware requirement 6 | 7 | | Raspberry Pi AI Kit | reComputer R1000 | 8 | | :----------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------: | 9 | | ![SuperCAP UPS LTC3350 Module](https://media-cdn.seeedstudio.com/media/catalog/product/cache/bb49d3ec4ee05b6f018e93f896b8a25d/x/-/x-110992004-supercap_ups_ltc3350_module.jpg) | ![reComputer R1100](https://media-cdn.seeedstudio.com/media/catalog/product/cache/bb49d3ec4ee05b6f018e93f896b8a25d/1/1/113991294-2_1.jpeg) | 10 | | [**Purchase Now**](https://www.seeedstudio.com/SuperCAP-UPS-LTC3350-Module-p-5934.html?utm_source=PiAICourse&utm_medium=github&utm_campaign=Course) | [**Purchase Now**](https://www.seeedstudio.com/reComputer-R1024-10-p-5923.html?utm_source=PiAICourse&utm_medium=github&utm_campaign=Course) | 11 | 12 | ## Quikly begin 13 | 14 | ``` 15 | git clone https://github.com/Seeed-Projects/Read-UPS-input-voltage.git 16 | cd Read-UPS-input-voltage 17 | sudo apt update 18 | sudo apt install python3-smbus i2c-tools 19 | python -m venv .venv --system-site-packages && source .venv/bin/activate 20 | python read_voltage.py 21 | ``` 22 | 23 | ## Result 24 | 25 | ![](./resource/result.png) 26 | -------------------------------------------------------------------------------- /read_voltage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import smbus2 3 | import time 4 | import errno 5 | 6 | # config 7 | I2C_BUS = 6 # I2C 8 | I2C_ADDR = 0x09 # LTC3350 I2C address 9 | MAX_RETRIES = 5 10 | RETRY_DELAY = 0.05 11 | 12 | # votage/current ADC address 13 | REG_MEAS_VIN = 0x25 14 | 15 | # LSB 16 | VIN_LSB_V = 0.00221 # 2.21mV / LSB 17 | 18 | 19 | def read_register_word(bus, reg): 20 | for attempt in range(1, MAX_RETRIES + 1): 21 | try: 22 | raw = bus.read_word_data(I2C_ADDR, reg) 23 | return raw 24 | except OSError as e: 25 | if e.errno == errno.EIO: 26 | print(f"⚠️ read register 0x{reg:02X} {attempt} error, try after {RETRY_DELAY}s ") 27 | time.sleep(RETRY_DELAY) 28 | else: 29 | raise 30 | print(f"❌ read 0x{reg:02X} over {MAX_RETRIES} times, give up.") 31 | return None 32 | 33 | def main(): 34 | print("🚀 Begin LTC3350 UPS monitoring input voltage, input Ctrl+C will exit\n") 35 | try: 36 | with smbus2.SMBus(I2C_BUS) as bus: 37 | while True: 38 | vin_code = read_register_word(bus, REG_MEAS_VIN) 39 | 40 | if vin_code is not None: 41 | vin = vin_code * VIN_LSB_V 42 | print(f"🔌 VIN = {vin:.3f} V (ADC Code = {vin_code})") 43 | else: 44 | print("🔌 read VIN error") 45 | 46 | print("-" * 40) 47 | time.sleep(1) 48 | 49 | except KeyboardInterrupt: 50 | print("\n🛑 exit。") 51 | 52 | if __name__ == "__main__": 53 | main() 54 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # UV 98 | # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | #uv.lock 102 | 103 | # poetry 104 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 105 | # This is especially recommended for binary packages to ensure reproducibility, and is more 106 | # commonly ignored for libraries. 107 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 108 | #poetry.lock 109 | 110 | # pdm 111 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 112 | #pdm.lock 113 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 114 | # in version control. 115 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 116 | .pdm.toml 117 | .pdm-python 118 | .pdm-build/ 119 | 120 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 121 | __pypackages__/ 122 | 123 | # Celery stuff 124 | celerybeat-schedule 125 | celerybeat.pid 126 | 127 | # SageMath parsed files 128 | *.sage.py 129 | 130 | # Environments 131 | .env 132 | .venv 133 | env/ 134 | venv/ 135 | ENV/ 136 | env.bak/ 137 | venv.bak/ 138 | 139 | # Spyder project settings 140 | .spyderproject 141 | .spyproject 142 | 143 | # Rope project settings 144 | .ropeproject 145 | 146 | # mkdocs documentation 147 | /site 148 | 149 | # mypy 150 | .mypy_cache/ 151 | .dmypy.json 152 | dmypy.json 153 | 154 | # Pyre type checker 155 | .pyre/ 156 | 157 | # pytype static type analyzer 158 | .pytype/ 159 | 160 | # Cython debug symbols 161 | cython_debug/ 162 | 163 | # PyCharm 164 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 165 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 166 | # and can be added to the global gitignore or merged into this file. For a more nuclear 167 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 168 | #.idea/ 169 | 170 | # Ruff stuff: 171 | .ruff_cache/ 172 | 173 | # PyPI configuration file 174 | .pypirc 175 | # Byte-compiled / optimized / DLL files 176 | __pycache__/ 177 | *.py[cod] 178 | *$py.class 179 | 180 | # C extensions 181 | *.so 182 | 183 | # Distribution / packaging 184 | .Python 185 | build/ 186 | develop-eggs/ 187 | dist/ 188 | downloads/ 189 | eggs/ 190 | .eggs/ 191 | lib/ 192 | lib64/ 193 | parts/ 194 | sdist/ 195 | var/ 196 | wheels/ 197 | share/python-wheels/ 198 | *.egg-info/ 199 | .installed.cfg 200 | *.egg 201 | MANIFEST 202 | 203 | # PyInstaller 204 | # Usually these files are written by a python script from a template 205 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 206 | *.manifest 207 | *.spec 208 | 209 | # Installer logs 210 | pip-log.txt 211 | pip-delete-this-directory.txt 212 | 213 | # Unit test / coverage reports 214 | htmlcov/ 215 | .tox/ 216 | .nox/ 217 | .coverage 218 | .coverage.* 219 | .cache 220 | nosetests.xml 221 | coverage.xml 222 | *.cover 223 | *.py,cover 224 | .hypothesis/ 225 | .pytest_cache/ 226 | cover/ 227 | 228 | # Translations 229 | *.mo 230 | *.pot 231 | 232 | # Django stuff: 233 | *.log 234 | local_settings.py 235 | db.sqlite3 236 | db.sqlite3-journal 237 | 238 | # Flask stuff: 239 | instance/ 240 | .webassets-cache 241 | 242 | # Scrapy stuff: 243 | .scrapy 244 | 245 | # Sphinx documentation 246 | docs/_build/ 247 | 248 | # PyBuilder 249 | .pybuilder/ 250 | target/ 251 | 252 | # Jupyter Notebook 253 | .ipynb_checkpoints 254 | 255 | # IPython 256 | profile_default/ 257 | ipython_config.py 258 | 259 | # pyenv 260 | # For a library or package, you might want to ignore these files since the code is 261 | # intended to run in multiple environments; otherwise, check them in: 262 | # .python-version 263 | 264 | # pipenv 265 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 266 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 267 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 268 | # install all needed dependencies. 269 | #Pipfile.lock 270 | 271 | # UV 272 | # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. 273 | # This is especially recommended for binary packages to ensure reproducibility, and is more 274 | # commonly ignored for libraries. 275 | #uv.lock 276 | 277 | # poetry 278 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 279 | # This is especially recommended for binary packages to ensure reproducibility, and is more 280 | # commonly ignored for libraries. 281 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 282 | #poetry.lock 283 | 284 | # pdm 285 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 286 | #pdm.lock 287 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 288 | # in version control. 289 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 290 | .pdm.toml 291 | .pdm-python 292 | .pdm-build/ 293 | 294 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 295 | __pypackages__/ 296 | 297 | # Celery stuff 298 | celerybeat-schedule 299 | celerybeat.pid 300 | 301 | # SageMath parsed files 302 | *.sage.py 303 | 304 | # Environments 305 | .env 306 | .venv 307 | env/ 308 | venv/ 309 | ENV/ 310 | env.bak/ 311 | venv.bak/ 312 | 313 | # Spyder project settings 314 | .spyderproject 315 | .spyproject 316 | 317 | # Rope project settings 318 | .ropeproject 319 | 320 | # mkdocs documentation 321 | /site 322 | 323 | # mypy 324 | .mypy_cache/ 325 | .dmypy.json 326 | dmypy.json 327 | 328 | # Pyre type checker 329 | .pyre/ 330 | 331 | # pytype static type analyzer 332 | .pytype/ 333 | 334 | # Cython debug symbols 335 | cython_debug/ 336 | 337 | # PyCharm 338 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 339 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 340 | # and can be added to the global gitignore or merged into this file. For a more nuclear 341 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 342 | #.idea/ 343 | 344 | # Ruff stuff: 345 | .ruff_cache/ 346 | 347 | # PyPI configuration file 348 | .pypirc 349 | --------------------------------------------------------------------------------