如何使用 Sparkfun - Artemis Thing Plus 读取电池电量?

How can I read battery levels using Sparkfun - Artemis Thing Plus?

提问人:Josue Baquerizo 提问时间:11/15/2023 最后编辑:Josue Baquerizo 更新时间:11/15/2023 访问量:24

问:

我有以下设置:

  1. SparkFun Artemis Thing Plus https://www.sparkfun.com/products/15574
  2. 3.7V 522332 400mAh 锂电池可充电锂聚合物离子电池组,带 PH2.0mm JST 连接器 https://www.amazon.com/Battery-Rechargeable-Lithium-Polymer-Connector/dp/B07BTWMM8H
  3. SparkFun 光电探测器分线板 - MAX30101 (Qwiic) https://www.sparkfun.com/products/16474
  4. SparkFun Micro OLED 分线板 (Qwiic) https://www.sparkfun.com/products/22495

enter image description here

Artemis Thing Plus 是主板,Lipo 电池连接到 JST 连接器,工作正常。MAX30101是光传感器,Micro OLED Breakout是显示器。所有部件都可以很好地协同工作,但现在我想添加到我的代码中以读取电池电量。电池非常适合我们的电池,并通过 USB-C 充电。

我想在我的OLED上看到电池电量百分比。

这是我当前的代码:

#include <SFE_MicroOLED.h>
#include <Wire.h>

#include "MAX30105.h"

#define PIN_RESET 9
#define DC_JUMPER 1

MAX30105 particleSensor;
MicroOLED oled(PIN_RESET, DC_JUMPER);

long unblockedValue;  // Average IR at power up

String multiplyChar(char c, int n) {
  String result = "";
  for (int i = 0; i < n; i++) {
    result += c;
  }
  return result;
}

void displayMeasurement(int rLevel) {
  oled.clear(PAGE);
  oled.setCursor(0, 0);

  int calibratedReading = f(rLevel);
  int centerPadding = 4 - String(calibratedReading).length();
  String paddingText = multiplyChar(' ', centerPadding);

  if (rLevel == 0) {
    oled.setFontType(1);
    oled.print("Please load   sample!");
    oled.display();
    return;
  }

  oled.setFontType(3);
  oled.print(paddingText);
  oled.print(calibratedReading);

  Serial.println("real:" + String(rLevel));
  Serial.println("agtron:" + String(calibratedReading));
  Serial.println("===========================");

  oled.display();
}

int f(int x) {
  int intersectionPoint = 117;
  float deviation = 0.165;

  return round(x - (intersectionPoint - x) * deviation);
}

void setup() {
  Serial.begin(9600);

  Wire.begin();
  oled.begin();      // Initialize the OLED
  oled.clear(ALL);   // Clear the display's internal memory
  oled.clear(PAGE);  // Clear the buffer.

  delay(100);  // Delay 100 ms
  oled.setFontType(3);

  // Initialize sensor
  if (particleSensor.begin(Wire, I2C_SPEED_FAST) == false)  // Use default I2C port, 400kHz speed
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1)
      ;
  }

  // The variable below calibrates the LED output on your hardware.
  byte ledBrightness = 135;

  byte sampleAverage = 4;  // Options: 1, 2, 4, 8, 16, --32--
  byte ledMode = 2;        // Options: 1 = Red only, --2 = Red + IR--, 3 = Red + IR + Green
  int sampleRate = 50;     // Options: 50, 100, 200, 400, 800, 1000, 1600, --3200--
  int pulseWidth = 411;    // Options: 69, 118, 215, --411--
  int adcRange = 16384;    // Options: 2048, --4096--, 8192, 16384

  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);  // Configure sensor with these settings

  particleSensor.setPulseAmplitudeRed(0);
  particleSensor.setPulseAmplitudeGreen(0);

  particleSensor.disableSlots();
  particleSensor.enableSlot(2, 0x02);  // Enable only SLOT_IR_LED = 0x02

  // Update to ignore readings under 30.000
  unblockedValue = 30000;
}

void loop() {
  int rLevel = particleSensor.getIR();
  long currentDelta = rLevel - unblockedValue;

  if (currentDelta > (long)100) {
    displayMeasurement(rLevel / 1000);
  } else {
    displayMeasurement(0);
  }
  delay(100);
}

拜托,任何事情都会有所帮助。我已经读过这个,但我不确定如何进行。如果有帮助,请忘记光传感器的作用。我希望能够显示电池电量百分比。

我确实尝试过这个,但它根本不起作用

const int batteryPin = A0; // Analog pin connected to the battery voltage
const float voltageDividerRatio = 2.0; // Adjust this value based on your voltage divider setup
const float minBatteryVoltage = 3.0; // Minimum battery voltage (volts)
const float maxBatteryVoltage = 4.2; // Maximum battery voltage (volts)

void setup() {
  Serial.begin(9600);
}

void loop() {
  // Read the analog voltage from the battery pin
  int batteryValue = analogRead(batteryPin);

  // Convert the analog value to battery voltage (assuming a 3.3V reference)
  float batteryVoltage = (batteryValue / 1023.0) * 3.3 * voltageDividerRatio;

  // Calculate the battery level percentage
  int batteryLevel = map(batteryVoltage, minBatteryVoltage, maxBatteryVoltage, 0, 100);

  // Print the battery voltage and level to the serial monitor
  Serial.print("Battery Voltage: ");
  Serial.print(batteryVoltage, 2); // Display with 2 decimal places
  Serial.print("V, Battery Level: ");
  Serial.print(batteryLevel);
  Serial.println("%");

  delay(10000); // Delay for 10 seconds before checking again
}
Arduino Arduino-C++ Sparkfun

评论

0赞 hcheung 11/15/2023
“我确实尝试过这个,但它根本没有工作”,据我从原理图中可以看出,没有分压器连接到 A0 引脚,所以除非你这样做了,否则你没有什么可以测量的。
0赞 Josue Baquerizo 11/30/2023
谢谢你的建议。我目前有这样的设置:我使用分压器、2 x 100K 电阻将 VBAT 引脚连接到 ADC A0 引脚。然后,我将中间点取到 A0,末端取到 GND 引脚。

答: 暂无答案