/ examples / pcf8591_adc_example.py
pcf8591_adc_example.py
 1  # SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  import time
 5  import board
 6  from adafruit_pcf8591.pcf8591 import PCF8591
 7  
 8  ################ Read/ADC Example #####################
 9  #
10  # This example shows how to use a PCF8591 instance to read one of the ADC channels.
11  #
12  # Wiring:
13  # Connect a voltage source to the first ADC channel, in addition to the
14  # normal power and I2C connections. The voltage level should be between 0V/GND and VCC
15  #
16  ########################################
17  i2c = board.I2C()
18  pcf = PCF8591(i2c)
19  
20  channel_a = 0
21  channel_b = 1
22  
23  while True:
24  
25      read_value = pcf.read(channel_a)
26      scaled_value = (read_value / 255) * pcf.reference_voltage
27  
28      print("Channel: %d %0.2fV" % (channel_a, scaled_value))
29      print("")
30      time.sleep(0.1)
31  
32      read_value = pcf.read(channel_b)
33      scaled_value = (read_value / 255) * pcf.reference_voltage
34  
35      print("Channel: %d %0.2fV" % (channel_b, scaled_value))
36      print("")
37      print("*" * 20)
38      time.sleep(1)