├── library.properties ├── README.md ├── library.json ├── LICENSE ├── examples ├── Unit_ADC_M5Core2 │ └── Unit_ADC_M5Core2.ino ├── ADC_M5StickC │ └── ADC_M5StickC.ino ├── Unit_ADC_M5Core │ └── Unit_ADC_M5Core.ino ├── ADC_M5StickCPlus │ └── ADC_M5StickCPlus.ino └── Unit_ADC_M5Atom │ └── Unit_ADC_M5Atom.ino └── src ├── M5_ADS1100.cpp └── M5_ADS1100.h /library.properties: -------------------------------------------------------------------------------- 1 | name=M5-ADS1100 2 | version=0.0.1 3 | author=M5Stack 4 | maintainer=M5Stack 5 | sentence=Library for Unit & Hat ADC 6 | paragraph=See more on https://docs.m5stack.com/en/unit/adc or https://docs.m5stack.com/en/hat/hat-adc 7 | category=Device Control 8 | url=https://github.com/m5stack/M5-ADS1100 9 | architectures=esp32 10 | includes=M5_ADS1100.h 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # M5-ADS1100 2 | 3 | ## Overview 4 | 5 | ### SKU:U013/U069 6 | 7 | Contains M5Stack **Unit & Hat ADC** related case programs. 8 | 9 | 10 | ## Related Link 11 | 12 | [Document & Datasheet - M5Unit-ADC](https://docs.m5stack.com/en/unit/adc) 13 | 14 | [Document & Datasheet - M5Hat-ADC](https://docs.m5stack.com/en/hat/hat-adc) 15 | 16 | ## License 17 | 18 | [Unit & Hat ADC - MIT](LICENSE) 19 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "M5-ADS1100", 3 | "description": "Library for Unit & Hat ADC", 4 | "keywords": "ADS1100", 5 | "authors": { 6 | "name": "M5Stack", 7 | "url": "http://www.m5stack.com" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/m5stack/M5-ADS1100.git" 12 | }, 13 | "version": "0.0.1", 14 | "frameworks": "arduino", 15 | "platforms": "espressif32" 16 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 M5Stack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /examples/Unit_ADC_M5Core2/Unit_ADC_M5Core2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5Core2 sample source code 5 | * 配套 M5Core2 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/adc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/adc 8 | * 9 | * Product: ADC. A/D转换器 10 | * Date: 2022/7/11 11 | ******************************************************************************* 12 | Please connect to Port A,Use ADC Unit to convert 0 ~ 12V analog voltage into 13 | 16-bit data and display it on the screen. 请连接端口A,利用ADC单元将0 ~ 14 | 12V模拟电压转换成16位数据显示在屏幕上。 15 | */ 16 | 17 | #include 18 | #include "M5_ADS1100.h" 19 | 20 | ADS1100 ads; 21 | 22 | void setup(void) { 23 | M5.begin(); // Init M5Core2. 初始化M5Core2 24 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 25 | 26 | // The address can be changed making the option of connecting multiple 27 | // devices 地址可以改变,以连接多个设备 28 | ads.getAddr_ADS1100( 29 | ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) 30 | 31 | // The ADC gain (PGA). ADC增益(PGA) 32 | ads.setGain(GAIN_ONE); // 1x gain(default) 33 | // ads.setGain(GAIN_TWO); // 2x gain 34 | // ads.setGain(GAIN_FOUR); // 4x gain 35 | // ads.setGain(GAIN_EIGHT); // 8x gain 36 | 37 | // Device operating mode. 设备工作模式 38 | ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) 39 | // ads.setMode(MODE_SINGLE); // Single-conversion mode 40 | 41 | // Data rate. 数据速率 42 | ads.setRate(RATE_8); // 8SPS (default) 43 | // ads.setRate(RATE_16); // 16SPS 44 | // ads.setRate(RATE_32); // 32SPS 45 | // ads.setRate(RATE_128); // 128SPS 46 | 47 | ads.setOSMode( 48 | OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换 49 | 50 | ads.begin(); // Sets up the Hardware. 设置硬件 51 | } 52 | 53 | void loop(void) { 54 | byte error; 55 | int8_t address; 56 | 57 | address = ads.ads_i2cAddress; 58 | Wire.beginTransmission(address); 59 | error = Wire.endTransmission(); 60 | if (error == 0) // If the device is connected. 如果连接上设备 61 | { 62 | int16_t result; 63 | result = ads.Measure_Differential(); 64 | M5.Lcd.fillScreen(BLACK); 65 | char data[20] = {0}; 66 | sprintf(data, "%d", result); 67 | M5.Lcd.drawCentreString(data, 160, 100, 4); 68 | } else { 69 | M5.Lcd.drawString("No Found ADC sensor.", 20, 100, 2); 70 | } 71 | delay(1000); 72 | } -------------------------------------------------------------------------------- /examples/ADC_M5StickC/ADC_M5StickC.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickC sample source code 5 | * 配套 M5StickC 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/adc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/adc 8 | * 9 | * Product: ADC. A/D转换器 10 | * Date: 2022/7/11 11 | ******************************************************************************* 12 | Please connect to Port,Use ADC Unit to convert 0 ~ 12V analog voltage into 13 | 16-bit data and display it on the screen. 14 | 请连接端口,利用ADC单元将0 ~12V模拟电压转换成16位数据显示在屏幕上。 15 | */ 16 | 17 | #include 18 | #include "M5_ADS1100.h" 19 | 20 | ADS1100 ads; 21 | 22 | void setup(void) { 23 | M5.begin(); // Init M5StickC. 初始化M5StickC 24 | M5.Lcd.setRotation(3); // Rotating display. 旋转显示屏 25 | 26 | // The address can be changed making the option of connecting multiple 27 | // devices 地址可以改变,以连接多个设备 28 | ads.getAddr_ADS1100( 29 | ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) 30 | 31 | // The ADC gain (PGA). ADC增益(PGA) 32 | ads.setGain(GAIN_ONE); // 1x gain(default) 33 | // ads.setGain(GAIN_TWO); // 2x gain 34 | // ads.setGain(GAIN_FOUR); // 4x gain 35 | // ads.setGain(GAIN_EIGHT); // 8x gain 36 | 37 | // Device operating mode. 设备工作模式 38 | ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) 39 | // ads.setMode(MODE_SINGLE); // Single-conversion mode 40 | 41 | // Data rate. 数据速率 42 | ads.setRate(RATE_8); // 8SPS (default) 43 | // ads.setRate(RATE_16); // 16SPS 44 | // ads.setRate(RATE_32); // 32SPS 45 | // ads.setRate(RATE_128); // 128SPS 46 | 47 | ads.setOSMode( 48 | OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换 49 | 50 | ads.begin(); // Sets up the Hardware. 设置硬件 51 | M5.Lcd.print("ADC"); 52 | } 53 | 54 | void loop(void) { 55 | byte error; 56 | int8_t address; 57 | 58 | address = ads.ads_i2cAddress; 59 | Wire.beginTransmission(address); 60 | error = Wire.endTransmission(); 61 | if (error == 0) // If the device is connected. 如果连接上设备 62 | { 63 | int16_t result; 64 | result = ads.Measure_Differential(); 65 | M5.Lcd.fillScreen(BLACK); 66 | char data[20] = {0}; 67 | sprintf(data, "%d", result); 68 | M5.Lcd.drawCentreString(data, 0, 20, 4); 69 | } else { 70 | M5.Lcd.setCursor(0, 40); 71 | M5.Lcd.drawString("No Found ADC sensor.", 0, 20); 72 | } 73 | delay(1000); 74 | } -------------------------------------------------------------------------------- /examples/Unit_ADC_M5Core/Unit_ADC_M5Core.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5Core sample source code 5 | * 配套 M5Core 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/adc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/adc 8 | * 9 | * Describe: ADC. A/D转换器 10 | * Date: 2022/7/11 11 | ******************************************************************************* 12 | Please connect to Port A,Use ADC Unit to convert 0 ~ 12V analog voltage into 13 | 16-bit data and display it on the screen. 请连接端口A,利用ADC单元将0 ~ 14 | 12V模拟电压转换成16位数据显示在屏幕上。 15 | */ 16 | 17 | #include 18 | #include "M5_ADS1100.h" 19 | 20 | ADS1100 ads; 21 | 22 | void setup(void) { 23 | M5.begin(); // Init M5Stack. 初始化M5Stack 24 | M5.Power.begin(); // Init power 初始化电源模块 25 | M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2 26 | 27 | // The address can be changed making the option of connecting multiple 28 | // devices 地址可以改变,以连接多个设备 29 | ads.getAddr_ADS1100( 30 | ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) 31 | 32 | // The ADC gain (PGA). ADC增益(PGA) 33 | ads.setGain(GAIN_ONE); // 1x gain(default) 34 | // ads.setGain(GAIN_TWO); // 2x gain 35 | // ads.setGain(GAIN_FOUR); // 4x gain 36 | // ads.setGain(GAIN_EIGHT); // 8x gain 37 | 38 | // Device operating mode. 设备工作模式 39 | ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) 40 | // ads.setMode(MODE_SINGLE); // Single-conversion mode 41 | 42 | // Data rate. 数据速率 43 | ads.setRate(RATE_8); // 8SPS (default) 44 | // ads.setRate(RATE_16); // 16SPS 45 | // ads.setRate(RATE_32); // 32SPS 46 | // ads.setRate(RATE_128); // 128SPS 47 | 48 | ads.setOSMode( 49 | OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换 50 | 51 | ads.begin(); // Sets up the Hardware. 设置硬件 52 | } 53 | 54 | void loop(void) { 55 | byte error; 56 | int8_t address; 57 | 58 | address = ads.ads_i2cAddress; 59 | Wire.beginTransmission(address); 60 | error = Wire.endTransmission(); 61 | if (error == 0) // If the device is connected. 如果连接上设备 62 | { 63 | int16_t result; 64 | result = ads.Measure_Differential(); 65 | M5.Lcd.fillScreen(BLACK); 66 | char data[20] = {0}; 67 | sprintf(data, "%d", result); 68 | M5.Lcd.drawCentreString(data, 160, 100, 4); 69 | } else { 70 | M5.Lcd.drawString("No Found ADC sensor.", 20, 100, 2); 71 | } 72 | delay(1000); 73 | } -------------------------------------------------------------------------------- /examples/ADC_M5StickCPlus/ADC_M5StickCPlus.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with M5StickCPlus sample source code 5 | * 配套 M5StickCPlus 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/adc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/adc 8 | * 9 | * Product: ADC. A/D转换器 10 | * Date: 2022/7/11 11 | ******************************************************************************* 12 | Please connect to Port,Use ADC Unit to convert 0 ~ 12V analog voltage into 13 | 16-bit data and display it on the screen. 14 | 请连接端口,利用ADC单元将0 ~12V模拟电压转换成16位数据显示在屏幕上。 15 | */ 16 | 17 | #include 18 | #include "M5_ADS1100.h" 19 | 20 | ADS1100 ads; 21 | 22 | void setup(void) { 23 | M5.begin(); // Init M5StickCPlus. 初始化M5StickCPlus 24 | M5.Lcd.setRotation(3); // Rotating display. 旋转显示屏 25 | 26 | // The address can be changed making the option of connecting multiple 27 | // devices 地址可以改变,以连接多个设备 28 | ads.getAddr_ADS1100( 29 | ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) 30 | 31 | // The ADC gain (PGA). ADC增益(PGA) 32 | ads.setGain(GAIN_ONE); // 1x gain(default) 33 | // ads.setGain(GAIN_TWO); // 2x gain 34 | // ads.setGain(GAIN_FOUR); // 4x gain 35 | // ads.setGain(GAIN_EIGHT); // 8x gain 36 | 37 | // Device operating mode. 设备工作模式 38 | ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) 39 | // ads.setMode(MODE_SINGLE); // Single-conversion mode 40 | 41 | // Data rate. 数据速率 42 | ads.setRate(RATE_8); // 8SPS (default) 43 | // ads.setRate(RATE_16); // 16SPS 44 | // ads.setRate(RATE_32); // 32SPS 45 | // ads.setRate(RATE_128); // 128SPS 46 | 47 | ads.setOSMode( 48 | OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换 49 | 50 | ads.begin(); // Sets up the Hardware. 设置硬件 51 | M5.Lcd.print("ADC"); 52 | } 53 | 54 | void loop(void) { 55 | byte error; 56 | int8_t address; 57 | 58 | address = ads.ads_i2cAddress; 59 | Wire.beginTransmission(address); 60 | error = Wire.endTransmission(); 61 | if (error == 0) // If the device is connected. 如果连接上设备 62 | { 63 | int16_t result; 64 | result = ads.Measure_Differential(); 65 | M5.Lcd.fillScreen(BLACK); 66 | char data[20] = {0}; 67 | sprintf(data, "%d", result); 68 | M5.Lcd.drawCentreString(data, 0, 20, 4); 69 | } else { 70 | M5.Lcd.setCursor(0, 40); 71 | M5.Lcd.drawString("No Found ADC sensor.", 0, 20); 72 | } 73 | delay(1000); 74 | } -------------------------------------------------------------------------------- /examples/Unit_ADC_M5Atom/Unit_ADC_M5Atom.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************* 3 | * Copyright (c) 2021 by M5Stack 4 | * Equipped with Atom-Lite/Matrix sample source code 5 | * 配套 Atom-Lite/Matrix 示例源代码 6 | * Visit for more information: https://docs.m5stack.com/en/unit/adc 7 | * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/adc 8 | * 9 | * Product: ADC. A/D转换器 10 | * Date: 2021/8/18 11 | ******************************************************************************* 12 | Please connect to Port A,Use ADC Unit to convert 0 ~ 12V analog voltage into 13 | 16-bit data and display it on the Serial. 请连接端口,利用ADC单元将0 ~ 14 | 12V模拟电压转换成16位数据显示在串口上。 15 | */ 16 | 17 | #include 18 | #include "M5_ADS1100.h" 19 | 20 | ADS1100 ads; 21 | 22 | void setup(void) { 23 | M5.begin(); // Init M5Atom. 初始化M5Atom 24 | Wire.begin(26, 32); // Initialize pin 26,32. 初始化26,32引脚 25 | 26 | // The address can be changed making the option of connecting multiple 27 | // devices 地址可以改变,以连接多个设备 28 | ads.getAddr_ADS1100( 29 | ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) 30 | 31 | // The ADC gain (PGA). ADC增益(PGA) 32 | ads.setGain(GAIN_ONE); // 1x gain(default) 33 | // ads.setGain(GAIN_TWO); // 2x gain 34 | // ads.setGain(GAIN_FOUR); // 4x gain 35 | // ads.setGain(GAIN_EIGHT); // 8x gain 36 | 37 | // Device operating mode. 设备工作模式 38 | ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) 39 | // ads.setMode(MODE_SINGLE); // Single-conversion mode 40 | 41 | // Data rate. 数据速率 42 | ads.setRate(RATE_8); // 8SPS (default) 43 | // ads.setRate(RATE_16); // 16SPS 44 | // ads.setRate(RATE_32); // 32SPS 45 | // ads.setRate(RATE_128); // 128SPS 46 | 47 | ads.setOSMode( 48 | OSMODE_SINGLE); // Set to start a single-conversion. 设置开始一次转换 49 | 50 | ads.begin(); // Sets up the Hardware. 设置硬件 51 | } 52 | 53 | void loop(void) { 54 | byte error; 55 | int8_t address; 56 | 57 | address = ads.ads_i2cAddress; 58 | 59 | Wire.beginTransmission(address); 60 | error = Wire.endTransmission(); 61 | if (error == 0) // If the device is connected. 如果连接上设备 62 | { 63 | int16_t result; 64 | 65 | Serial.println("Getting Differential Reading from ADS1100"); 66 | Serial.println(" "); 67 | result = ads.Measure_Differential(); 68 | Serial.print("Digital Value of Analog Input between Channel 0 and 1: "); 69 | Serial.println(result); 70 | char data[20] = {0}; 71 | sprintf(data, "%d", result); 72 | Serial.println("-----------"); 73 | } else { 74 | Serial.println("ADS1100 Disconnected!"); 75 | Serial.println("-----------"); 76 | } 77 | 78 | delay(1000); 79 | } -------------------------------------------------------------------------------- /src/M5_ADS1100.cpp: -------------------------------------------------------------------------------- 1 | #include "M5_ADS1100.h" 2 | 3 | #if ARDUINO >= 100 4 | #include "Arduino.h" 5 | #else 6 | #include "WProgram.h" 7 | #endif 8 | 9 | #include 10 | 11 | // Abstract away platform differences in Arduino wire library 12 | static uint8_t i2cread(void) { 13 | #if ARDUINO >= 100 14 | return Wire.read(); 15 | #else 16 | return Wire.receive(); 17 | #endif 18 | } 19 | 20 | // Abstract away platform differences in Arduino wire library 21 | static void i2cwrite(uint8_t x) { 22 | #if ARDUINO >= 100 23 | Wire.write((uint8_t)x); 24 | #else 25 | Wire.send(x); 26 | #endif 27 | } 28 | 29 | /*! @brief Writes 8-bits to the destination register.*/ 30 | static void writeRegister(uint8_t i2cAddress, uint8_t value) { 31 | Wire.beginTransmission(i2cAddress); 32 | i2cwrite((uint8_t)value); 33 | Wire.endTransmission(); 34 | } 35 | 36 | /*! @brief Reads 16-bits from the destination register.*/ 37 | static uint16_t readRegister(uint8_t i2cAddress) { 38 | Wire.beginTransmission(i2cAddress); 39 | Wire.endTransmission(); 40 | Wire.requestFrom(i2cAddress, (uint8_t)2); 41 | return (int16_t)((i2cread() << 8) | i2cread()); 42 | } 43 | 44 | /*! @brief Instantiates a new ADS1100 class with appropriate properties.*/ 45 | void ADS1100::getAddr_ADS1100(uint8_t i2cAddress) { 46 | ads_i2cAddress = i2cAddress; 47 | ads_conversionDelay = ADS1100_CONVERSIONDELAY; 48 | } 49 | 50 | /*! @brief Sets up the Hardware.*/ 51 | void ADS1100::begin() { 52 | Wire.begin(); 53 | } 54 | 55 | /*! @brief Sets the Operational status/single-shot conversion start 56 | This determines the operational status of the device.*/ 57 | void ADS1100::setOSMode(adsOSMode_t osmode) { 58 | ads_osmode = osmode; 59 | } 60 | 61 | /*! @brief Gets the Operational status/single-shot conversion start.*/ 62 | adsOSMode_t ADS1100::getOSMode() { 63 | return ads_osmode; 64 | } 65 | 66 | /*! @brief Sets the Device operating mode 67 | This controls the current operational mode of the ADS1100.*/ 68 | void ADS1100::setMode(adsMode_t mode) { 69 | ads_mode = mode; 70 | } 71 | 72 | /*! @brief Gets the Device operating mode.*/ 73 | adsMode_t ADS1100::getMode() { 74 | return ads_mode; 75 | } 76 | 77 | /*! @brief Sets the Date Rate. 78 | This controls the data rate setting.*/ 79 | void ADS1100::setRate(adsRate_t rate) { 80 | ads_rate = rate; 81 | } 82 | 83 | /*! @brief Gets the Date Rate.*/ 84 | adsRate_t ADS1100::getRate() { 85 | return ads_rate; 86 | } 87 | 88 | /*! @brief Sets the gain and input voltage range 89 | This configures the programmable gain amplifier.*/ 90 | void ADS1100::setGain(adsGain_t gain) { 91 | ads_gain = gain; 92 | } 93 | 94 | /*! @brief Gets a gain and input voltage range.*/ 95 | adsGain_t ADS1100::getGain() { 96 | return ads_gain; 97 | } 98 | 99 | /*! @brief Reads the conversion results, measuring the voltage 100 | difference between the P and N input 101 | Generates a signed value since the difference can be either 102 | positive or negative.*/ 103 | int16_t ADS1100::Measure_Differential() { 104 | // Start with default values 105 | uint16_t config; 106 | 107 | // Set Operational status/single-shot conversion start 108 | config |= ads_osmode; 109 | 110 | // Set Device operating mode 111 | config |= ads_mode; 112 | 113 | // Set Data rate 114 | config |= ads_rate; 115 | 116 | // Set PGA/voltage range 117 | config |= ads_gain; 118 | 119 | // Write config register to the ADC 120 | writeRegister(ads_i2cAddress, config); 121 | 122 | // Wait for the conversion to complete 123 | delay(ads_conversionDelay); 124 | 125 | // Read the conversion results 126 | uint16_t raw_adc = readRegister(ads_i2cAddress); 127 | return (int16_t)raw_adc; 128 | } 129 | -------------------------------------------------------------------------------- /src/M5_ADS1100.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @brief A 16-bit ADS1100 ADC converter library From M5Stack 3 | * @copyright Copyright (c) 2022 by M5Stack[https://m5stack.com] 4 | * 5 | * @Links [Unit ADC](https://docs.m5stack.com/en/unit/adc) 6 | * @Links [Hat ADC](https://docs.m5stack.com/en/hat/hat-adc) 7 | * @version V0.0.1 8 | * @date 2022-07-11 9 | */ 10 | #ifndef _M5_ADS1100_H_ 11 | #define _M5_ADS1100_H_ 12 | 13 | #if ARDUINO >= 100 14 | #include "Arduino.h" 15 | #else 16 | #include "WProgram.h" 17 | #endif 18 | 19 | #include 20 | 21 | /************************************************************************** 22 | I2C ADDRESS/BITS 23 | **************************************************************************/ 24 | #define ADS1100_DEFAULT_ADDRESS (0x48) // 1001 000 (ADDR = GND) 25 | 26 | /************************************************************************** 27 | CONVERSION DELAY (in mS) 28 | **************************************************************************/ 29 | #define ADS1100_CONVERSIONDELAY (100) 30 | 31 | /************************************************************************** 32 | CONFIG REGISTER 33 | **************************************************************************/ 34 | #define ADS1100_REG_CONFIG_OS_MASK (0x80) // Conversion 35 | #define ADS1100_REG_CONFIG_OS_NOEFFECT (0x00) // Write: Bit = 0 No effect 36 | #define ADS1100_REG_CONFIG_OS_SINGLE \ 37 | (0x80) // Write: Bit = 1 Begin a conversion (default) 38 | #define ADS1100_REG_CONFIG_OS_BUSY \ 39 | (0x00) // Read: Bit = 0 Device is not performing a conversion 40 | #define ADS1100_REG_CONFIG_OS_NOTBUSY \ 41 | (0x80) // Read: Bit = 1 Device is busy performing a conversion 42 | 43 | #define ADS1100_REG_CONFIG_MODE_MASK (0x10) // Device operating mode 44 | #define ADS1100_REG_CONFIG_MODE_CONTIN \ 45 | (0x00) // Continuous conversion mode (default) 46 | #define ADS1100_REG_CONFIG_MODE_SINGLE (0x10) // Single-conversion mode 47 | 48 | #define ADS1100_REG_CONFIG_DR_MASK (0x0C) // Data rate 49 | #define ADS1100_REG_CONFIG_DR_128SPS (0x00) // 128 samples per second 50 | #define ADS1100_REG_CONFIG_DR_32SPS (0x04) // 32 samples per second 51 | #define ADS1100_REG_CONFIG_DR_16SPS (0x08) // 16 samples per second 52 | #define ADS1100_REG_CONFIG_DR_8SPS (0x0C) // 8 samples per second (default) 53 | 54 | #define ADS1100_REG_CONFIG_PGA_MASK \ 55 | (0x03) // Programmable gain amplifier configuration 56 | #define ADS1100_REG_CONFIG_PGA_1 (0x00) // Gain 1 (default) 57 | #define ADS1100_REG_CONFIG_PGA_2 (0x01) // Gain 2 58 | #define ADS1100_REG_CONFIG_PGA_4 (0x02) // Gain 4 59 | #define ADS1100_REG_CONFIG_PGA_8 (0x03) // Gain 8 60 | 61 | /**************************************************************************/ 62 | 63 | typedef enum { 64 | OSMODE_SINGLE = ADS1100_REG_CONFIG_OS_SINGLE, 65 | OSMODE_BUSY = ADS1100_REG_CONFIG_OS_BUSY, 66 | OSMODE_NOTBUSY = ADS1100_REG_CONFIG_OS_NOTBUSY 67 | 68 | } adsOSMode_t; 69 | 70 | typedef enum { 71 | MODE_CONTIN = ADS1100_REG_CONFIG_MODE_CONTIN, 72 | MODE_SINGLE = ADS1100_REG_CONFIG_MODE_SINGLE 73 | } adsMode_t; 74 | 75 | typedef enum { 76 | RATE_128 = ADS1100_REG_CONFIG_DR_128SPS, 77 | RATE_32 = ADS1100_REG_CONFIG_DR_32SPS, 78 | RATE_16 = ADS1100_REG_CONFIG_DR_16SPS, 79 | RATE_8 = ADS1100_REG_CONFIG_DR_8SPS 80 | } adsRate_t; 81 | 82 | typedef enum { 83 | GAIN_ONE = ADS1100_REG_CONFIG_PGA_1, 84 | GAIN_TWO = ADS1100_REG_CONFIG_PGA_2, 85 | GAIN_FOUR = ADS1100_REG_CONFIG_PGA_4, 86 | GAIN_EIGHT = ADS1100_REG_CONFIG_PGA_8 87 | } adsGain_t; 88 | 89 | class ADS1100 { 90 | protected: 91 | // Instance-specific properties 92 | uint8_t ads_conversionDelay; 93 | adsOSMode_t ads_osmode; 94 | adsMode_t ads_mode; 95 | adsRate_t ads_rate; 96 | adsGain_t ads_gain; 97 | 98 | public: 99 | uint8_t ads_i2cAddress; 100 | void getAddr_ADS1100(uint8_t i2cAddress); 101 | void begin(void); 102 | int16_t Measure_Differential(); 103 | void setOSMode(adsOSMode_t osmode); 104 | adsOSMode_t getOSMode(void); 105 | void setMode(adsMode_t mode); 106 | adsMode_t getMode(void); 107 | void setRate(adsRate_t rate); 108 | adsRate_t getRate(void); 109 | void setGain(adsGain_t gain); 110 | adsGain_t getGain(void); 111 | 112 | private: 113 | }; 114 | 115 | #endif 116 | --------------------------------------------------------------------------------