Simple test

Ensure your device works with this simple test.

examples/pcf8591_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3import time
 4import board
 5
 6import adafruit_pcf8591.pcf8591 as PCF
 7from adafruit_pcf8591.analog_in import AnalogIn
 8from 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#####################################################################
21i2c = board.I2C()  # uses board.SCL and board.SDA
22# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
23pcf = PCF.PCF8591(i2c)
24
25pcf_in_0 = AnalogIn(pcf, PCF.A0)
26pcf_out = AnalogOut(pcf, PCF.OUT)
27
28while True:
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)