{"id":13893941,"url":"https://github.com/RobTillaart/MCP_ADC","last_synced_at":"2025-07-17T08:31:44.985Z","repository":{"id":45321622,"uuid":"287734384","full_name":"RobTillaart/MCP_ADC","owner":"RobTillaart","description":"Arduino library for MCP3001 MCP3002 MCP3004 MCP3008 MCP3201 MCP3202 MCP3204 MCP3208","archived":false,"fork":false,"pushed_at":"2024-06-05T16:48:36.000Z","size":84,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-19T06:33:50.842Z","etag":null,"topics":["adc","arduino","esp32","esp8266"],"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-08-15T11:34:31.000Z","updated_at":"2024-11-04T21:37:20.000Z","dependencies_parsed_at":"2024-06-05T18:47:39.174Z","dependency_job_id":"a5d6b6d8-3eed-4ffb-a035-33f64c70f822","html_url":"https://github.com/RobTillaart/MCP_ADC","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP_ADC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP_ADC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP_ADC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP_ADC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobTillaart","download_url":"https://codeload.github.com/RobTillaart/MCP_ADC/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","esp32","esp8266"],"created_at":"2024-08-06T18:01:20.373Z","updated_at":"2024-11-24T22:30:59.384Z","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/MCP_ADC/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)\n[![Arduino-lint](https://github.com/RobTillaart/MCP_ADC/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP_ADC/actions/workflows/arduino-lint.yml)\n[![JSON check](https://github.com/RobTillaart/MCP_ADC/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MCP_ADC/actions/workflows/jsoncheck.yml)\n[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/MCP_ADC.svg)](https://github.com/RobTillaart/MCP_ADC/issues)\n\n[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MCP_ADC/blob/master/LICENSE)\n[![GitHub release](https://img.shields.io/github/release/RobTillaart/MCP_ADC.svg?maxAge=3600)](https://github.com/RobTillaart/MCP_ADC/releases)\n[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/MCP_ADC.svg)](https://registry.platformio.org/libraries/robtillaart/MCP_ADC)\n\n\n# MCP_ADC\n\nArduino library for MCP3001 MCP3002 MCP3004 MCP3008 MCP3201 MCP3202 MCP3204 MCP3208 and compatibles.\n\n\n## Description\n\nThis library reads the ADC ports of the MCP ADC convertors. \nThe chips are 1 to 8 channels, 10 or 12 bit and communicates with SPI.\nThe library supports both hardware SPI and software SPI.\n\n\n|  type     |  bits  |  channels  |  notes  |\n|:----------|:------:|:----------:|:--------|\n|  MCP3001  |   10   |      1     |  not tested yet.\n|  MCP3002  |   10   |      2     |\n|  MCP3004  |   10   |      4     |\n|  MCP3008  |   10   |      8     |\n|  MCP3201  |   12   |      1     |  test, see #13\n|  MCP3202  |   12   |      2     |\n|  MCP3204  |   12   |      4     |\n|  MCP3208  |   12   |      8     |\n\n\nCurrent version allows manual override of the hardware SPI clock as the speed is not\noptimized per ADC type. \n\nThe MCP ADC's allow a single mode read which compares voltage of a single channel against GND.\nFurthermore they allow a differential mode which compares two channels **IN+** and **IN-** \nto each other. \nIf the **IN+** is equal or below **IN-** the ADC will return 0.\n\nBuild into the library is a delta mode which is a software enhanced differential mode.\nThis delta mode can return negative values too. \n\n\n#### 0.5.0 Breaking change\n\nVersion 0.5.0 introduced a breaking change to improve handling the SPI dependency.\nThe user has to call **SPI.begin()** or equivalent before calling **MCP.begin()**.\nOptionally the user can provide parameters to the **SPI.begin(...)**\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()          |  bugfix.\n|  analogReadMultiple() |  readMultiple()  |  for consistency.\n\n\n#### 0.3.0 Breaking change\n\nThe version 0.3.0 has breaking changes in the interface. \nThe essence is removal of ESP32 specific code from the library. \nThis makes it possible to support the ESP32-S3 and other processors in the future. \nAlso it makes the library a bit simpler to maintain.\n\n\n#### Related\n\n- https://gammon.com.au/adc  tutorial about ADC's (UNO specific)\n- https://github.com/RobTillaart/ADS1x15  (12 \u0026 16 bit ADC, I2C, slow)\n- https://github.com/RobTillaart/PCF8591  (8 bit ADC + 1 bit DAC)\n- https://github.com/RobTillaart/MCP_DAC\n\n\n## Interface\n\n```cpp\n#include \"MCP_ADC.h\"\n```\n\n#### Constructors\n\n- **MCP_ADC(SPIClassRP2040 \\* mySPI = \u0026SPI)** hardware constructor RP2040\n- **MCP_ADC(SPIClass \\* mySPI = \u0026SPI)** hardware constructor other\n- **MCP_ADC(uint8_t dataIn, uint8_t dataOut, uint8_t clock)**\n\nThe derived classes have both constructors with same parameters.\n- **MCP3001(...)** constructor 10 bit ADC 1 channel.\n- **MCP3002(...)** constructor 10 bit ADC 2 channel.\n- **MCP3004(...)** constructor 10 bit ADC 4 channel.\n- **MCP3008(...)** constructor 10 bit ADC 8 channel.\n- **MCP3201(...)** constructor 12 bit ADC 1 channel.\n- **MCP3202(...)** constructor 12 bit ADC 2 channel.\n- **MCP3204(...)** constructor 12 bit ADC 4 channel.\n- **MCP3208(...)** constructor 12 bit ADC 8 channel.\n- **void begin(uint8_t select)** set select pin.\n- **uint8_t channels()** returns the number of channels.\n- **int16_t maxValue()** returns maxReading of ADC, typical 1023 or 4095.\nThis makes it easy to calculate relative measurements.\n\n\n#### Base\n\n- **int16_t read(uint8_t channel)** reads the value of a single channel.\n- **void readMultiple(uint8_t channels[], uint8_t numChannels, int16_t readings[])**\nreads multiple channels in one call. See section below.\n- **int16_t differentialRead(uint8_t channel)** reads differential between two channels.  \nCheck datasheet for details.  \nNote if the **IN+** is equal or below **IN-** this function will return 0.\n- **int16_t deltaRead(uint8_t channel)** reads differential like above however it\nwill return a negative value if **IN+** is below **IN-**.\n- **void setSPIspeed(uint32_t speed)** sets SPI clock in **Hz**, please read datasheet\nof the ADC first to get optimal speed.\n- **uint32_t getSPIspeed()** gets current speed in **Hz**.\n\n\n### Differential channel table:\n\n| Channel | diff IN+ | diff IN- | 3x02 | 3x04 | 3x08 |\n|:-------:|:--------:|:--------:|:----:|:----:|:----:|\n|   0     |    0     |    1     |  V   |  V   |  V   |\n|   1     |    1     |    0     |  V   |  V   |  V   |\n|   2     |    2     |    3     |      |  V   |  V   |\n|   3     |    3     |    2     |      |  V   |  V   |\n|   4     |    4     |    5     |      |      |  V   |\n|   5     |    5     |    4     |      |      |  V   |\n|   6     |    6     |    7     |      |      |  V   |\n|   7     |    7     |    6     |      |      |  V   |\n\n\nNote: the MCP3x01 ADC's are not included in this table, not investigated yet.\n\n\n### Debug\n\n- **bool usesHWSPI()** returns true if hardware SPI is used.\n- **uint32_t count()** returns number of channels reads since start.\n\n\n## About SPI Speed\n\nSee https://github.com/RobTillaart/MCP_ADC/issues/3\n\nThe default SPI speed is reduced to 1 MHz. \nThis is the value recommended in the datasheet for 2.7 Volt.\n\nIn a test with an ESP32 (3.3V) the library showed stable results \nat 4 MHz and at 6 MHz it was almost good.\n\nThe maximum value read at 6 MHz was 1020 instead of 1023 (MCP3008) \nwhich indicates that the last 2 bits got lost probably due to signal \ndeformation.\n\n|  Board  |  Voltage  |  safe   |   max   |\n|:-------:|:---------:|:-------:|:-------:|\n|  ESP32  |   2.7V    |  1 MHz  |  4 MHz  |\n|  UNO    |   5.0V    |  2 MHz  |  4 MHz  |\n\n\nFor hardware SPI the ESP32 uses the VSPI pins. (see ESP examples).\n\n\n## ReadMultiple()\n\nSince version 0.2.0 the **readMultiple(channels[], numChannels, readings[])** \nis added to the interface.\n(See https://github.com/RobTillaart/MCP_ADC/pull/11 - Thanks to Alex Uta).\n\nThis function allows to read multiple channels in one call, which improves \nthe performance of fetching new readings from the MCP_ADC device.\nThe amount of gain differs per platform, so run your own performance test.\n\nBesides fetching all ADC's in one call this function also allows to fetch\nthe data from a specific channel multiple times, e.g. to be averaged.\nOther patterns are possible. \nThese scenarios need still to be tested in practice.\n\nFinally **readMultiple()** can be used to read only one channel too\nby using numChannels = 1.\n\n\n## MCP3001, MCP3201 experimental\n\nSince 0.2.0 code for the MCP3201 has been added.\nThe first tests are done (see #13) which showed that the 0.2.0 implementation\nwas not correct. \nThis has been fixed in the 0.2.1 version.\n\nNote that not all function calls make sense for the MCP3201 and MCP3001 as these \ndevices only have one channel. So use the library carefully with these ADC's.\n\nFeedback is as always welcome. \n\n\n## Future\n\n#### Must\n\n- improve documentation\n- test readMultiple() scenario's\n- MCP3201 buy hardware and test\n\n#### Should\n\n- improve SWSPI for AVR \n  (code is under test for MCP23S17)\n\n\n#### Could\n\n\n#### Wont\n\n- get / setF(float A, float B) =\u003e float readF(channel)   output = A\\*value + B;\n  it actually does float mapping. As it implies the same mapping for all it might \n  not be that useful =\u003e check multiMap library.\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRobTillaart%2FMCP_ADC","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRobTillaart%2FMCP_ADC","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRobTillaart%2FMCP_ADC/lists"}