pcf8591_analog_in.py
1 # SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 import time 4 import board 5 6 import adafruit_pcf8591.pcf8591 as PCF 7 from adafruit_pcf8591.analog_in import AnalogIn 8 9 ################ AnalogIn Example ##################### 10 # 11 # This example shows how to use the AnalogIn class provided 12 # by the library by creating an AnalogIn instance and using 13 # it to measure the voltage at the first ADC channel input 14 # 15 # Wiring: 16 # Connect a voltage source to the first ADC channel, in addition to the 17 # normal power and I2C connections. The voltage level should be between 0V/GND and VCC 18 # 19 ######################################## 20 21 i2c = board.I2C() 22 pcf = PCF.PCF8591(i2c) 23 24 pcf_in_0 = AnalogIn(pcf, PCF.A0) 25 while True: 26 raw_value = pcf_in_0.value 27 scaled_value = (raw_value / 65535) * pcf_in_0.reference_voltage 28 29 print("Pin 0: %0.2fV" % (scaled_value)) 30 print("") 31 time.sleep(1)