Skip to content

Commit

Permalink
Add example sketches.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Aug 1, 2018
1 parent c9af061 commit d90ff36
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)*
Expand Down
50 changes: 50 additions & 0 deletions examples/CT2_LCD/CT2_LCD.ino
Original file line number Diff line number Diff line change
@@ -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 <CurrentTransformer.h> // https://github.com/JChristensen/CurrentTransformer
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <LiquidTWI.h> //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
}

2 changes: 1 addition & 1 deletion examples/CT2_Serial/CT2_Serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
45 changes: 45 additions & 0 deletions examples/CT_LCD/CT_LCD.ino
Original file line number Diff line number Diff line change
@@ -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 <CurrentTransformer.h> // https://github.com/JChristensen/CurrentTransformer
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <LiquidTWI.h> //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
}

4 changes: 2 additions & 2 deletions examples/CT_Serial/CT_Serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
}

0 comments on commit d90ff36

Please sign in to comment.