/ examples / pcf8591_simpletest.py
pcf8591_simpletest.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  from adafruit_pcf8591.analog_out import AnalogOut
 9  
10  ############# AnalogOut & AnalogIn Example ##########################
11  #
12  # This example shows how to use the included AnalogIn and AnalogOut
13  # classes to set the internal DAC to output a voltage and then measure
14  # it with the first ADC channel.
15  #
16  # Wiring:
17  # Connect the DAC output to the first ADC channel, in addition to the
18  # normal power and I2C connections
19  #
20  #####################################################################
21  i2c = board.I2C()
22  pcf = PCF.PCF8591(i2c)
23  
24  pcf_in_0 = AnalogIn(pcf, PCF.A0)
25  pcf_out = AnalogOut(pcf, PCF.OUT)
26  
27  while True:
28  
29      print("Setting out to ", 65535)
30      pcf_out.value = 65535
31      raw_value = pcf_in_0.value
32      scaled_value = (raw_value / 65535) * pcf_in_0.reference_voltage
33  
34      print("Pin 0: %0.2fV" % (scaled_value))
35      print("")
36      time.sleep(1)
37  
38      print("Setting out to ", 32767)
39      pcf_out.value = 32767
40      raw_value = pcf_in_0.value
41      scaled_value = (raw_value / 65535) * pcf_in_0.reference_voltage
42  
43      print("Pin 0: %0.2fV" % (scaled_value))
44      print("")
45      time.sleep(1)
46  
47      print("Setting out to ", 0)
48      pcf_out.value = 0
49      raw_value = pcf_in_0.value
50      scaled_value = (raw_value / 65535) * pcf_in_0.reference_voltage
51  
52      print("Pin 0: %0.2fV" % (scaled_value))
53      print("")
54      time.sleep(1)