From 6b86aa707ba59a179ab8500847a101d176e7f29e Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Mon, 24 May 2021 16:08:01 +0200 Subject: [PATCH] Add Possibility to Measure External Batteries via Analog Pin --- src/config.h.example | 11 +++++++++++ src/main.cpp | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/config.h.example b/src/config.h.example index a939ade..4a27ea2 100644 --- a/src/config.h.example +++ b/src/config.h.example @@ -26,6 +26,17 @@ *************************************************************************************************************************/ // #define BTN_PIN PIN_PC2 +/************************************************************************************************************************** + * External Battery Monitoring - If EXT_BAT_PIN is set to an analog pin of the ATTiny (e.g. PB4 or PB5), it will be used + * to read the Battery Voltage instead of the internal supply voltage. This can e.g. be used to monitor the voltage of an + * LiPo/LiIon cell connected to the Node via the PowerPack. The Devider should not put out more than 2.5V at the + * measurement point. Choose high resistor values (e.g. 2x 1MOhm for LiPo/LiIon) to minimize current drain through the + * resistors. The Send voltage will be measured voltage at the pin times EXT_BAT_RATIO. + *************************************************************************************************************************/ +// #define EXT_BAT_PIN PIN_PB5 +// #define EXT_BAT_RATIO 2 + + /************************************************************************************************************************** * Number of active Sensors (used as long as HAS_NO_SENSOR is not enabled) * Set to the correct number of enabled sensors below diff --git a/src/main.cpp b/src/main.cpp index f68a767..d7687e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -220,6 +220,20 @@ void onEvent(ev_t ev) { } // Get Battery Voltage +#ifdef EXT_BAT_PIN +uint16_t readSupplyVoltage() { + pinMode(EXT_BAT_PIN, INPUT); + analogReference(INTERNAL2V5); + ADC0.CTRLD = ADC_INITDLY_DLY256_gc; + ADC0_CTRLB = ADC_SAMPNUM_ACC64_gc; + uint16_t reading = analogRead(EXT_BAT_PIN); + reading = reading/64; + DEBUG_PRINT("ADC Reading: "); + DEBUG_PRINTLN(reading); + reading = (int16_t)(reading * (2500/1024.0) * EXT_BAT_RATIO); + return reading; +} +#else uint16_t readSupplyVoltage() { //returns value in millivolts to avoid floating point uint16_t temp = 0; analogReference(VDD); @@ -233,6 +247,7 @@ uint16_t readSupplyVoltage() { //returns value in millivolts to avoid floating p reading = intermediate / temp; return reading; } +#endif // Read Sensors and Send Data void do_send(osjob_t* j) { @@ -305,6 +320,10 @@ void setup() for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++) pinMode(disabledPins[i], INPUT_PULLUP); + #ifdef EXT_BAT_PIN + + #endif + // Setup WS2812B LEDs #ifdef WS2812B_PIN pinMode(WS2812B_PIN, OUTPUT);