Skip to content

Commit c39aa8a

Browse files
committed
updated/shuffled examples
1 parent 55de893 commit c39aa8a

File tree

10 files changed

+184
-112
lines changed

10 files changed

+184
-112
lines changed

README.rst

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,40 @@ Usage Example
6363

6464
.. code-block:: python3
6565
66-
import board
67-
import adafruit_pcf8591
68-
import time
69-
70-
i2c = board.I2C()
71-
pcf = adafruit_pcf8591.PCF8591(i2c)
72-
73-
while True:
74-
read_value = pcf.analog_read(0)
75-
print("Read value:", read_value, "scaled value: %0.2f V"%((read_value/255)*3.3))
76-
time.sleep(.1)
66+
import time
67+
import board
68+
69+
import adafruit_pcf8591.pcf8591 as PCF
70+
from adafruit_pcf8591.analog_in import AnalogIn
71+
from adafruit_pcf8591.analog_out import AnalogOut
72+
73+
############# AnalogOut & AnalogIn Example ##########################
74+
#
75+
# This example shows how to use the included AnalogIn and AnalogOut
76+
# classes to set the internal DAC to output a voltage and then measure
77+
# it with the first ADC channel.
78+
#
79+
# Wiring:
80+
# Connect the DAC output to the first ADC channel, in addition to the
81+
# normal power and I2C connections
82+
#
83+
#####################################################################
84+
i2c = board.I2C()
85+
pcf = PCF.PCF8591(i2c)
86+
87+
pcf_in_0 = AnalogIn(pcf, PCF.A0)
88+
pcf_out = AnalogOut(pcf, PCF.OUT)
89+
90+
while True:
91+
92+
print("Setting out to ", 65535)
93+
pcf_out.value = 65535
94+
raw_value = pcf_in_0.value
95+
scaled_value = (raw_value / 65535) * pcf_in_0.reference_voltage
96+
97+
print("Pin 0: %0.2fV" % (scaled_value))
98+
print("")
99+
time.sleep(1)
77100
78101
79102

adafruit_pcf8591/analog_in.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,34 @@ def __init__(self, pcf, pin):
3535
"""AnalogIn
3636
3737
:param ads: The PCF8591 object.
38-
:param ~digitalio.DigitalInOut pin: Required pin for single-ended.
38+
:param ~digitalio.DigitalInOut pin: Required ADC channel pin.
3939
4040
"""
4141
self._pcf = pcf
4242
self._channel_number = pin
4343

44+
@property
45+
def voltage(self):
46+
"""Returns the value of an ADC channel in volts as compared to the reference voltage."""
47+
48+
if not self._pcf:
49+
raise RuntimeError(
50+
"Underlying ADC does not exist, likely due to callint `deinit`"
51+
)
52+
raw_reading = self._pcf.read(self._channel_number)
53+
return ((raw_reading << 8) / 65535) * self._pcf.reference_voltage
54+
4455
@property
4556
def value(self):
46-
"""Returns the value of an ADC pin scaled to a 16-bit integer from the native value."""
57+
"""Returns the value of an ADC channel.
58+
The value is scaled to a 16-bit integer from the native 8-bit value."""
4759

4860
if not self._pcf:
4961
raise RuntimeError(
5062
"Underlying ADC does not exist, likely due to callint `deinit`"
5163
)
5264

53-
return self._pcf.analog_read(self._channel_number) << 8
65+
return self._pcf.read(self._channel_number) << 8
5466

5567
@property
5668
def reference_voltage(self):

adafruit_pcf8591/analog_out.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def value(self, new_value): # this may have to scale from 16-bit
5959
"Underlying DAC is disabled, likely due to callint `deinit`"
6060
)
6161
# underlying sensor is 8-bit, so scale accordingly
62-
self._pcf.analog_write(new_value >> 8)
62+
self._pcf.write(new_value >> 8)
6363
self._value = new_value
6464

6565
def deinit(self):

