Hallo
Leider in Deutsch.
Schliesse den PCF8591 mit dem I2C Bus an einen Paspberry Pi Pico und Micropython (ESP32) an. Der PCF wird erkannt und Daten ausgelesen. Es wird aber nur 64, 128 und 192 dargestellt. Alle Zwischenwerte werden nicht erkannt. Es wird wahrscheinlich nur B7 und B6 gelesen. Kann aber keinen Grund dazu finden.
achim
Ciao,
Purtroppo, in tedesco:
Collega il PCF8591 al bus I2C di un Raspberry Pi Pico e a Micropython (ESP32). Il PCF viene riconosciuto e i dati vengono letti. Tuttavia, vengono visualizzati solo 64, 128 e 192. Tutti i valori intermedi non vengono riconosciuti. Probabilmente vengono letti solo B7 e B6. Non riesco però a trovare una ragione specifica.
Achim
#
# PCF8591 Analog GPIO Port Expand
#
# AUTHOR: Renzo Mischianti
# Website: www.mischianti.org
# VERSION: 0.0.1
#
#
# Porting of PCF8591 library for Arduino
# http://mischianti.org/pcf8591-i2c-analog-i-o-expander/
#
# +---------------+
# AIN0 (1) | * 1 U 16 | VDD
# AIN1 (2) | * 2 15 | AOUT
# AIN2 (3) | * 3 14 | VREF
# AIN3 (4) | * 4 13 | AGND
# A0 (5) | * 5 12 | EXT
# A1 (6) | * 6 11 | OSC
# A2 (7) | * 7 10 | SCL
# VSS (8) | * 8 9 | SDA
# +---------------+
#
# AIN0: Analog input channel 0
# AIN1: Analog input channel 1
# AIN2: Analog input channel 2
# AIN3: Analog input channel 3
# A0: Address input 0
# A1: Address input 1
# A2: Address input 2
# VSS: Ground
# SDA: Serial data line (I2C)
# SCL: Serial clock line (I2C)
# OSC: Oscillator output
# EXT: External trigger input
# AGND: Analog ground
# VREF: Voltage reference input
# AOUT: Analog output
# VDD: Power supply
#
#
# The MIT License (MIT)
#
# Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
#
# You may copy, alter and reuse this code in any way you like, but please leave
# reference to www.mischianti.org in your comments if you redistribute this code.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import utime
from machine import I2C, Pin
# AIN0 = CHANNEL0 = 0b00000000
# AIN1 = CHANNEL1 = 0b00000001
# AIN2 = CHANNEL2 = 0b00000010
# AIN3 = CHANNEL3 = 0b00000011
class PCF8591:
AIN0 = CHANNEL0 = 0b00000000
AIN1 = CHANNEL1 = 0b00000001
AIN2 = CHANNEL2 = 0b00000010
AIN3 = CHANNEL3 = 0b00000011
AUTOINCREMENT_READ = 0b00000100
SINGLE_ENDED_INPUT = 0b00000000
TREE_DIFFERENTIAL_INPUT = 0b00010000
TWO_SINGLE_ONE_DIFFERENTIAL_INPUT = 0b00100000
TWO_DIFFERENTIAL_INPUT = 0b00110000
ENABLE_OUTPUT = 0b01000000
DISABLE_OUTPUT = 0b00000000
OUTPUT_MASK = 0b01000000
def __init__(self, address, i2c=None, i2c_id=0, sda=None, scl=None):
if i2c:
self._i2c = i2c
elif sda and scl:
self._i2c = I2C(i2c_id, scl=Pin(scl), sda=Pin(sda))
else:
raise ValueError('Either i2c or sda and scl must be provided')
self._address = address
self._output_status = self.DISABLE_OUTPUT # default
# Kontrolle ob Slave vorhanden ist
def begin(self):
if self._i2c.scan().count(self._address) == 0:
# raise OSError('PCF8591 not found at I2C address {:#x}'.format(self._address))
return False # Slave nicht vorhanden
else:
return True # Slave vorhanden
def analog_read_all(self, read_type=SINGLE_ENDED_INPUT):
operation = self.AUTOINCREMENT_READ | read_type | (self._output_status & self.OUTPUT_MASK)
self._i2c.writeto(self._address, bytearray([operation]))
utime.sleep_ms(1)
# data = self._i2c.readfrom(self._address, 5)
data = []
self._i2c.readfrom(self._address, 1)
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), "big"))
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
data.append(int.from_bytes(self._i2c.readfrom(self._address, 1), 'big'))
return data[0], data[1], data[2], data[3]
def analog_read(self, channel, read_type = SINGLE_ENDED_INPUT):
operation = channel | read_type | (self._output_status & self.OUTPUT_MASK)
self._i2c.writeto(self._address, bytearray([operation]))
utime.sleep_ms(1)
data = self._i2c.readfrom(self._address, 2)
return data[1]
#def voltage_read(self, channel, reference_voltage=3.3):
def voltage_read(self, channel, reference_voltage=5.0):
voltage_ref = reference_voltage
ana = self.analog_read(channel, self.SINGLE_ENDED_INPUT)
return ana * voltage_ref / 255
def voltage_write(self, value, reference_voltage=3.3):
ana = value * 255 / reference_voltage
self.analog_write(ana)
def analog_write(self, value):
if value > 255 or value < 0:
Exception('Value must be between 0 and 255')
self._output_status = self.ENABLE_OUTPUT
self._i2c.writeto(self._address, bytearray([self.ENABLE_OUTPUT, value]))
def disable_output(self):
self._output_status = self.DISABLE_OUTPUT
self._i2c.writeto(self._address, bytearray([self.DISABLE_OUTPUT]))
Hello!
I encountered the same problem, although I used the updated version 0.02, it seems that the situation has not changed: my pcf8591 still only reads three values 64, 128, 192. The microcontroller is a Raspberry Pi pico w, Using micropython and ThonyyIDE, is there any solution? Thank you so much
Ahh, I did not directly test with the example in github. I used this library in my project and find some problem. And wrote a small program to test:
from machine import I2C, Pin
from PCF8591 import PCF8591
i2c = I2C(0, scl=Pin(9), sda=Pin(8))
pcf8591 = PCF8591(0x48, i2c)
if not pcf8591.begin():
print("PCF8591 not found")
else:
with open('test.txt', 'w') as file:
for value in range(256):
pcf8591.analog_write(value)
utime.sleep_ms(10)
read_value = pcf8591.analog_read(pcf8591.AIN0)
file.write(f"{value}, {read_value}\n")
print(f"Written: {value}, Read: {read_value}")
utime.sleep_ms(10)
print("TEST FINISHED")
During the test, I connected the AOUT and AIN0 ports and no error in wiring. And when the read and write functions are tested separately(both analog and voltage), the oscilloscope can also see the changes in the AOUT port. However, the AIN port cannot correctly read the waveform generated by another function generator.
I also tried whether it was a problem with the Raspberry Pi pico, but when I connected the AOUT port of the PCF8591 to the pico’s internal AD converter, it could read the changes in the waveform normally.
If there is no problem with the program, maybe there is some conflict in the hardware? Between PCF8591 and Raspberry Pi picow ?
Hi jiangzhiyu233,
I executed your code and I replicated the issue. It seems that there are some problems when writing and reading at the same time.
I do a little fix, try It, and give me feedback.
Thanks Renzo
Hello Renzo!
I tried running the modified library (and also tried it on another set of Raspberry Pi picow and PCF8591). PCF8591 still couldn’t correctly read the value which generated by itself, but the value it read are not fixed now.
Here is the result:
When the value read by analog_read is greater than 184 (sometimes change a little), PCF8591 will read correctly from time to time (I marked it; and the AIN port can only read a maximum of 233 or 234), but most of the time it is still 0 or 128 . When the number read by the AIN port is less than 184, it will only read 0, 128 or 184 (I think 184 should be when PCF8591 tries to read ‘correctly’). The waveform of the AOUT port can still be read normally by the oscilloscope.
Maintaining a repository (or site or forum) is a lot like tending to a garden - it requires constant care and attention to keep it thriving. If you're a skilled gardener (or coder!) and want to help keep our repository blooming, we'd love to have you on board! We're also looking for talented writers and forum moderators to help us grow our community. Interested in joining our team? Don't hesitate to reach out and let us know how you can contribute!
Are you a fan of electronics or programming? Share your knowledge with others, write a simple tutorial or how to make a great project Contact me: share_your_ideas@mischianti.org
The content displayed on this website is protected under a CC BY-NC-ND license. Visitors are prohibited from using, redistributing, or altering any content from this website for commercial purposes, including generating revenue through advertising. Any unauthorized use is a violation of the license terms and legal action may be taken against individuals or entities found to be in violation.
You must also provide the link to the source.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.