Home › Forums › The libraries hosted on the site › PCF8591 i2c analog expander › MicroPython I2C PCF8591 round value problem Raspberry Pi Pico
Tagged: Student Assignment Help online
- This topic has 15 replies, 3 voices, and was last updated 1 year, 6 months ago by
jiangzhiyu233.
-
AuthorPosts
-
-
5 May 2023 at 16:59 #25608
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.
achimCiao,
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 -
16 May 2023 at 09:10 #25741
Please, can you reupload the file.
Thanks -
16 May 2023 at 11:36 #25750
# # PCF8591 Analog GPIO Port Expand # # AUTHOR: Renzo Mischianti # Website: www.mischianti.org # VERSION: 0.0.1 # # Description: # This script read one channel for time of pcf8591 # # +---------------+ # 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 # # Porting of PCF8591 library for Arduino # http://mischianti.org/pcf8591-i2c-analog-i-o-expander/ # from machine import I2C, Pin from PCF8591 import PCF8591 import utime # Initialize the I2C bus i2c = I2C(0, scl=Pin(21), sda=Pin(20)) # Adjust the pins and frequency as needed for your board # Initialize the PCF8591 pcf8591 = PCF8591(0x48, i2c) # Adjust the address if needed if pcf8591.begin(): print("PCF8591 found") print("AIN0 ", pcf8591.analog_read(PCF8591.AIN0)) # You can use PCF8591.CHANNEL_0 print("AIN1 ", pcf8591.analog_read(PCF8591.AIN1)) print("AIN2 ", pcf8591.analog_read(PCF8591.AIN2)) print("AIN3 ", pcf8591.analog_read(PCF8591.AIN3))
Das ist der gesamte Code. Es wird nur 0, 64, 128 oder 192 ausgegeben
-
16 May 2023 at 11:59 #25751
Probably I undestand, can you try with analog_read_all?
Thanks Renzo -
16 May 2023 at 13:31 #25754
# # 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]))
Damit wird nur A0 angezeigt, A1, A2 und A3 fehlen
-
16 May 2023 at 13:34 #25755
# # PCF8591 Analog GPIO Port Expand # # AUTHOR: Renzo Mischianti # Website: www.mischianti.org # VERSION: 0.0.1 # # Description: # This script read all analog input of pcf8591 # # +---------------+ # 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 # # Porting of PCF8591 library for Arduino # http://mischianti.org/pcf8591-i2c-analog-i-o-expander/ # from machine import I2C, Pin import utime from PCF8591 import PCF8591 # Initialize the I2C bus i2c = I2C(0, scl=Pin(21), sda=Pin(20)) # Adjust the pins and frequency as needed for your board # Initialize the PCF8591 pcf8591 = PCF8591(0x48, i2c) # Adjust the address if needed if pcf8591.begin(): print("PCF8591 found") # Main loop while True: # Read all analog input channels ain0, ain1, ain2, ain3 = pcf8591.analog_read_all() # Print the results print("AIN0:", ain0, "AIN1:", ain1, "AIN2:", ain2, "AIN3:", ain3) # Wait for 1 second utime.sleep(1)
Ist dieser Code
-
16 May 2023 at 13:58 #25756
Can you post the REPL console output.
Thanks RM -
16 May 2023 at 17:09 #25758
Hi,
I redo all the tests if you get the library from pippip install pcf8591-library
Bye Renzo
-
16 May 2023 at 17:29 #25759
Wie kann ich eine pip installieren wenn ich mit Micropython auf dem Raspberry Pi Pico arbeite. Ist kein Raspberry.
How can I install pip when working with MicroPython on the Raspberry Pi Pico? It’s not a Raspberry Pi.
-
16 May 2023 at 17:52 #25761
What IDE do you use?
-
5 November 2023 at 00:26 #28003
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 -
5 November 2023 at 00:35 #28004
The AOUT port is good, analog/voltage write can output correct value, but there are some problem with the analog/voltage reading of all AIN port.
-
6 November 2023 at 10:03 #28033
Hi jiangzhiyu233,
I retry the code and I don’t have that behavior, do you use the example provided in github??
Bye Renzo -
6 November 2023 at 20:51 #28043
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")
And part of result:
Written: 49, Read: 128 Written: 50, Read: 128 Written: 51, Read: 128 Written: 52, Read: 128 Written: 53, Read: 128 Written: 54, Read: 128 Written: 55, Read: 128 Written: 56, Read: 128 Written: 57, Read: 128 Written: 58, Read: 128 Written: 59, Read: 128 Written: 60, Read: 128 Written: 61, Read: 128 Written: 62, Read: 128 Written: 63, Read: 128 Written: 64, Read: 128
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 ? -
7 November 2023 at 17:28 #28044
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 -
8 November 2023 at 15:34 #28096
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:Written: 0, Read: 128 Written: 1, Read: 184 Written: 2, Read: 184 Written: 3, Read: 184 Written: 4, Read: 0 Written: 5, Read: 0 Written: 6, Read: 0 Written: 7, Read: 0 Written: 8, Read: 128 Written: 9, Read: 184 Written: 10, Read: 0 Written: 11, Read: 184 Written: 12, Read: 0 Written: 13, Read: 0 Written: 14, Read: 184 Written: 15, Read: 0 Written: 16, Read: 0 Written: 17, Read: 0 Written: 18, Read: 0 Written: 19, Read: 184 Written: 20, Read: 0 Written: 21, Read: 128 Written: 22, Read: 184 Written: 23, Read: 0 Written: 24, Read: 0 Written: 25, Read: 0 Written: 26, Read: 184 Written: 27, Read: 184 Written: 28, Read: 0 Written: 29, Read: 184 Written: 30, Read: 184 Written: 31, Read: 0 Written: 32, Read: 184 Written: 33, Read: 0 Written: 34, Read: 0 Written: 35, Read: 0 Written: 36, Read: 0 Written: 37, Read: 0 Written: 38, Read: 0 Written: 39, Read: 184 Written: 40, Read: 184 Written: 41, Read: 0 Written: 42, Read: 128 Written: 43, Read: 0 Written: 44, Read: 184 Written: 45, Read: 0 Written: 46, Read: 0 Written: 47, Read: 0 Written: 48, Read: 184 Written: 49, Read: 0 Written: 50, Read: 0 Written: 51, Read: 0 Written: 52, Read: 0 Written: 53, Read: 0 Written: 54, Read: 0 Written: 55, Read: 0 Written: 56, Read: 184 Written: 57, Read: 0 Written: 58, Read: 184 Written: 59, Read: 0 Written: 60, Read: 0 Written: 61, Read: 0 Written: 62, Read: 184 Written: 63, Read: 184 Written: 64, Read: 184 Written: 65, Read: 128 Written: 66, Read: 184 Written: 67, Read: 0 Written: 68, Read: 184 Written: 69, Read: 0 Written: 70, Read: 184 Written: 71, Read: 128 Written: 72, Read: 184 Written: 73, Read: 128 Written: 74, Read: 0 Written: 75, Read: 0 Written: 76, Read: 0 Written: 77, Read: 184 Written: 78, Read: 184 Written: 79, Read: 0 Written: 80, Read: 0 Written: 81, Read: 0 Written: 82, Read: 0 Written: 83, Read: 0 Written: 84, Read: 0 Written: 85, Read: 0 Written: 86, Read: 0 Written: 87, Read: 184 Written: 88, Read: 0 Written: 89, Read: 184 Written: 90, Read: 0 Written: 91, Read: 0 Written: 92, Read: 0 Written: 93, Read: 0 Written: 94, Read: 0 Written: 95, Read: 0 Written: 96, Read: 184 Written: 97, Read: 184 Written: 98, Read: 184 Written: 99, Read: 0 Written: 100, Read: 128 Written: 101, Read: 184 Written: 102, Read: 0 Written: 103, Read: 0 Written: 104, Read: 184 Written: 105, Read: 128 Written: 106, Read: 0 Written: 107, Read: 184 Written: 108, Read: 0 Written: 109, Read: 0 Written: 110, Read: 0 Written: 111, Read: 128 Written: 112, Read: 0 Written: 113, Read: 184 Written: 114, Read: 184 Written: 115, Read: 0 Written: 116, Read: 128 Written: 117, Read: 184 Written: 118, Read: 128 Written: 119, Read: 0 Written: 120, Read: 184 Written: 121, Read: 184 Written: 122, Read: 184 Written: 123, Read: 0 Written: 124, Read: 0 Written: 125, Read: 184 Written: 126, Read: 128 Written: 127, Read: 184 Written: 128, Read: 0 Written: 129, Read: 0 Written: 130, Read: 184 Written: 131, Read: 0 Written: 132, Read: 0 Written: 133, Read: 0 Written: 134, Read: 0 Written: 135, Read: 0 Written: 136, Read: 184 Written: 137, Read: 0 Written: 138, Read: 0 Written: 139, Read: 0 Written: 140, Read: 0 Written: 141, Read: 184 Written: 142, Read: 128 Written: 143, Read: 0 Written: 144, Read: 184 Written: 145, Read: 184 Written: 146, Read: 184 Written: 147, Read: 0 Written: 148, Read: 0 Written: 149, Read: 0 Written: 150, Read: 0 Written: 151, Read: 184 Written: 152, Read: 0 Written: 153, Read: 184 Written: 154, Read: 0 Written: 155, Read: 184 Written: 156, Read: 0 Written: 157, Read: 0 Written: 158, Read: 0 Written: 159, Read: 0 Written: 160, Read: 184 Written: 161, Read: 0 Written: 162, Read: 0 Written: 163, Read: 184 Written: 164, Read: 0 Written: 165, Read: 0 Written: 166, Read: 0 Written: 167, Read: 0 Written: 168, Read: 0 Written: 169, Read: 184 Written: 170, Read: 128 Written: 171, Read: 0 Written: 172, Read: 0 Written: 173, Read: 128 Written: 174, Read: 184 Written: 175, Read: 184 Written: 176, Read: 184 Written: 177, Read: 0 Written: 178, Read: 0 Written: 179, Read: 184 Written: 180, Read: 0 Written: 181, Read: 184 Written: 182, Read: 184 Written: 183, Read: 0 Written: 184, Read: 0 Written: 185, Read: 128 Written: 186, Read: 0 Written: 187, Read: 128 Written: 188, Read: 0 Written: 189, Read: 128 <strong>Written: 190, Read: 189</strong> Written: 191, Read: 128 <strong>Written: 192, Read: 192</strong> Written: 193, Read: 128 Written: 194, Read: 128 Written: 195, Read: 0 Written: 196, Read: 128 <strong>Written: 197, Read: 196</strong> Written: 198, Read: 128 Written: 199, Read: 0 <strong>Written: 200, Read: 198</strong> Written: 201, Read: 128 Written: 202, Read: 0 Written: 203, Read: 128 Written: 204, Read: 0 Written: 205, Read: 128 Written: 206, Read: 0 Written: 207, Read: 128 Written: 208, Read: 0 Written: 209, Read: 128 Written: 210, Read: 0 Written: 211, Read: 0 Written: 212, Read: 128 Written: 213, Read: 0 Written: 214, Read: 128 Written: 215, Read: 0 <strong>Written: 216, Read: 213 Written: 217, Read: 214</strong> Written: 218, Read: 128 <strong>Written: 219, Read: 216 Written: 220, Read: 217</strong> Written: 221, Read: 0 <strong>Written: 222, Read: 219</strong> Written: 223, Read: 128 Written: 224, Read: 0 <strong>Written: 225, Read: 221</strong> Written: 226, Read: 0 Written: 227, Read: 128 <strong>Written: 228, Read: 224</strong> Written: 229, Read: 128 <strong>Written: 230, Read: 226</strong> Written: 231, Read: 128 Written: 232, Read: 128 <strong>Written: 233, Read: 228 Written: 234, Read: 229 Written: 235, Read: 230</strong> Written: 236, Read: 0 Written: 237, Read: 128 <strong>Written: 238, Read: 232</strong> Written: 239, Read: 128 <strong>Written: 240, Read: 233 Written: 241, Read: 233</strong> Written: 242, Read: 128 Written: 243, Read: 0 Written: 244, Read: 0 Written: 245, Read: 0 Written: 246, Read: 0 Written: 247, Read: 0 Written: 248, Read: 0 Written: 249, Read: 0 <strong>Written: 250, Read: 234</strong> Written: 251, Read: 0 Written: 252, Read: 0 Written: 253, Read: 128 <strong>Written: 254, Read: 234</strong> Written: 255, Read: 234
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.
-
-
AuthorPosts
- You must be logged in to reply to this topic.