Using a 5v hall sensor to 3.3v eensy 4.1 via SN74 difficulty.

walrus20

Member
Hi all,
I got my hands on a few DC motors (no label/name except 12v/5A and hall sensor name sharpied on it) with this hall effect sensor soldered to it. Pages 4&5 have the specs. I have a 5V power supply used for the sensor.

set up:
Same code, different pins used, difference is using this SN74 logic shifter with the teensy.

I have 2 software debounced switches, some motor driver board, and this sensor. All from the same 2 channel power supply unit, one with a 12v rail to the motor driver for the motor, the other via a step down converter to 5v to power the teensy and the hall sensor.

My issue is I am unable to read the hall sensor data in my 4.1 board via interrupts. I was able to get this working correctly using an Arduino Mega board, but not with the Teensy 4.1 I think because the logic voltage.

Code used:

Code:
//global:
volatile long hallCount=0;

void setup()
{
  pinMode(hallSensor1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(hallSensor1), hallCounter, LOW);
}
//I tried changing the ISR to changing/rising, no luck. 

void hallCounter() {
  if (motorFWDS== 0)  //a flag to add/subtract the hall pulse depending on motor direction
 hallCount=  hallCount+1;
  else
  hallCount= hallCount-1;
}

What I have tried:
2 logic level converters/shifters
1. TXS0108E. I think same issue as with the SN74, but gave up early with this.
2. SN74AHCT125 (saw this suggested in various threads here). I realized this is used for 3.3v ->5v, but the adafruit website said if I powered it with 3.3v and had a 5v input, it'll have a 3.3v friendly output.

Correct:
In the arduino mega board, the hall output directly connected to the arduino I/O pin. It was pulled high, triggered on LOW as seen in the code. So at default, the hall outputs 5v, and testing the halls across 5 motors gave an avg ~6000 pulses/second at full speed.

