It doesn't matter when you were born, when you hear Pac-Man, Space Invaders, or Street Fighter the first thing that probably pops into your mind are those big arcade machines where you typically had one joystick and 2 push buttons to control a gaming character or an object. Those push buttons were probably realized using a simple mechanism:
- Inside the button housing, there are typically two metal contacts, normally open
- when the button is pressed, these contacts are brought together, completing an electrical circuit
A even longer lasting option is to use Infineons Hall Switch Shield2Go, which provides enhanced sensitivity and longevity also allowing precise and consistent position detection of button presses and other position measurements.
Read along and find out how with the help of Arduino IDE!
1 Specs and Functionality1.1 Crash Course: Hall EffectWhen an electric current flows through a conductor, such as a metal or semiconductor material, and a magnetic field is applied perpendicular to the current flow, the moving charged particles (usually electrons) experience a force called the Lorentz force. This force causes the charge carriers to be deflected towards one side of the conductor, accumulating charges on that side.
Due to the accumulation of charges, an electric field is established in a direction perpendicular to both the current flow and the applied magnetic field. This electric field generates a voltage across the conductor, which is known as the Hall voltage. The Hall voltage is directly proportional to the product of the current, magnetic field strength, and a constant called the Hall coefficient.
By measuring the Hall voltage, one can determine the magnitude and polarity of the magnetic field applied to the conductor. This principle forms the basis for Hall effect sensors, such as the TLE4964-3M, which are designed to accurately detect and measure magnetic fields.
The TLE4964-3M is a special kind of switch called a Hall effect switch. It can handle different power levels, works in a wide range of temperatures, and stays stable even when the temperature changes.
Put it next to a Resistor, TVS-Diode and a Capacitor, et Voila! You have your Shield2Go:
For the spec freaks, the TLE4964-3M:
- has an operating temperature range from -40°C to 170°C
- is capable of handling supply voltages from 3.0 V to 32 V
- has active error compensation
- ensures high stability of magnetic thresholds
and so on and so forth. For more info, you could always refer to the datasheet of the Sensor.
This Sensor also follows the hysteresis principle, for simplicity purposes, the lower and upper thresholds (B_RP and B_OP respectively) are already predefined. Have a look at the table below:
The sensor has an active low Output, meaning that when a magnetic field is present and the strength (fluxdensity) of this magnetic field is greater than B_op the output is pulled to LOW until the strength is less than the B_rp then the output gets pushed to HIGH again. The difference between B_op and B_rp is what we call "Hysteresis" (second picture) and equals the B_hyst value.
2 PinoutHere is the pinout Diagram:
Just make sure that the voltage applied on any pin of the Shield2Go itself does not exceed 4V.
CAUTION: While the TLE4964 (the chip itself) can handle up to 32V as supply voltage, the microcontroller (µC) board you're using to read the output with will probably NOT withstand such voltage because the voltage of output Q depends on VDD (=up to 32V).
You'll find the J2 Bridge soldered by default on the Shield2Go, so GPIO3 is equivalent to Q (= the Output of the Sensor).
3 Code and LibraryIf you're not familiar with XMC-for-Arduino, check out this post to get a headstart!
Know your way out? Then go ahead and mount the Shield2Go on top of your XMC, plug it in, and let's do this!
First, fire up the Arduino IDE and install the library from Library Manager:
or clone the GitHub repository and add it as a.ZIP Library.
3.1 A typical Hall-SwitchTo start programming the Shield2Go, include the library first:
#include <hall-switch-ino.h>
This is where we part ways; There is the 'interrupt' way and the 'polling' way to deal with this Shield2Go. For the INTERRUPTmethod, you can begin by calling the Constructor of the HallSwitchIno
class:
HallSwitchIno hs(TLE4964_3M_S2Go_XMC2Go, IntCBack);
TLE4964_3M_S2Go_XMC2Go
:
actually only holds the output pin (you can replace that with '9')IntCBack:
the interrupt handler function. When an interrupt occurs this function gets called
then define your IntCBack
function:
void IntCBack(HallSwitch::Result_t result)
{
if(result == HallSwitch::Result_t::B_FIELD_ON)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else if(result == HallSwitch::Result_t::B_FIELD_OFF)
{
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(result);
}
This basically turns the LED ON if a magnetic field is present and stays OFF otherwise.
NOTE: The LED on the Shield2Go only indicates that the board is powered properly (=ON). The LED that turns on and off based on the presence of a magnetic field is located on the µC-Board you're using (here the XMC2Go).
Then you'll just have to initialize serial communication and define the pinMode in the setup() function and call the begin()
function of the HallSwitchIno
Class:
void setup()
{
int err = 0;
delay(1000);
Serial.begin(115200);
Serial.println("Serial initialized");
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("LED1 initialized");
err = hs.begin();
if(0 > err ){ Serial.print("Hall Switch error");}
Serial.println("Hall Switch started");
}
you can leave the loop()
-function empty or do something on the side, it's up to you!
The POLLING method is actually pretty similar to the interrupt method. Call the constructor with only one parameter at the beginning:
HallSwitchIno hs(Q_OUTPUT_PIN);
Then in the setup function, just initialize serial communication and define pinModes
as shown before.
Now in the loop()
- function type in:
void loop()
{
curbfield = hs.getBField();
if (curbfield != bfield)
{
bfield = curbfield;
if(1 == bfield)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else if(0 == bfield)
{
digitalWrite(LED_BUILTIN, LOW);
}
else if(0 > bfield)
{
Serial.println("Hall swith error.");
}
Serial-println(bfield);
}
}
- Only updates LED Status and data when the B field changes
hs.getBfield():
pretty much only reads the Q Output pin
Not your typical Hall Switch, right?! You can easily extract speed (RPM) with a Hall switch. The concept is:
- As the magnetic object spins, the Hall switch detects the magnetic field changes.
- Each time the magnet passes the Hall switch, it triggers a signal.
- Count the number of signals within a specific time frame (e.g., one minute).
- Divide the signal count by the time frame to calculate the RPM.
Well if it's complicated, then you're lucky because there is a function that gets the RPM (or Hertz or Rad/s) for you.
At the start you'll have to include another library:
#include <hall-speed-ino.hpp>
Once again you can choose between 'interrupt' or 'polling'.
If you go for the interrupt way, define your desired call-back function first then call the constructor:
HallSpeedIno hs(TLE4964_3M_S2Go_XMC2Go, 1, HallSpeed::HERTZ, JSONPrint);
TLE4964_3M_S2Go_XMC2Go:
Platform you're using1:
is the number of magnetic pole pairs, it's 1 by defaultHallSpeed::HERTZ:
speed unit of the measurement (can beRPM
orRADS
for rad/s)JSONPrint:
Call back function, this only prints out the speed in JSON format.
Once again only the setup()
function requires your attention when using the interrupt method:
void setup()
{
int err = 0;
delay(1000);
Serial.begin(115200);
Serial.println("Serial initialized");
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("LED1 Initialized");
err = hs.begin();
if(0 > err ){ Serial.print("Hall Speed error");}
Serial.println("Hall Speed started");
}
You can leave the loop() function empty if you want to.
For the polling method call the constructor at the beginning but without adding a callback function, something like this:
HallSpeedIno hs(TLE4964_3M_S2Go_XMC2Go, 1, HallSpeed::HERTZ);
You know the drill in the setup()-
part:
void setup()
{
int err = 0;
delay(1000);
Serial.begin(115200);
Serial.println("Serial initialized");
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("LED1 Initialized");
err = hs.begin();
if(0 > err ){ Serial.print("Hall Speed error");}
Serial.println("Hall Speed started");
}
the main action takes place in the loop()
part with the help of hs.getSpeed()
method:
void loop()
{
double curSpeed = hs.getSpeed();
if (curSpeed != speed)
{
speed = curSpeed;
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("speed: ");Serial.print(speed);
Serial.println();
}
else
{
digitalWrite(LED_BUILTIN, LOW);
}
}
4 FarewellYou may now call yourself a true Hall Switch Master.
You may also want to check out other examples from the library and learn e.g. how to use the sensor with just the basic Arduino libraries.
Check out this video and see the potential of the TLE4964-3M Hall Shield2Go for yourself:
Now it's safe to say, the TLE4964-3M Hall Shield2Go is indeed a true gamechanger.
Impressed with the gaming station? Build it yourself with the help of this post!
Stay safe, keep making, and goodbye!
Comments
Please log in or sign up to comment.