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