From d90ff36755b3d63f39e9a3c85b7d9e62b6dbc92d Mon Sep 17 00:00:00 2001 From: JChristensen Date: Wed, 1 Aug 2018 12:18:42 -0400 Subject: [PATCH] Add example sketches. --- README.md | 4 +-- examples/CT2_LCD/CT2_LCD.ino | 50 ++++++++++++++++++++++++++++++ examples/CT2_Serial/CT2_Serial.ino | 2 +- examples/CT_LCD/CT_LCD.ino | 45 +++++++++++++++++++++++++++ examples/CT_Serial/CT_Serial.ino | 4 +-- 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 examples/CT2_LCD/CT2_LCD.ino create mode 100644 examples/CT_LCD/CT_LCD.ino diff --git a/README.md b/README.md index b07db93..ce055bc 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,8 @@ The constructor defines a CurrentTransformer object. ##### Syntax `CurrentTransformer(channel, ratio, burden, vcc, freq);` ##### Required parameters -**channel:** ADC channel number that the current transformer is connected to. (Arduino pin numbers can also be used, i.e. A0-A5). *(uint8_t)* -**ratio:** Secondary:Primary turns ratio for the current transformer. *(float)* +**channel:** ADC channel number that the current transformer is connected to. (Arduino pin numbers can also be used, e.g. A0-A5). *(uint8_t)* +**ratio:** Secondary:Primary turns ratio for the current transformer. *(float)* **burden:** Current transformer burden resistor value in ohms. *(float)* ##### Optional parameters **vcc:** Microcontroller supply voltage. For best accuracy, measure the actual microcontroller supply voltage and provide it using this parameter. Defaults to 5.0V if not given. *(float)* diff --git a/examples/CT2_LCD/CT2_LCD.ino b/examples/CT2_LCD/CT2_LCD.ino new file mode 100644 index 0000000..84ca27f --- /dev/null +++ b/examples/CT2_LCD/CT2_LCD.ino @@ -0,0 +1,50 @@ +// Arduino Current Transformer Library +// https://github.com/JChristensen/CurrentTransformer +// Copyright (C) 2018 by Jack Christensen and licensed under +// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html +// +// Example sketch to read two current transformers periodically +// and display the measurement on a serial (I2C) LCD display. +// Tested with TA17L-03 current transformer (10A max), Arduino Uno, +// Arduino v1.8.5. + +#include // https://github.com/JChristensen/CurrentTransformer +#include // http://arduiniana.org/libraries/streaming/ +#include //http://forums.adafruit.com/viewtopic.php?t=21586 +// or http://dl.dropboxusercontent.com/u/35284720/postfiles/LiquidTWI-1.5.1.zip + +const uint8_t ctChannel0(0); // adc channel for ct-0 +const uint8_t ctChannel1(1); // adc channel for ct-1 +const float ctRatio(1000); // current transformer winding ratio +const float rBurden(200); // current transformer burden resistor value +const float vcc(5.070); // adjust to actual value for best accuracy +const uint32_t MS_BETWEEN_SAMPLES(1000); // milliseconds +const int32_t BAUD_RATE(115200); + +// object definitions +CurrentTransformer ct0(ctChannel0, ctRatio, rBurden, vcc); +CurrentTransformer ct1(ctChannel1, ctRatio, rBurden, vcc); +LiquidTWI lcd(0); //i2c address 0 (0x20) + +void setup() +{ + delay(1000); + Serial.begin(BAUD_RATE); + ct0.begin(); + lcd.begin(16, 2); + lcd.clear(); +} + +void loop() +{ + uint32_t msStart = millis(); + float i0 = ct0.read(); + float i1 = ct1.read(); + Serial << millis() << F(" ") << _FLOAT(i0, 3) << F(" A ") << _FLOAT(i1, 3) << F(" A\n"); + lcd.setCursor(0, 0); + lcd << F("CT-") << ctChannel0 << ' ' << _FLOAT(i0, 3) << F(" AMP"); + lcd.setCursor(0, 1); + lcd << F("CT-") << ctChannel1 << ' ' << _FLOAT(i1, 3) << F(" AMP"); + while (millis() - msStart < MS_BETWEEN_SAMPLES); // wait to start next measurement +} + diff --git a/examples/CT2_Serial/CT2_Serial.ino b/examples/CT2_Serial/CT2_Serial.ino index f94780d..7052651 100644 --- a/examples/CT2_Serial/CT2_Serial.ino +++ b/examples/CT2_Serial/CT2_Serial.ino @@ -13,7 +13,7 @@ const float ctRatio(1000); // current transformer winding ratio const float rBurden(200); // current transformer burden resistor value -const float vcc(5.120); // adjust to actual value for best accuracy +const float vcc(5.070); // adjust to actual value for best accuracy const uint32_t MS_BETWEEN_SAMPLES(5000); // milliseconds const int32_t BAUD_RATE(115200); diff --git a/examples/CT_LCD/CT_LCD.ino b/examples/CT_LCD/CT_LCD.ino new file mode 100644 index 0000000..5206589 --- /dev/null +++ b/examples/CT_LCD/CT_LCD.ino @@ -0,0 +1,45 @@ +// Arduino Current Transformer Library +// https://github.com/JChristensen/CurrentTransformer +// Copyright (C) 2018 by Jack Christensen and licensed under +// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html +// +// Example sketch to read a current transformer every five seconds +// and display the measurement on a serial (I2C) LCD display. +// Tested with TA17L-03 current transformer (10A max), Arduino Uno, +// Arduino v1.8.5. + +#include // https://github.com/JChristensen/CurrentTransformer +#include // http://arduiniana.org/libraries/streaming/ +#include //http://forums.adafruit.com/viewtopic.php?t=21586 +// or http://dl.dropboxusercontent.com/u/35284720/postfiles/LiquidTWI-1.5.1.zip + +const uint8_t ctChannel(0); // adc channel +const float ctRatio(1000); // current transformer winding ratio +const float rBurden(200); // current transformer burden resistor value +const float vcc(5.070); // adjust to actual value for best accuracy +const uint32_t MS_BETWEEN_SAMPLES(1000); // milliseconds +const int32_t BAUD_RATE(115200); + +// object definitions +CurrentTransformer ct0(ctChannel, ctRatio, rBurden, vcc); +LiquidTWI lcd(0); //i2c address 0 (0x20) + +void setup() +{ + delay(1000); + Serial.begin(BAUD_RATE); + ct0.begin(); + lcd.begin(16, 2); + lcd.clear(); +} + +void loop() +{ + uint32_t msStart = millis(); + float i0 = ct0.read(); + Serial << millis() << ' ' << _FLOAT(i0, 3) << F(" A\n"); + lcd.setCursor(0, 0); + lcd << F("CT-") << ctChannel << ' ' << _FLOAT(i0, 3) << F(" AMP"); + while (millis() - msStart < MS_BETWEEN_SAMPLES); // wait to start next measurement +} + diff --git a/examples/CT_Serial/CT_Serial.ino b/examples/CT_Serial/CT_Serial.ino index b035450..9014a8f 100644 --- a/examples/CT_Serial/CT_Serial.ino +++ b/examples/CT_Serial/CT_Serial.ino @@ -14,7 +14,7 @@ const uint8_t ctChannel(0); // adc channel const float ctRatio(1000); // current transformer winding ratio const float rBurden(200); // current transformer burden resistor value -const float vcc(5.120); // adjust to actual value for best accuracy +const float vcc(5.070); // adjust to actual value for best accuracy const uint32_t MS_BETWEEN_SAMPLES(5000); // milliseconds const int32_t BAUD_RATE(115200); @@ -31,7 +31,7 @@ void loop() { uint32_t msStart = millis(); float i0 = ct0.read(); - Serial << millis() << ' ' << _FLOAT(i0, 3) << F("A\n"); + Serial << millis() << ' ' << _FLOAT(i0, 3) << F(" A\n"); while (millis() - msStart < MS_BETWEEN_SAMPLES); // wait to start next measurement }