Are you searching for a precise and dependable current sensing solution for your upcoming project? Look no further than the TLI4971 Kit2Go. This Protip will guide you through the process of setting up and configuring the board, unlocking the full potential of the TLI4971 Kit2Go. Expect accuracy, reliability, user-friendliness, and flexibility with this solution.
Let's start by briefly understanding the TLI4971 Current measuring chip before delving into the kit's details.
The TLI4971 is a highly accurate miniature coreless magnetic current sensor designed for both AC and DC measurements. It features an analog interface and two fast over-current detection outputs.
The term "coreless" indicates that the sensor does not include a magnetic core, a typical component in traditional magnetic sensors. Instead, the TLI4971 employs an innovative design that achieves high accuracy and reliability without relying on a core. This design offers various advantages, including reduced size, improved linearity, and enhanced temperature stability.
Both over-current detection outputs can be configured and have their configuration stored within the EEPROM (electrically erasable programmable read-only memory) of the sensor chip.
1 Pin BreakdownPin 1: Supply Voltage (VDD)
Pin 2: Ground (GND)
Pin 3: Reference voltage input or output (VREF)
Pin 4: Analog signal Output (AOUT)
Pin 5: Over-current detection output 1 (open drain output) (OCD1)
Pin 6: Over-current detection output 2 (open drain output) (OCD2)
Pin 7: Negative current terminal pin (current-out) (IP-)
Pin 8: Positive current terminal pin (current-in) (IP+)
Now let's take a look at the Kit2go Pinout:
Connecting the wires carrying the current is quite simple. Ensure that there is contact between the inductive part of the wire and the Sensor's IP+ and IP- contacts. The sensor comes with two M4 connectors, making it easy to wrap your wire around the M4 screw and tighten it using the connector.
For a closer look at the device, check out this video!
As you see in the video, there is a GUI provided by Infineon to read sensor values. You can find it here. In this thread, however, we will guide you on how to use the Kit using the Arduino IDE.
2 Library InstallationInstalling the library for this Kit is very simple. All you have to do is to open your Arduino IDE and click on Sketch>Include Library>Manage Libraries, search for "4971" and install the library.
Or you could also clone the GitHub repository and add it to your Arduino Libraries.
3 Code3.0 Sensor SetupFirst, include the library by adding this line of code:
#include <TLI4971.h>
Next, initialize the TLI4971 object and specify the pin configuration based on your microcontroller board. Here's a code snippet that covers different board options:
#if defined(XMC1100_XMC2GO)
TLI4971 CurrentSensor = TLI4971(A0, A1, 5, 8, 9, 3, 4, false); //XMC2Go
#elif defined(XMC1100_Boot_Kit) || defined(XMC4700_Relax_Kit) || defined(__AVR_ATmega328P__) /**< MyIoT Adapter Socket 1 */
TLI4971 CurrentSensor = TLI4971(A0, A1, 9, 5, 2, 10, 20, false);
#endif
- 1st parameter: A_Out pin (analog channel for reading sensors analog output)
- 2nd parameter: V_Ref pin (analog channel for sensors reference voltage output)
- 3rd parameter: pwr pin for switching sensors VDD
- 4th parameter: SICI pin for SICI communication
- 5th parameter: ocd1 pin for over-current detection 1 signal of the sensor
- 6th parameter: ocd2 pin for over-current detection 2 signal of the sensor
- 7th parameter: mux pin connected to analog multiplexer on Kit2Go
- 8th parameter: mc5V states whether microcontroller is a 5V(=true) or 3V3(=false) device (needed for calculation)
Now, let's set up the serial communication. For most applications, a baud rate of 1Mbps is sufficient. Add this line of code to set the baud rate:
Serial.begin(1000000);
To ensure successful initialization of the TLI4971 object, use a while loop to check the return value of the begin() method:
while(!CurrentSensor.begin())
{
Serial.println("Current Sensor init failed!");
delay(100);
}
Once the TLI4971 object is successfully initialized, you can proceed with the configuration. First, set the measuring range of the sensor using the setMeasRange() method. Choose a range from the available options:
While the sensor of the TLI4971 Kit2Go supports up to +/- 25A, you can use any of these possible ranges above with the following line:
CurrentSensor.setMeasRange(TLI4971::FSR25);
Next, set the operating mode of the sensor. There are different modes to choose from, such as semi-differential bi-directional, fully differential, semi-differential uni-directional, and single-ended. Use the setOpMode() method to set the desired mode:
- Semi-differential Bi-directional: In this mode, the TLI4971 Kit measures current which can flow in either direction. This mode is useful for applications in which the current can change direction, such as in motor control.
CurrentSensor.setOpMode(TLI4971::SD_BID);
- Fully differential: TLI4971 Kit measures current which can only flow in one direction. This mode is useful for applications where the current flow is unidirectional, such as in power supplies. This Increases resolution by factor 2.
CurrentSensor.setOpMode(TLI4971::FD);
- Semi-differential Uni-directional: This mode is similar to the semi-differential bi-directional mode, but the current can only flow in one direction. This mode is useful for applications where the current only flows in one direction, but there may be some common-mode noise.
Common-mode noise: In electronic circuits, common-mode noise refers to unwanted signals or interference that appear on both input lines of a differential amplifier or sensor, such as the TLI4971 Kit. By rejecting common-mode noise, the TLI4971 Kit can provide more accurate measurements of the desired signal. However, it's important to note that even in fully differential mode, some common-mode noise may still be present and can affect the accuracy of the measurement.
CurrentSensor.setOpMode(TLI4971::SD_UNI);
- Single-Ended: In single-ended mode, the TLI4971 Kit measures the current flowing through a single wire, relative to GND. This mode is useful for applications in which the current flows in only one direction and there is little or no common-mode noise.
CurrentSensor.setOpMode(TLI4971::SE);
NOTE: In a semi-differential or fully differential mode, the TLI4971 Kit is designed to reject common-mode noise by amplifying only the difference between the two input signals and ignoring any signals that appear on both input lines at the same time.
That covers the basic setup and configuration of the TLI4971 Kit2Go.
Let's talk about the main features of a current sensor which are:
- Reading actual current values
- Detecting the presence of an Over-Current
The method read( ) is used to obtain the current being measured, this method returns a value of the data type double:
double currentVa=0.0;
currentVal=CurrentSensor.read();
3.2 Over-Current Detection (OCD)Over-current detection is an important safety feature that protects electrical devices from damage caused by excessive current. It involves a current sensor to measure a circuit's current and compare it to a predefined threshold. If the current exceeds this threshold, a safety mechanism is activated to prevent further damage or potential hazards.
In the case of the TLI4971, the sensor part of this safety mechanism is implemented either by:
- monitoring a toggling pin -> Hardware OCD
- a user-defined function that is called -> Software OCD
There are two ways to utilize the hardware over-current detection (OCD) feature: interrupts and polling.
Interrupts: In short, hardware interrupts are like a "pause button" for a microcontroller that allows it to handle important events by interrupting its normal program flow.
So to use the interrupt for OCD channel 1 we will be using the function:
CurrentSensor.registerOcd1Function(CurrentSensor.INTERRUPT,name_of_whatever_function);
This function takes in the response mode, in which we here specify that we want to be using an interrupt and it also takes a function that would be our Interrupt service routine (ISR).
Similarly, for OCD channel 2, use:
CurrentSensor.registerOcd2Function(CurrentSensor.INTERRUPT,name_of_whatever_function);
Now, if one of the OCD Channels is not physically connected to an interrupt pin the functions above will return false and you'll have to use the polling method.
Polling: Polling involves constantly checking the sensor to see if any changes have occurred. This method is slower and less efficient but in case your microcontroller or hardware does not support hardware interrupts, polling can be used. Call the process similar to the interrupt method:
For OCD channel 1:
CurrentSensor.registerOcd1Function(CurrentSensor.POLLING,name_of_whatever_function);
For OCD channel 2:
CurrentSensor.registerOcd2Function(CurrentSensor.POLLING,name_of_whatever_function);
NOTE: When using the Polling method, you have to call the function below in the loop() function, to execute your name_of_whatever_function that gets called when there is an OCD:
CurrentSensor.ocdPolling();
3.2.2.1 Hardware OCD ThresholdsDetermining the threshold for triggering an over-current alert depends on two factors:
- Sensor's variant: The TLI4971 has many variants, such as 25A, 50A, 75A, and 120A (Click here for a full look at all models).
If you are using the TLI4971 Kit2Go, then you're using the 25A-Sensor Variant. - Sensitivity range specified in the software: This range, the range which was specified earlier with s
etMeasRange()
, can be configured based on your specific application requirements.
So by considering both the sensor's variant and the specified sensitivity range (FSR), you can set the threshold at which the sensor detects an over-current with:
CurrentSensor.configOcd1();
or
CurrentSensor.configOcd2();
These methods take two parameters: a boolean value to enable the over-current detection (always set to true), and a predefined threshold value:
DISCLAIMER: These Threshold values are predefined in the library and are ONLY for the 25A-Sensor Variant (the one that comes with the Kit2Go) or the 75A-Variant.
Let's say, for example, we want to set a specific threshold value, such as 14.9A, so we search in the tables above and find 14.9A in the THR2_4
row and the FSR25
column of the OCD2 table. So to configure this, we have to use the OCD2 channel and the configOcd2()
method while having our measuring range set to FSR25
with setMeasRange(TLI4971::FSR25);
.
In code, it would look something like this:
CurrentSensor.setMeasRange(TLI4971::FSR25);
...
CurrentSensor.configOcd2(true,CurrentSensor.THR2_4);
The threshold values are written as:
the_name_of_our_TLI4971_instance.THRx_y
where the_name_of_our_TLI4971_instance corresponds in our example to CurrentSensor and the x represents the OCDchannel (either 1 or 2) and y represents the threshold index (1 to 8)
3.2.2.3 Hardware OCD (Lower)-ThresholdsWhen one of the OCD channels detects an over-current event and alerts the user, this alarm won't go off until the current passed through the channel is lower or equal to a user-specified value (Hysteresis principle):
You can set the hysteresis using the setOcdCompHyst()
function. It takes a threshold value from the table above and applies a hysteresis at 20% of that threshold.
For example, if the threshold is set at 15 amps (In our case THR2_4), a hysteresis value of 3 amps (=20% of 15 amps) can be used.
CurrentSensor.setOcdCompHyst(CurrentSensor.THR2_4)
This resets the over-current detection signal when the current drops below that value, ensuring stable and reliable detection.
3.3 Software OCD ApproachIn addition to the hardware-based approach, there is a software-based approach that provides flexibility and ease of implementation. It involves utilizing the microcontroller to handle over-current events.
After setting the measuring range and operation mode, you only need to use two methods:
The first one is the registerSwOcdFunction
()
: This method allows you to specify a current value in amps and the name of the function that should be called when that current value is reached.
For example:
CurrentSensor.registerSwOcdFunction(12.5,ocd_func);
In this case, the function named ocd_func will be executed when the current value reaches 12.5 amps.
The next thing is to set the hysteresis. This is done using the setSwOcdCompHyst() method: This method sets the lower hysteresis threshold value. When the current level falls below this threshold, the over-current detection (OCD) signal is reset.
For example:
CurrentSensor.setSwOcdCompHyst(5.0);
With this setting, the OCD signal will be reset when the current falls below 5 amps.
To retrieve the current value that triggered the software-based OCD event, you can use the getLastSwOcdCurrent() method. This method returns a double value representing the current that caused the event.
By utilizing these methods, you can implement software-based over-current detection and take appropriate actions when over-current conditions are detected.
3.4 Read OCD statesYou can get the Over-current detection(OCD) state of the sensor by using the following methods:
- getOcd1State( ): Hardware OCD Channel 1
- getOcd2State( ): Hardware OCD Channel 2
- getSwOcdState( ): Software OCD
Where all of these methods don't take any input parameters and they return a bool value, which is true if the respective OCD is triggered and false if it's not.
4.1 Example Code: Hardware OCD ApproachNow that you know how to set up your sensor, it's time to actually use it. You've got the basics of using the board down, so let's take a look at an example code using the Hardware OCD approach:
#include <TLI4971.h>
#if defined(XMC1100_XMC2GO)
TLI4971 CurrentSensor = TLI4971(A0, A1, 5, 8, 9, 3, 4, false); //XMC2Go
#elif defined(XMC1100_Boot_Kit) || defined(XMC4700_Relax_Kit) || defined(__AVR_ATmega328P__) /**< MyIoT Adapter Socket 1 */
TLI4971 CurrentSensor = TLI4971(A0, A1, 9, 5, 2, 10, 20, false);
#endif
void ocd1_func() {
Serial.println();
Serial.println("OCD1");
Serial.println();
}
void setup() {
Serial.begin(1000000);
while(!CurrentSensor.begin())
{
Serial.println("Current Sensor init failed!");
delay(100);
}
CurrentSensor.setMeasRange(TLI4971::FSR25);
CurrentSensor.setOpMode(TLI4971::SD_BID);
CurrentSensor.registerOcd1Function(CurrentSensor.INTERRUPT,ocd1_func);
CurrentSensor.configOcd1(true,CurrentSensor.THR1_8);
CurrentSensor.setOcdCompHyst(CurrentSensor.THR1_1);
}
void loop() {
// put your main code here, to run repeatedly:
long timer = millis();
int numReads = 0;
double avg = 0.0;
while(millis()-timer < 500)
{
avg += CurrentSensor.read();
numReads++;
}
avg /= numReads;
Serial.println(avg);
}
- The code sets up the sensor with a sensitivity range of +/-25Amps.
- It sets the operational mode to Semi-differential Bi-directional.
- The OCD channel is registered to Interrupt mode.
- The OCD routine is assigned to a function named ocd1_func, which prints "OCD1" on the screen when OCD1 is triggered
- The over-current threshold value is set to about 47 Amps, which corresponds to the 8th threshold value of OCD1 (OCD1rel_thresh_8).
- The hysteresis value is set to about 3.6 Amps.
The loop function repeatedly reads the current sensor and calculates the average current over a 500-millisecond period. Then the calculated average is printed to the Serial console.
4.2 Example Code: Software OCD Approach#include <TLI4971.h>
#if defined(XMC1100_XMC2GO)
TLI4971 CurrentSensor = TLI4971(A0, A1, 5, 8, 9, 3, 4, false); //XMC2Go
#elif defined(XMC1100_Boot_Kit) || defined(XMC4700_Relax_Kit) || defined(__AVR_ATmega328P__) /**< MyIoT Adapter Socket 1 */
TLI4971 CurrentSensor = TLI4971(A0, A1, 9, 5, 2, 10, 20, false);
#endif
void ocd_func()
{
Serial.println();
Serial.print("OCD reached at: ");
Serial.println(CurrentSensor.getLastSwOcdCurrent());
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(1000000);
delay(1000);
while(!CurrentSensor.begin())
{
Serial.println("Current Sensor init failed!");
delay(100);
}
CurrentSensor.setMeasRange(TLI4971::FSR25);
CurrentSensor.setOpMode(TLI4971::SD_BID);
CurrentSensor.registerSwOcdFunction(12.5,ocd_func);
CurrentSensor.setSwOcdCompHyst(5.0);
}
void loop() {
// put your main code here, to run repeatedly:
long timer = millis();
int numReads = 0;
double avg = 0.0;
while(millis()-timer < 500)
{
avg += CurrentSensor.read();
numReads++;
}
avg /= numReads;
Serial.println(avg);
}
This code technically does the same as the Hardware OCD Approach, but as you can see it's a lot easier.
You just need to:
- define the function that gets called when there is an Over-Current detected. Here it also prints out the last current value stored that triggered the OCD.
- set the upper Threshold with registerSwOcdFunction()
- set the lower Threshold value to complete the Hysteresis with setSwOcdCompHyst()
All of this belongs in the setup()-function of the code. The loop function is basically performing the same thing as the Hardware OCD Approach code.
5 FarewellAnd that's a wrap on our Hackster protip about using the TLI4971 current sensor with a microcontroller! Current measurement is a fundamental aspect of many electronic systems, and the TLI4971 sensor provides an accurate and reliable way to measure current in your own projects.
Don't hesitate to experiment and explore the possibilities of this amazing tool in your future projects. Keep being creative, stay safe, and have a great day!
Comments