adafruit_pcf8591/pcf8591.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def reference_voltage(self):
7373
An ADC value of 65535 will equal `reference_voltage`"""
7474
return self._reference_voltage
7575

76-
def analog_read(self, channel):
76+
def read(self, channel):
7777
"""Read an analog value from one of the four ADC inputs
7878
7979
param: :adcnum The single-ended ADC to read from, 0 thru 3
@@ -103,9 +103,9 @@ def dac_enabled(self):
103103
def dac_enabled(self, enable_dac):
104104

105105
self._dac_enabled = enable_dac
106-
self.analog_write(self._dacval)
106+
self.write(self._dacval)
107107

108-
def analog_write(self, value):
108+
def write(self, value):
109109
"""Writes a uint8_t value to the DAC output
110110
111111
param: :output The value to write: 0 is GND and 65535 is VCC

examples/pcf8591_adc_example.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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)

examples/pcf8591_analog_in.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
import adafruit_pcf8591.pcf8591 as PCF
77
from adafruit_pcf8591.analog_in import AnalogIn
88

9-
VOLTAGE_LEVEL = 3.3
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+
1021
i2c = board.I2C()
1122
pcf = PCF.PCF8591(i2c)
1223

examples/pcf8591_analog_out.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/pcf8591_dac_example.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
import time
6+
import board
7+
from adafruit_pcf8591.pcf8591 import PCF8591
8+
9+
################ read/DAC Example #####################
10+
#
11+
# This example shows how to use a PCF8591 instance to set the voltage output by the included DAC.
12+
#
13+
# Wiring:
14+
# Connect the DAC output to the first ADC channel, in addition to the
15+
# normal power and I2C connections
16+
#
17+
########################################
18+
i2c = board.I2C()
19+
20+
pcf = PCF8591(i2c)
21+
print("enabling DAC")
22+
pcf.dac_enabled = True
23+
while True:
24+
print("Setting DAC to", 255)
25+
pcf.write(255)
26+
print("Reading channel 0")
27+
read_value = pcf.read(0)
28+
scaled_value = (read_value / 255) * pcf.reference_voltage
29+
30+
print("Channel 0: %0.2fV" % (scaled_value))
31+
print("")
32+
time.sleep(0.5)
33+
print("Setting DAC to", 0)
34+
pcf.write(0)
35+
36+
print("Reading channel 0")
37+
read_value = pcf.read(0)
38+
scaled_value = (read_value / 255) * pcf.reference_voltage
39+
40+
print("Channel 3: %0.2fV" % (scaled_value))
41+
print("")
42+
43+
time.sleep(1.0)

examples/pcf8591_dac_test.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

examples/pcf8591_simpletest.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2-
#
32
# SPDX-License-Identifier: MIT
43
import time
54
import board
6-
from adafruit_pcf8591.pcf8591 import PCF8591
75

8-
VOLTAGE_LEVEL = 3.3
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+
#####################################################################
921
i2c = board.I2C()
10-
pcf = PCF8591(i2c)
22+
pcf = PCF.PCF8591(i2c)
1123

12-
channel_a = 0
13-
channel_b = 1
24+
pcf_in_0 = AnalogIn(pcf, PCF.A0)
25+
pcf_out = AnalogOut(pcf, PCF.OUT)
1426

1527
while True:
1628

17-
read_value = pcf.analog_read(channel_a)
18-
scaled_value = (read_value / 255) * VOLTAGE_LEVEL
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
1933

20-
print("Channel: %d %0.2fV" % (channel_a, scaled_value))
34+
print("Pin 0: %0.2fV" % (scaled_value))
2135
print("")
22-
time.sleep(0.1)
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)
2346

24-
read_value = pcf.analog_read(channel_b)
25-
scaled_value = (read_value / 255) * VOLTAGE_LEVEL
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
2651

27-
print("Channel: %d %0.2fV" % (channel_b, scaled_value))
52+
print("Pin 0: %0.2fV" % (scaled_value))
2853
print("")
29-
print("*" * 20)
3054
time.sleep(1)

0 commit comments

Comments
 (0)