English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Hat ADC Arduino Tutorial

1. Preparations

2. Precautions

Wiring Notice
The input is a single-ended input referenced to GND. The DC 0–12 V range applies only when PGA=1. Connect the power supply negative terminal to the module GND and the positive terminal to the input +. Do not reverse the input, apply AC, or exceed 12 V. Use a current-limited supply and apply the input voltage after the controller has powered up.
Model ADC Chip Reference Voltage Input Divider
Hat ADC (U069) ADS1100 3.3 V supply voltage 300 kΩ / 100 kΩ, ratio 4.00
Hat ADC v1.1 (U069-V11) ADS1110 Internal 2.048 V 510 kΩ / 100 kΩ, ratio 6.10

3. Sample Program

  • This tutorial uses StickC-Plus2 with Hat ADC plugged directly into the controller HAT header. The HAT_ADC_SDA_PIN and HAT_ADC_SCL_PIN macros default to G0 and G26.
  • The example uses setChip() to select ADS1100 or ADS1110 and displays the converted input voltage on the screen and serial monitor. Set V11 to false for Hat ADC and true for Hat ADC v1.1. The two chips do not provide a readable version ID, so select the parameters for the actual module.
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#include <M5Unified.h>
#include <Wire.h>
#include <M5_ADS1100.h>

#define HAT_ADC_SDA_PIN 0
#define HAT_ADC_SCL_PIN 26

namespace {
constexpr uint8_t ADC_ADDR = ADS1100_DEFAULT_ADDRESS;
constexpr bool V11 = true;  // false: U069 ADS1100, true: U069-V11.
constexpr float RATIO = V11 ? 6.10f : 4.00f;
constexpr int W = 240, H = 135, M = 8, CY = 45, CH = 64, CW = W - 2 * M;
constexpr uint16_t BG = 0x0861, CARD = 0x10C4, TRACK = 0x3186;
constexpr uint8_t ZERO_N = 16, ZERO_SKIP = 2;

ADS1100 adc;
M5Canvas canvas(&M5.Display);
int32_t zeroOffset = 0;

bool calibrateZero() {
  int32_t sum = 0;
  int16_t sample = 0;

  for (uint8_t i = 0; i < ZERO_N; ++i) {
    if (!adc.measureDifferential(sample)) {
      return false;
    }
    if (i >= ZERO_SKIP) {
      sum += sample;
    }
  }

  zeroOffset = sum / (ZERO_N - ZERO_SKIP);
  return true;
}

void drawText(const char* text, int x, int y, textdatum_t datum,
              uint16_t color, uint16_t background) {
  canvas.setTextDatum(datum);
  canvas.setTextColor(color, background);
  canvas.drawString(text, x, y);
}

void drawScreen(bool ok, int16_t raw, float voltage) {
  const uint16_t accent = !ok || voltage < -0.1f || voltage > 12.5f
                              ? TFT_RED
                              : voltage > 11.5f ? TFT_ORANGE : TFT_CYAN;
  char value[24];
  char detail[40];

  if (ok) {
    snprintf(value, sizeof(value), "%.3f V", voltage);
  } else {
    snprintf(value, sizeof(value), "ADC NOT FOUND");
  }
  if (ok) {
    snprintf(detail, sizeof(detail), "RAW %d", raw);
  } else {
    snprintf(detail, sizeof(detail), "CHECK INPUT / I2C");
  }

  canvas.fillSprite(BG);
  canvas.fillRect(0, 0, W, 3, accent);
  canvas.setFont(&fonts::FreeSansBold9pt7b);
  canvas.setTextSize(1);
  drawText("HAT ADC", W / 2, 15, middle_center, TFT_WHITE, BG);
  drawText(V11 ? "ADS1110" : "ADS1100", W / 2, 32,
           middle_center, TFT_LIGHTGREY, BG);

  canvas.fillRoundRect(M, CY, CW, CH, 12, CARD);
  canvas.drawRoundRect(M, CY, CW, CH, 12, accent);
  if (ok) {
    canvas.setFont(&fonts::FreeSansBold18pt7b);
  } else {
    canvas.setFont(&fonts::FreeSansBold12pt7b);
  }
  drawText(value, W / 2, CY + 27, middle_center,
           ok ? TFT_WHITE : TFT_RED, CARD);

  canvas.setFont(&fonts::FreeSansBold9pt7b);
  drawText(detail, W / 2, CY + CH - 12, middle_center,
           TFT_LIGHTGREY, CARD);
  drawText("0 V", M, 113, middle_left, TFT_LIGHTGREY, BG);
  drawText("12 V", W - M, 113, middle_right, TFT_LIGHTGREY, BG);

  canvas.fillRoundRect(M, 121, CW, 8, 4, TRACK);
  if (ok && voltage > 0) {
    const int fill = constrain(static_cast<int>(voltage / 12.0f * CW + 0.5f), 0, CW);
    canvas.fillRoundRect(M, 121, fill, 8, min(4, fill / 2), accent);
  }
  canvas.pushSprite(0, 0);
}
}  // namespace

void setup() {
  M5.begin();
  M5.Display.setRotation(3);
  Serial.begin(115200);
  canvas.setColorDepth(16);
  canvas.createSprite(W, H);

  adc.getAddr_ADS1100(ADC_ADDR);
  adc.setChip(V11 ? ADS_CHIP_ADS1110 : ADS_CHIP_ADS1100);
  adc.setGain(GAIN_ONE);
  adc.setMode(MODE_CONTIN);
  adc.setRate(V11 ? ADS1110_RATE_15 : RATE_8);
  adc.setOSMode(OSMODE_SINGLE);

  Wire.end();
  const bool connected = adc.begin(HAT_ADC_SDA_PIN, HAT_ADC_SCL_PIN, 100000) &&
                         adc.isConnected();

  // Keep the input floating while the startup offset is measured.
  const bool ok = connected && calibrateZero();
  if (ok) {
    Serial.printf("ZERO=%ld\n", static_cast<long>(zeroOffset));
  } else {
    Serial.println("ADC init/zero calibration failed");
  }
  drawScreen(ok, 0, 0.0f);
}

void loop() {
  M5.update();

  int16_t raw = 0;
  if (!adc.measureDifferential(raw)) {
    drawScreen(false, 0, 0.0f);
    delay(500);
    return;
  }

  const int16_t adjusted = static_cast<int16_t>(
      constrain(static_cast<int32_t>(raw) - zeroOffset, -32768, 32767));
  const float voltage =
      adjusted / 32768.0f * adc.getNominalReferenceVoltage() * RATIO;
  drawScreen(true, adjusted, voltage);
  Serial.printf("VIN=%.3f V RAW=%d\n", voltage, adjusted);
  delay(500);
}

4. Compile and Upload

  • Copy and paste the example code above into the project code area, select the device port (see Program Compile and Upload), click the compile and upload button in the upper-left corner of the Arduino IDE, and wait for the program to compile and upload.

5. Running Result

  • After the program starts, StickC-Plus2 displays HAT ADC, the selected ADC model, the input voltage, the RAW value, and a 0–12 V progress bar. The serial monitor outputs VIN and RAW. The interface is cyan for normal readings and orange near 12 V. Values may differ slightly because of divider tolerance and ADC error.
Page Tools
PDF
On This Page