{"id":13893969,"url":"https://github.com/RobTillaart/PCF8591","last_synced_at":"2025-07-17T08:31:44.363Z","repository":{"id":45295462,"uuid":"281679177","full_name":"RobTillaart/PCF8591","owner":"RobTillaart","description":"Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC","archived":false,"fork":false,"pushed_at":"2024-04-13T09:09:30.000Z","size":32,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-19T06:33:37.721Z","etag":null,"topics":["adc","arduino","dac"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RobTillaart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"RobTillaart","custom":"https://www.paypal.me/robtillaart"}},"created_at":"2020-07-22T13:03:24.000Z","updated_at":"2024-07-25T07:47:40.000Z","dependencies_parsed_at":"2023-12-10T15:29:03.300Z","dependency_job_id":"adca4807-ba72-4794-9cdb-e345349cd101","html_url":"https://github.com/RobTillaart/PCF8591","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FPCF8591","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FPCF8591/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FPCF8591/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FPCF8591/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobTillaart","download_url":"https://codeload.github.com/RobTillaart/PCF8591/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226243893,"owners_count":17594452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adc","arduino","dac"],"created_at":"2024-08-06T18:01:20.935Z","updated_at":"2024-11-24T22:31:02.349Z","avatar_url":"https://github.com/RobTillaart.png","language":"C++","funding_links":["https://github.com/sponsors/RobTillaart","https://www.paypal.me/robtillaart"],"categories":["C++"],"sub_categories":[],"readme":"\n[![Arduino CI](https://github.com/RobTillaart/PCF8591/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)\n[![Arduino-lint](https://github.com/RobTillaart/PCF8591/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PCF8591/actions/workflows/arduino-lint.yml)\n[![JSON check](https://github.com/RobTillaart/PCF8591/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PCF8591/actions/workflows/jsoncheck.yml)\n[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/PCF8591.svg)](https://github.com/RobTillaart/PCF8591/issues)\n\n[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PCF8591/blob/master/LICENSE)\n[![GitHub release](https://img.shields.io/github/release/RobTillaart/PCF8591.svg?maxAge=3600)](https://github.com/RobTillaart/PCF8591/releases)\n[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/PCF8591.svg)](https://registry.platformio.org/libraries/robtillaart/PCF8591)\n\n\n# PCF8591\n\nArduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.\n\n\n## Description\n\n**warning** during tests I could overclock the PCF8591 chip up to 650 KHz.\nHowever it is only specified to run at 100 kHz. \nAfter some time it was getting pretty hot and it broke down. \nSo overclocking is fun but **not recommended**.\n\nPCF8591 has one 8 bit ADC on board for 4 channels. The ADC is 8 bit and quite fast.\nAt 100 KHz one gets \\\u003e 2000 reads per second for **read()** and \n\\\u003e 2000 writes per second for **write()**.  \nNote that most time is probably spend on I2C communication.\n\nThe auto-increment functionality is used in the **read4()** function.\nThe library only supports it for the mode 0 (plain ADC, no differential). \nThe **lastRead()** function is needed to get access to the values.\nFirst tests shows it is 2.6 x faster than 4 individual reads.\n\n\n#### 0.4.0 Breaking change\n\nThe version 0.4.0 has breaking changes in the interface. \nThe rationale is that the programming environment of the **Arduino ESP32 S3** \nboard uses a remapping by means of the include file **io_pin_remap.h**.\nThis file remaps the pins of several core Arduino functions. \nThe remapping is implemented by #define macros and these implement \"hard\" text \nreplacements without considering context. \nThe effect is that methods from this class (and several others) which have the same \nname as those Arduino core functions will be remapped into something not working.\n\nThe following library functions have been renamed:\n\n|  old name        |  new name    |  notes  |\n|:-----------------|:-------------|:--------|\n|  analogRead()    |  read()      |\n|  analogWrite()   |  write()     |\n|  pinMode()       |  pinMode1()  |\n|  digitalRead()   |  read1()     |\n|  digitalWrite()  |  write1()    |\n\n\n#### 0.3.0 Breaking change\n\nVersion 0.3.0 introduced a breaking change.\nYou cannot set the pins in **begin()** any more.\nThis reduces the dependency of processor dependent Wire implementations.\nThe user has to call **Wire.begin()** and can optionally set the Wire pins \nbefore calling **begin()**.\n\n\n#### Related\n\n- https://github.com/RobTillaart/MCP_DAC\n- https://github.com/RobTillaart/MCP_ADC\n\n\n\n## Interface\n\n```cpp\n#include \"PCF8591.h\"\n```\n\n#### Constructor\n\n- **PCF8591(uint8_t address = 0x48, TwoWire \\*wire = \u0026Wire)** constructor with I2C address.\nDefault is 0x48, optional set the WireN I2C bus.\n- **bool begin(uint8_t value = 0)** Set initial value for the DAC.\nReturns false if address out of range, or if it cannot be seen on the I2C bus.\nReturns **true** if successful.\n- **bool isConnected()** test to see if address can be reached on the I2C bus.\n- **uint8_t getAddress()** returns address set in constructor.\n\n\n#### ADC channels\n\nThe PCF8591 has four 8 bit ADC channels. Values = 0..255.\n\n- **void enableINCR()** used in read4(). \nCould become private in the future.\n- **void disableINCR()** idem.\n- **bool isINCREnabled()** idem.\n- **uint8_t read(uint8_t channel, uint8_t mode = 0)** returns value of the analogue channel.\nReturn 0 in case of an error, so check **lastError()** to be verify validity.\nDefault mode is PCF8591_FOUR_SINGLE_CHANNEL, see table below.\nFor details comparator modes see datasheet figure 4 page 6.\n- **uint8_t read4()** read all 4 channels in one call.\nWorks in PCF8591_FOUR_SINGLE_CHANNEL mode only.\nUses **enableINCR()** to do that efficiently. \nIt is about 2.6 times faster than 4 individual **read()** calls.\nThe latter allows for optimized timing per channel and the order \nin which the channels are read.\nUse **lastRead()** to access the 4 values.\nReturns **PCF8591_OK** or an error code if appropriate.\n- **uint8_t lastRead(uint8_t channel)** get last read value from cache.  \nThis cache is filled both by **read()** and **read4()**. \nSee example sketch.\n\n\n|  ADC mode                     |  Value  |  Notes    |\n|:------------------------------|:-------:|:----------|\n|  PCF8591_FOUR_SINGLE_CHANNEL  |  0x00   |  default  |\n|  PCF8591_THREE_DIFFERENTIAL   |  0x01   |\n|  PCF8591_MIXED                |  0x02   |\n|  PCF8591_TWO_DIFFERENTIAL     |  0x03   |\n\n\n#### Comparator\n\nSince 0.2.0 four direct comparator calls are added to make the code more explicit.\nThese four calls are in fact wrappers around the **read()**.\nThese still need to be tested as I had no hardware available.\n\n- **int readComparator_01()** == read(channel = 0, mode = 3);\n- **int readComparator_23()** == read(channel = 1, mode = 3);\n- **int readComparator_03()** == read(channel = 0, mode = 1);\n- **int readComparator_13()** == read(channel = 1, mode = 1);\n\nThe values of the comparator calls are cached and can be accessed with **lastRead()**.\nBe sure to select the right channel for **lastRead()**.\n\nNote: these functions do never use PCF8591_MIXED (2) mode.\n\n\n#### DAC channel\n\nThe PCF8591 has one 8 bit DAC. output value 0..255 == 0..Vref Volts (datasheet).\n\n- **void enableDAC()** switch on the analogue output.\n- **void disableDAC()** switch off the analogue output (high impedance). Sort of energy saving mode.\n- **bool isDACEnabled()** check the modus operandi.\n- **bool write(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage.\nNote, this is a real voltage not a PWM signal like **analogWrite()** on an UNO.\n- **uint8_t lastWrite()** get last value written (from cache).\n\n\n#### Error codes\n\n- **int lastError()** always check this value after a read / write to see if it was OK (== 0).\nAfter the read the error value is reset to OK.\n\n|  Error code             |  Value  |  Notes  |\n|:------------------------|:-------:|:--------|\n|  PCF8591_OK             |  0x00   |\n|  PCF8591_PIN_ERROR      |  0x81   |\n|  PCF8591_I2C_ERROR      |  0x82   |\n|  PCF8591_MODE_ERROR     |  0x83   |\n|  PCF8591_CHANNEL_ERROR  |  0x84   |\n|  PCF8591_ADDRESS_ERROR  |  0x85   |\n\n\n## Operations\n\nSee examples.\n\n\n## Future\n\n#### Must\n\n- improve documentation\n- test / verify comparator calls with hardware.\n  - datasheet (par.8.2 figure 4)\n\n#### Should\n\n- add examples \n  - for comparator calls.\n  - schema?\n- add examples boards\n  - ESP32, RP2040 (pins)\n\n#### Could\n\n\n#### Wont\n\n\n## Support\n\nIf you appreciate my libraries, you can support the development and maintenance.\nImprove the quality of the libraries by providing issues and Pull Requests, or\ndonate through PayPal or GitHub sponsors.\n\nThank you,\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRobTillaart%2FPCF8591","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRobTillaart%2FPCF8591","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRobTillaart%2FPCF8591/lists"}