Incorrect:
In the teensy, I use the SN74 logic shifter. I have 1OE to GND to activate it, 1A to the hall 5v output, and 1Y to the teensy 4.1 I/O pin. Same code as above, hall pulled high, triggered on LOW. So at default/rest (motor not moving), the hall reads the correct 3.3V, but when I push my switch to rotate the motor, the SN74 hall sensor output doesn't trigger the LOW ISR signal.
- I tried 2 things.
1. SN74 output not connected to teensy. When switch pressed, motor rotates, voltage will show the 3.3v to 1.65v when the motor is on (I think it shows 1.65v bc it's the mean/rms of 0v and 3.3v)
2. SN74 output connected to teensy 4.1 pin 4, most times the switch and motor no longer work (ie pressed switch and no motor rotation, and thus, no hall voltage changes).


If I change the ISR to "CHANGING," and plug in the SN74 output to my teensy, my ISR triggers when the motor is rotating and the hall count gives me insanity levels of pulse counts that make zero sense (ie like 100,000+ counts within 2-3 seconds). I get why the changing is no good as it's in the middle/floating b/w a low and high logic lvl.

My ideas:
I don't have an oscilloscope, but I think I need one. I have a pack of resistors, capacitors, and zener diodes.

Few thoughts:
1. SN74 datasheet says "All unused inputs of the device must be held at VCC or GND to ensure proper device operation. Refer to the TI application report,
Implications of Slow or Floating CMOS Inputs, literature number SCBA004
. So since these OE pins are active when pulled to low/ground, I should pull the unused OE pins/channels to VCC?

2. Buy the 74LVC125, which is designed for 5v -->3.3v logic direction. Or if there's a "newer/better" chip/component?

3. his post here mentions decoupling capacitors. Lowest capacitor I have is 0.1uF. Should I attached this b/w ground and hall sensor Voltage input using the correct polarity? I noticed my halls on the arduino would slowly trigger (1-2 triggers every 2-3s) even when my motors weren't rotating. I assume this is the noise issue that the 0.1uf capacitor could resolve.


Besides those ideas, I have no clue what to do and would appreciate any suggestions/input. I am currently learning about basic electronics so I am not too great with understanding how to resolve what appears to be a simple issue.
 
Last edited:
The Hall sensor's datasheet indicates that it has open collector output so I suggest using a 1K0 pullup to +3.3v.
 
Did you power the Hall sensor with 5V while pulling up the output pin to 3V3 like MatrixRat suggested?

Paul
 
Hi @PaulS and @MatrixRat

I did what you both suggested, added a 1k resistor from the sn74 output to the 3.3 vcc and it started working with my teensy rather than the teensy being fully unresponsive. A huge step forward towards the right direction. Thanks for both of your inputs.


I forgot to mention I received these motors from a college lab and there are 2 halls soldered to it. Hall sensor 1 has a "leading signal" when the motor is rotating right, and hall sensor 2 has a "leading signal" when the motor rotates left. So I tweaked the code to add both simultaneously because they share the same one VCC and GND wires from being soldered the the motor. Plugging one hall properly but not the other may have been also causing issues.

The hall counts were still off so I did the proper set up: (ran all unused OEs to VCC (+3.3v) so they remain off and reduce noise/floating issues from the SN74 chip.

I then added a 0.1uf Capacitor between GND and the +3.3v rail that powered the SN74, the pulse counts cleaned up pretty well. I tested it out a few times, and then also with a 0.22uf capacitor for comparisons sake.

I also added snap on ferrite rings on all of the wires. Not sure if it made a difference.

Code:
//global:
volatile long hallCount=0;

void setup()
{
  pinMode(hallSensor1, INPUT_PULLUP);
  pinMode(hallSensor2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(hallSensor1), hallCounter1, RISING);
  attachInterrupt(digitalPinToInterrupt(hallSensor2), hallCounter2, RISING);
} 

void hallCounter1() {
  if (motorFWDS== 0)  //means motor rot right, so add to hall count
 hallCount=  hallCount+1;
}
void hallCounter2() {
  if (motorFWDS== 1)  //means motor rot left, so subtract from hall count
 hallCount=  hallCount-1;
}

I noticed the current, far minor, challenge:

With the 0.1uf cap, however much I run the motor in the right direction, when I turn it the opposite direction to return to an equal number of rotations/revolutions going left, the hall sensor count ISR triggers more rotating left vs when the motor turns right.
I ran a test to spin the motors both ways for 5, 10, ... 30s at a time one way and then the opposite to try to get the halls back to 0, but the hall count always runs about 3-5% more negative. I used a software motor run time with a delay between motor direction change.

EX)
Trial 1:
With 0.1uf capacitor, For a 30s rotate right, minor pause, then left, hall count starts at 0, goes up to 29,992 counts, then rotating right for 30s, it goes back to -950 counts. In the 2nd trial, same 30s, it went from 0 -> 22,892 -> -822 hall counts. In the Arduino, the hall counts get within 1% each time, within +/- 100 pulses vs +/- 1000 pulses on my teensy. So I am still not optimizing my logic shift circuitry well.

Trial 2:
Used a 0.22uf capacitor for comparison's sake (to see if the hall count discrepancy's were a noise issue), and I got obscure counts. In the same 30s trial of spinning the motor right then left, the halls went: 0 -> 32,000 -> 12,900. So this time the hall sensors' ISR triggered more when the motors spun right and less when the motors spun left.

I think the imbalance ISR triggering is because of some noise issue and/or I used 2 1k resistors going to the +3.3v rail, but the 0.22uf capacitor trial makes me think I need to get an oscilloscope to learn to use/understand what's going on with the halls when I spin the motor both ways.

I drew a basic outline (apologies for using paint vs a nice schematic) draw.jpg
 
Those Hall sensors are specified to run from 4-24V, so running them at 3V3 might not lead to satisfactorily results.
You can power both Hall sensors from the Teensy Vin (5V) pin [assuming it is powered over USB].
Then connect both Vout's, with a 1K pullup-resistor to 3V3, to the Teensy pins. Forget about the level shifter.
Another suggestion is to use the Encoder library. This basic code should show something:
Code:
#include <Encoder.h>
Encoder myEnc(4, 5);

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Paul
 
I messed up the drawing. I have the halls connected to a separate 5v rail. (I have a multi channel PSU, all powered components share the same ground).

I am going to try your pulse count method. I think it will be more precise than what I was doing.

Thanks again for your help.
 
Back
Top