How to Read the Signal from a Hall Effect Sensor Using an Arduino

How to Read the Signal from a Hall Effect Sensor Using an Arduino

Adam Morissette
Adam Morissette
PA Engineer

Hall effect sensors are one of the feedback options we offer in our linear actuators to provide position and speed information. Our PA-04-HS and PA-100 have Hall effect sensors on the stock units. While several of our other actuators can be custom ordered with Hall effect sensors.

Browse our range of Arduino microcontrollers for ultimate actuator control!

WHAT IS A HALL EFFECT SENSOR?

A Hall effect sensor is an electronics component that generates a voltage based on the strength of the magnetic field flowing through it. When this sensor is paired with voltage threshold detecting circuitry a signal with two states can be produced. In our actuators, the hall effect sensor is designed to produce a two channel signal with the waveforms quantified  in either of two binary states; on, or off. These two signals will rise and fall as the electric motor rotates with a 90° phase difference between them, as shown below. The frequency of these pulses as they relate to the change of position in the actuator is dependent on their overall resolution and differs between our different actuators.

Hall effect sensors 

HOW ARE THESE HALL SIGNALS READ?

How to use a Hall effect sensor

When reading a digital signal on a microcontroller there are two main methods; polling, and interrupts. Polling is a programmed method in which a microcontroller periodically checks the state of an input to see if there has been a change. While interrupts are a hardware mechanism that immediately shifts the focus of the microcontroller’s program when the signal changes on an input. Each of these methods has its pros and cons, and each has applications that they are better suited for. For our case, we will want to know the exact moment when a signal changes state, so we will be using interrupts. In order to use a hall effect sensor with Arduino microcontrollers an interrupt is used. By creating an ISR or Interrupt Servicing Routine the arduino can be made to execute a section of code immediately when a defined change is detected on a specified input. An example of an ISR for an Arduino is shown below, a modified version directing the detected signals to LEDs is one way on how to test a Hall effect sensor.

Programming an Arduino to read Hall effect sensors - interrupt method

// global volatile variables are needed to pass data between the

// main program and the ISR's

volatile byte signalA;
volatile byte signalB;

// the pins that can be used with interrupts depend on the board you

// are using
const byte inputA = 2;
const byte inputB = 3;

void setup() {
  // enable internal resistors on the input pins
  pinMode(inputA, INPUT_PULLUP);
  pinMode(inputB, INPUT_PULLUP);
  // read the initial state of the inputs
  signalA = digitalRead(inputA);
  signalB = digitalRead(inputB);

  // will detect a rising or a falling edge
  attachInterrupt(digitalPinToInterrupt(inputA),signalA_ISR,CHANGE);
  attachInterrupt(digitalPinToInterrupt(inputB),signalB_ISR,CHANGE);

}

void loop() {
  // This is where the signal information can be used in a program
}

void signalA_ISR() {
  // when a change is detected it will always be

  // to the opposite of the current state

  signalA = !signalA;
}

void signalB_ISR() {
  signalB = !signalB;
}

Our PA-04-HS comes with built-in Hall effect feedback!

WHAT NEEDS TO BE CONSIDERED IN THE APPLICATION?

Since the signals we are reading are going to be high frequency there are a few considerations that need to be made. Firstly, how long will the program take to execute the code in the ISR? How many separate signals need to have ISR’s? How fast is the microcontroller’s clock speed?

A problem that can be encountered in a program with a lengthy ISR is that the ISR will be triggered again before it has completed the code it contains from the previous time it was triggered. It is recommended to keep the minimal amount of code necessary in an ISR to help avoid this problem.

In the example code above, two signals are set up with separate interrupts. Both signals are needed in order to detect the direction of movement of the linear actuator, this is done by checking which signal is changing from low to high before the other. The downside to enabling interrupts on both of the signals is that there is twice as much ISR code that will be run. In applications where the direction of movement of the actuator is not needed or is already apparent from the program that is running, only one signal would need to be set up with an interrupt service routine.

Some microcontrollers have the ability to have the clock speed changed to be faster. The clock speed changes the rate at which the microcontroller can run the program. If the frequency of the signals being read is high, then the clock speed may have to be increased to keep up. However, it is more power-efficient to use as slow of clock speed as the application will allow.

WHAT HAPPENS IF THE MICROCONTROLLER IS NOT FAST ENOUGH?

After the above considerations have been made, sometimes the microcontroller is just not fast enough to get through the main code and keep up with the ISR’s. In these cases, you may want to make use of an additional microcontroller. One microcontroller can be used to run the ISR’s, read the data, then transmit the data needed to another microcontroller where the main code can be executed without being interrupted.