Greetings fellow makers! When thinking about any fancy project, you might almost certainly require some input. This input could potentially be a button, a microphone, a switch or even some fancy sensor like a radar. But what if there was something else. Maybe we could try to look at this from a different angle (*Badum TSSS*), AHA! What if you could use angle as input. As the title of this protip suggests, today we will be exploring using the TLE5012KIT2GO, which is an angle sensing solution packed in one PCB. (PSST it has a temperature sensor as well ;) )
The goal throughout this protip is that you:
- Understand what an angle sensor brings to the table, what it is and how to use it
- Understand how to program the TLE5012Kit2GO so that you could use it in your next project
The potential applications for an angle sensor in your project are limitless; it's like opening a new dimension for input. Take for example a project I worked on involving a critical gear rotation controlled by a motor, which was directly influenced by the angle sensor's readings.
But let's simplify things. Suppose you're prototyping, needing mechanical input for functionality testing. Traditional options like buttons or switches may spring to mind, providing straightforward binary input - 'on' or 'off', 'yes' or 'no'. But what if you need something more nuanced?
Enter the precision angle sensor, a device offering far more than a binary choice. It's like a 360-degree turntable of choices, transforming your project into a dynamic, interactive and detailed conversation. So let's explore the possibilities this tool offers.
FeaturesLet's take a better look at the kit we've got over here:
This beautiful piece of circuitry consists of two main characters:
- An XMC 1100 Microcontroller (so you could use this for implementing your logic as well)
- the TLX5012B Sensor
Of course as you could see in the photo there are other components involved, but all the other components are essentially working in the shadows for these two MVPs to operate correctly
There are four derivatives for the Sensor:
- TLE5012B E1000 version: automotive predefined variant with SSC and IIF communication protocols
- TLE5012B E5000 version: automotive predefined variant with SSC and PWM communication protocols
- TLE5012B E9000 version: automotive predefined variant with SSC and SPC communication protocols
- TLI5012B E1000 version: industrial predefined variant with SSC and IIF communication protocols
Note: If you just started your maker career you will most probably stick to SSC, and that's exactly what we will be using through out this protip!
Abbreviations
SSC: Synchronous Serial Communication. IIF: Incremental InterfacePWM: Pulse Width ModulationSPC: Short PWM Code.
For more concrete information about these protocols, check out the datasheet by clicking here!
And as we're at it, let's talk a bit about the specs:
- 360° magnetic angle sensor
- Integrated GMR technology (We'll talk more about that below)
- On chip signal processing with angle calculation
- 42µs update rate at 15bit angular resolution
- Multiple digital interfaces
- Supply voltage 3.3 or 5.0V
- -40°C to 150°C
Good Question! To answer it we need to understand how the sensor works.
The IC has these in-built components called Giant Magneto Resistance elements, or iGMR for short. These little guys are fantastic at measuring the 'sine' and 'cosine' components of the magnetic field. These might sound like scary math terms, but they're just ways of describing different aspects of the field, much like how we might describe a journey in terms of distance and direction.
Once the TLE5012B has these raw measurements (the sine and cosine), it gets to work processing them internally, just like solving a puzzle. By piecing together these parts, it's able to calculate the orientation, or angle, of the magnetic field.
So?
So we just need a magnet and voila! As soon as the magnet is close enough to the sensor IC, it will detect it and calibrate itself to it's magnetic field. And if there is a change in the magnetic field lines getting to the IC (either the IC itself rotates or the magnet itself does) this rotation will be registered.
For a better representation we could take a look at some of the official accessories that Infineon provides. (These are also 3D printable and the files are on the website).
Disclaimer: These attachments were initially designed for Infineon's 3D magnetic sensors. (This board is 2D) so not all attachments on the website are optimal, since in some accessories the magnet also moves not only in 2 axes, but also in three (such as for example the Joystick accessory). We could nevertheless still use it. However we will have to keep in mind, that we will only be using the magnets in 2D. Nevertheless, principle is always the same, there's a magnet and it gets attached on top of the sensor ;) )
Before diving into programming the kit ourselves (which is extremely simple and straight to the point I promise!!!!) feel free to check out this presentation on the sensor and a graphical user interface tool that tests it and provides an extra interactive interface to see how the sensor works. (It's only three minutes)
Getting our Hands DirtyArduino Library Installation
Before we start using the board, we'll have to install its library, that makes our lives easier when programming.
ToInstall the library. In the Arduino IDE, go to the menu Sketch > Include library > Library Manager. Type TLE5012 and install the library.
Setting up the sensor
Arduino code basically consists of two parts: a dynamic and a static part.
The static part or "the skeleton" as I like to call it: is just the part where all the initialization is done and we tell the Arduino essentially what it's connected to. The skeleton part is ran only once and then the Arduino jumps onto the dynamic part.
The dynamic part is the part, where we tell the Arduino "Hey I need you to do this and that, and in case of a certain scenario maybe do this" and so on. It's the part where we explicitly instruct the microcontroller to do stuff. This part is continuously ran, as long as there's power delivered to the microcontroller.
QuickNote: You don't need to understand all the setup code! So if you just started with your maker career, it's okay all you have to do is copy the setup skeleton code as a bulk and modify the dynamic part of the code:
setup skeleton
#include <TLE5012-ino.hpp>
Tle5012Ino Tle5012Sensor = Tle5012Ino();
errorTypes checkError = NO_ERROR;
void setup() {
Serial.begin(9600);
while (!Serial) {};
checkError = Tle5012Sensor.begin();
Serial.print("checkError: ");
Serial.println(checkError,HEX);
delay(1000);
Serial.println("Init done");
}
If you decided to not just copy the skeleton code and maybe dive into what constitutes the code then first of all I would like to congratulate you (truly,your boldness inspires me).
First, include the library at the top of your Arduino sketch:
#include <TLE5012-ino.hpp>
Next, create an instance of the TLE5012Ino class:
Tle5012Ino Tle5012Sensor = Tle5012Ino();
This is like telling your Arduino "We have a TLE5012 sensor, and we're going to call it Tle5012Sensor.
Now we need to get in the setup function:
We also need to keep track of any errors that might occur when we're setting up or using the sensor. We'll do this with a variable called checkError
:
errorTypes checkError = NO_ERROR;
At this point, checkError is set to NO_ERROR, because we haven't encountered any problems yet.
Now, we move on to the setup()
function, which is where we'll initialize the sensor and set up communication with your computer.
We start the Serial communication:
Serial.begin(9600);
The Serial.begin(9600);
command starts the Serial communication at a baud rate of 9600. This will allow the Arduino to send data to your computer.
We want to make sure the Serial communication is set up correctly before we proceed, so we include this line:
while (!Serial) {};
Now, we're ready to start the TLE5012 sensor:
checkError = Tle5012Sensor.begin();
The Tle5012Sensor.begin();
command starts the sensor and returns an error code that gets stored in checkError
.
We can then print this error code to the Serial monitor, which will help us diagnose any problems:
Serial.print("checkError: ");
Serial.println(checkError,HEX);
The Serial.println(checkError,HEX);
command prints the error code in a hexadecimal format, which is a common format for error codes.
After another brief pause, we print a message to the Serial monitor to confirm that the initialization is done:
delay(1000);
Serial.println("Init done");
And that's it! You've set up your TLE5012 sensor with your Arduino. With this setup, your Arduino can check the sensor, let you know if there are any problems, and communicate the results to you over the Serial monitor.
BasicFunctions
The sensor provides a lot of bells and whistles to tinker with. The most important ones being the angle and temperature and here's the good news, using these functionalities is super simple (like really - it's just one line of code).
Angle
To get the angle all you have to do is call the getAngleValue()
method. Additionally you have to feed this method with a variable (of type double), where it will store the angle values.
Let's say we want to create a variable, store the angle value in it and then print it out in the serial monitor. In that case all we'll do in the loop function is:
double angleReading = 0.0; //Variable Creation
Tle5012Sensor.getAngleValue(angleReading); //Calling the method
Serial.print("angle:"); Serial.println(angleReading); //Printing the result
The angle value that is then returned is a value between -180 and 180 degrees.
Temperature
Analogous to the angle values, you could also obtain the temperature reading with just a single method. All you have to use is the getTemperature()
method. This method also takes in a variable of type double, where it saves the temperature values onto.
Let's say we want to create a variable and then store the temperature value in it and then print it out in the serial monitor. In that case all we'll have to do in the loop function is:
double tempReading =0.0; //Variable Creation
Tle5012Sensor.getTemperature(tempReading); // Calling the method
Serial.print("Temperature:"); Serial.println(tempReading); //Printing the result
Let's say we take those last two code snippets and slap them on the skeleton and see how that code behaves?by just copy pasting we get the following code
#include <TLE5012-ino.hpp>
Tle5012Ino Tle5012Sensor = Tle5012Ino();
errorTypes checkError = NO_ERROR;
void setup() {
delay(2000);
Serial.begin(9600);
while (!Serial) {};
checkError = Tle5012Sensor.begin();
Serial.print("checkError: ");
Serial.println(checkError,HEX);
delay(1000);
Serial.println("Init done");
}
void loop(){
double angleReading = 0.0; //Variable Creation
Tle5012Sensor.getAngleValue(angleReading); //Calling the method
Serial.print("angle:"); Serial.println(angleReading); //Printing the result
double tempReading =0.0; //Variable Creation
Tle5012Sensor.getTemperature(tempReading); // Calling the method
Serial.print("Temperature:"); Serial.println(tempReading); //Printing the result
}
when we run the following code in the Arduino IDE we get the following application
As you can see in the GIF above, the microcontroller constantly gives us new readings for the temperature and angle.
Joystick Input ExampleNow let's spice this up a little bit, let's try to see if we could use the angle sensor to mimic the input of a joystick. To do this all we'll have to do is use our previous code that we hooked up together. We will essentially just move our joystick addon up, down, left and right and just record the readings of the sensor. Based on that we will determine the value windows for each direction. After monitoring these values I just wrote if conditions so that in the dynamic part that's always running, the microcontroller read's the angle value from the sensor and then check's to see, to which direction does the angle value belong and so it prints that direction.
Additionally I used an integer value to represent to direction of input, so that I could use that further on if I decide to build an application, that is dependent on this input.
#include <TLE5012-ino.hpp>
Tle5012Ino Tle5012Sensor = Tle5012Ino();
errorTypes checkError = NO_ERROR;
double positionReading = 0.0;
int input=0;
void setup() {
// put your setup code here, to run once:
delay(2000);
Serial.begin(9600);
while (!Serial) {};
checkError = Tle5012Sensor.begin();
Serial.print("checkError: ");
Serial.println(checkError,HEX);
delay(1000);
Serial.println("Init done");
}
void loop() {
// put your main code here, to run repeatedly:
Tle5012Sensor.getAngleValue(positionReading);
if (positionReading >= -45 && positionReading <= 45) {
Serial.println("Up");
input=1;
}
else if (positionReading < -45 && positionReading >= -120) {
Serial.println("Right");
input=2;
}
else if (positionReading < -120 || positionReading > 120) {
Serial.println("Down");
input=3;
}
else if (positionReading > 45 && positionReading <= 120) {
Serial.println("Left");
input=4;
}
delay(200);
}
Let's see how the code looks like in action!
What extras does the library offer?
There are definitely more features that we did not talk about here, but feel free to explore what the library has to offer by checking out the Arduino code examples of the library. You could reach them by opening your Arduino IDE and hovering over File>Examples>TLE5012B
Angle Sensor vs 3D sensor?
2D Magnetic Sensors: These sensors measure magnetic fields in a flat plane (like the compass direction of a magnet relative to the sensor). They can't detect if a magnet is moving up or down.
3D Magnetic Sensors: These sensors measure magnetic fields in all three dimensions - not only can they detect the compass direction of a magnet, but they can also tell if it's moving up or down relative to the sensor. It's like having a full picture of the magnetic field's orientation and strength.
More Info about 3D Magnetic Sensors?
Interested in 3D magnetic Sensors? You're in luck because we also have a protip covering Infineon's 3D magnetic sensory solutions. (Click here for more details)
Angles Sensor vs Potentiometer?
Angle Sensors (Magnetic 2D): These sensors use a magnetic field to determine the angular position or orientation of an object. They are non-contact devices, which means they don't need to physically touch the object whose position is being measured. This makes them more durable as they don't wear out from friction. They can also typically measure a full 360 degrees rotation and are less affected by environmental conditions like dust or dirt.
Potentiometers: These are a type of resistor with a variable resistance that can be manually adjusted. When used as angle sensors, they operate on the principle of mechanical rotation. As the potentiometer is turned, it changes the resistance, which can be measured and translated into an angle. However, they usually can't measure a full 360 degrees rotation, and being contact devices, they can wear out over time due to friction. Also, they are more affected by environmental conditions and require more maintenance.
Comments