Skip to content

Commit 5d1980c

Browse files
committed
1 parent b242220 commit 5d1980c

File tree

9 files changed

+36
-17
lines changed

9 files changed

+36
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### MicroPython Library to use i2c analog IC with arduino and esp8266. Can read analog value and write analog value with only 2 wire (perfect for ESP-01).
1616

1717
#### Changelog
18+
- 16/05/2023: v0.0.2 Minor fix on read and write [#Forum](https://www.mischianti.org/forums/topic/micropython-i2c-pcf8591-round-value-problem-raspberry-pi-pico/)
1819
- 18/04/2023: v0.0.1 Initial commit of stable version.
1920

2021
I try to simplify the use of this IC, with a minimal set of operation.

examples/readAllAnalog.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# AUTHOR: Renzo Mischianti
55
# Website: www.mischianti.org
6-
# VERSION: 0.0.1
76
#
87
# Description:
98
# This script read all analog input of pcf8591

examples/readAnalog.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# AUTHOR: Renzo Mischianti
55
# Website: www.mischianti.org
6-
# VERSION: 0.0.1
76
#
87
# Description:
98
# This script read one channel for time of pcf8591

examples/readVoltage.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# AUTHOR: Renzo Mischianti
55
# Website: www.mischianti.org
6-
# VERSION: 0.0.1
76
#
87
# Description:
98
# This script read voltage of the channel

examples/writeAnalog.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# AUTHOR: Renzo Mischianti
55
# Website: www.mischianti.org
6-
# VERSION: 0.0.1
76
#
87
# Description:
98
# This script write an analog value on OUT of pcf8591

examples/writeVoltage.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# AUTHOR: Renzo Mischianti
55
# Website: www.mischianti.org
6-
# VERSION: 0.0.1
76
#
87
# Description:
98
# This script write an voltage value on OUT of pcf8591

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# setup.cfg file at the root directory
22
[metadata]
33
name = pcf8591-library
4-
version = 0.0.1
4+
version = 0.0.2
55
author = Renzo Mischianti
66
description = PCF8591 micropython library for Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266
77
long_description = MicroPython library to use pcf8591 i2c analog IC with Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266. Can read analog value and write analog value with only 2 wire.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
name="pcf8591-library",
77
package_dir={'': 'src'},
88
py_modules=["PCF8591"],
9-
version="0.0.1",
9+
version="0.0.2",
1010
description="PCF8591, library for Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266",
1111
long_description="Library to use pcf8591 i2c analog IC with Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266. Can read analog value and write analog value with only 2 wire. ",
1212
keywords="micropython, digital, i2c, expander, pcf8591, pcf8591a, esp32, esp8266, stm32, SAMD, Arduino, wire, rp2040, Raspberry",

src/PCF8591.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# AUTHOR: Renzo Mischianti
55
# Website: www.mischianti.org
6-
# VERSION: 0.0.1
6+
# VERSION: 0.0.2
77
#
88
#
99
# Porting of PCF8591 library for Arduino
@@ -93,6 +93,7 @@ class PCF8591:
9393
OUTPUT_MASK = 0b01000000
9494

9595
def __init__(self, address, i2c=None, i2c_id=0, sda=None, scl=None):
96+
self._last_operation = None
9697
if i2c:
9798
self._i2c = i2c
9899
elif sda and scl:
@@ -110,24 +111,46 @@ def begin(self):
110111
else:
111112
return True
112113

114+
def _get_operation(self, auto_increment=False, channel=AIN0, read_type=SINGLE_ENDED_INPUT):
115+
# print('auto_increment: {}, channel: {}, read_type: {}, self._output_status {}'.format(auto_increment, channel, read_type, self._output_status))
116+
return 0 | (self._output_status & self.OUTPUT_MASK) | read_type | \
117+
(self.AUTOINCREMENT_READ if auto_increment else 0) | \
118+
channel
119+
120+
def _write_operation(self, operation):
121+
if operation != self._last_operation:
122+
self._i2c.writeto(self._address, bytearray([operation]))
123+
utime.sleep_ms(1)
124+
self._i2c.readfrom(self._address, 1)
125+
self._last_operation = operation
126+
113127
def analog_read_all(self, read_type=SINGLE_ENDED_INPUT):
114-
operation = self.AUTOINCREMENT_READ | read_type | (self._output_status & self.OUTPUT_MASK)
115-
self._i2c.writeto(self._address, bytearray([operation]))
116-
utime.sleep_ms(1)
117-
# data = self._i2c.readfrom(self._address, 5)
128+
# operation = self.AUTOINCREMENT_READ | read_type | (self._output_status & self.OUTPUT_MASK)
129+
# operation = self.AUTOINCREMENT_READ | self.OUTPUT_MASK
130+
# self._i2c.writeto(self._address, bytearray([operation]))
131+
# utime.sleep_ms(1)
132+
# self._i2c.readfrom(self._address, 1)
133+
134+
self._output_status = self.ENABLE_OUTPUT
135+
operation = self._get_operation(auto_increment=True)
136+
self._write_operation(operation)
137+
138+
# data = self._i2c.readfrom(self._address, 4)
118139
data = []
119-
self._i2c.readfrom(self._address, 1)
120140
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
121141
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
122142
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
123143
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
124144

125-
return data[0], data[1], data[2], data[3]
145+
return int(data[0]), int(data[1]), int(data[2]), int(data[3])
126146

127147
def analog_read(self, channel, read_type=SINGLE_ENDED_INPUT):
128-
operation = channel | read_type | (self._output_status & self.OUTPUT_MASK)
129-
self._i2c.writeto(self._address, bytearray([operation]))
130-
utime.sleep_ms(1)
148+
# operation = channel | read_type | (self._output_status & self.OUTPUT_MASK)
149+
# self._i2c.writeto(self._address, bytearray([operation]))
150+
# utime.sleep_ms(1)
151+
operation = self._get_operation(auto_increment=False, channel=channel, read_type=read_type)
152+
self._write_operation(operation)
153+
131154
data = self._i2c.readfrom(self._address, 2)
132155
return data[1]
133156

0 commit comments

Comments
 (0)