Teensy 4.x using a Hall Effect Encoder

Status
Not open for further replies.

mjs513

Senior Member+
Ok all I am stumped. I picked up a pack of the Dagu Encoders (Hall Effect Sensor). Its has an input voltage range of 3-24v. So I hooked up Gnd to Gnd, Power to 3.3v and Signal to pin 2. Ok so far so good. I threw together a quick and dirty sketch:
Code:
const int enc_l_pin = 2;
volatile unsigned long enc_l = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(enc_l_pin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enc_l_pin), countLeft, CHANGE);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long num_ticks_l;
  enc_l = 0;
  unsigned long enc_l_prev = enc_l;

  while ( (enc_l < 20)) {
    Serial.println(enc_l);
  }
}

void countLeft() {
  enc_l++;
}
Loaded it on a T4.1 and nothing - interrupt never fired, always got 0 readings. Oh hooked up an LA to pin 2 and it read high even when rotating the wheel over the sensor. So I pulled out an Arduino Uno and ran same sketch and it worked so what am I missing in setting up the interrupt or if that's ok what else could it be.
 
wonder if their spec is wrong and needs 5v? You might try the same sketch with T3.2 or T3.5 with VIN = 5v and see if anything shows up?
 
@manitou - @KurtE

Ok went through the suggestions: (RED - didn't work, GREEN WORKED)
1. Adding 10k resistor doesn't get it working
2. Wiring correct the sensor comes prewired and color coded with dupont connectors so one less thing to remember.
3. Changing to FALLING does not work either

4. Attaching PWM pin to pin 2 does fire the ISR for the encoder so at least we know that works correctly
5. Attached the sensor to a T3.6 but left power connected to 3.3v and sketch worked ISR fired when wheel turned.


Weird isn't it.
 
I ran this sketch on my T4.0 with my adafruit hall sensor (3v3, GND, 10k pullup)
Code:
volatile uint32_t ticks;
void myisr() {
  ticks++;
}
void setup() {
  Serial.begin(9600);
  while (!Serial);
  attachInterrupt(2, myisr, FALLING);

}

void loop() {
  Serial.println(ticks);
  delay(2000);
}
sketch worked as i swished magnet across face of sensor. my adafruit hall sensor definitely requires 10k pullup between 3v3 and sensor output.

I mostly got your sketch to work, but I added delay(1000) in the while() to control excessive print output. Also I'm not happy with you zero'ing enc_l in the loop(). you can have a race condition with the isr which is also changing enc_l;

sketches worked on T4.1 too
 
Last edited:
@manitou

Thanks for hooking up the sensor. Got my problem resolved. Never even thought about the solution before.

I ran your sketch on a T4.0 and it worked but I made a couple of changes just to see if it would work with one addition: pinMode(2, INPUT_PULLUP.

So I went back to my original sketch but did a bit of clean up:
Code:
const int enc_l_pin = 2;

void setup() {
  Serial.begin(9600);
  pinMode(enc_l_pin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enc_l_pin), countLeft, CHANGE);
}

void loop() {
  unsigned long num_ticks_l;
  enc_l = 0;
  unsigned long enc_l_prev = enc_l;

  while ( (enc_l < 20)) {
    Serial.println(enc_l);
  }
}

void countLeft() {
  enc_l++;
}
and that worked as well on the T4.0. So went back and tested on the T4.1 but no luck until I changed the connection to GND to a different pin on my breakout board and then it worked.

I know you don't like zeroing the encoder count in the loop but not to worry this isn't the actual sketch I would be using. The code I usually use for the this type of encoder is buried in another more complicated sketch and I have a separate function call to zero out the count.

EDIT: This is Dagu Encode I am using: https://www.sparkfun.com/products/12629
 
Glad you got it working. I almost ordered a couple. But then if I get back to doing Wheeled Robot,

I will either go back to using Robotis Dynamixel Servos, like the ones on my Turtlebot 3 ROS setups.

Or like my old Lynxmotion Rover/Tri-track which has motors with encoders, something like: https://www.robotshop.com/en/lynxmo...in-1269-brushed-dc-gear-motor-w--encoder.html
And probably be lazy and use a motor controller with encoder handling, like the roboclaw: https://www.basicmicro.com/RoboClaw-2x15A-Motor-Controller_p_10.html

But I know that is getting lazy :D
 
@KurtE

Glad you didn't. Needed something quick and dirty to add to my Rocker-Bogie chassis from Servocity: https://www.servocity.com/bogie. It only has those cheap 6v motors so needed something like the Dagu Encoder. Was also toying with the idea of swapping those motors with those LewanSeoul servos I have but that would require more effort than I was will to put in at this point. Probably in the future will do it but not for this project. :)

My other big rover is the Dagu Rover 5 with their motor controller board: https://www.sparkfun.com/products/retired/11593 but that one is all hooked up with a bunch of 5v sensors with a Mega 2560. Didn't feel like taking it apart and redoing it just for some quick testing I wanted to do.

PS. Much prefer the motors with the built-in encoders as well. The other rover I have set up has those but that's otherwise tied up with a OpenMV project :)

Was actually looking at putting something like this together: https://www.youtube.com/watch?v=l90EvvPsKK8&t=64s, but would need some redesign
 
Looks like fun! What I would like is a version that can actually go up stairs. Actually what I would like is a house vacuum that can traverse my whole main floor of the house, where parts of it you may need to go up or down a couple of steps. But as I said I am getting lazy ;)
 
Ok got a new problem - think one of the T4.1's is dead.
1. Tried 15s restore, red light flashes but doesn't seem to want to reset and get BLink reloaded
2. Have 5v on the Vin
3. Have 3.3v on the 3.3.pin

Know somewhere Paul posted some detailed troubleshooting for the T4.1 but now can't seem to find it. Probably should have been added to wiki
 
Ok got a new problem - think one of the T4.1's is dead.
1. Tried 15s restore, red light flashes but doesn't seem to want to reset and get BLink reloaded
2. Have 5v on the Vin
3. Have 3.3v on the 3.3.pin

Know somewhere Paul posted some detailed troubleshooting for the T4.1 but now can't seem to find it. Probably should have been added to wiki

mjs513:

Is this Paul's detailed troubleshooting that you remembered (taken from https://forum.pjrc.com/threads/60900-Teensy-4-0-Snaffu?p=239532&viewfull=1#post239532http://):

One last thing to try is external 3.3V power. Connect 3.3V to VBAT first. This is important, since the RTC & power management part of the chip needs power first. Then connect to the 3.3V pin.

Good luck & have fun !!

Mark J Culross
KD5RXT
 
Thanks Mark - that's the one I was looking for. There was a longer list of things he had but that was the one I was trying to remember!
 
I assume you have done all of the normal debug/recovery things like:

Remove everything from it, i.e. no other IO pins connected if possible, and likewise check for shorts.

Try different USB cable. I have had a couple of times where one USB cable stops working with a specific teensy... Something like maybe contacts don't line up or something.

Try to use the simple blink program and do verify build and have the Teensy loader program up. Then plug in the T4.1 while holding down the program button. then release the button. Then press it again and see if blink downloads.

Check the program button voltage signal... A long time ago I had a T3.2 where the button failed..

Use hammer or put in vise and turn to make sure it is dead.
 
Use hammer or put in vise and turn to make sure it is dead.
Ok think I am at this point now. And yes tried all the normal stuff plus.

Not sure how I fried it since I don't have anything where I was putting 5v into it.
 
Again if it were me, and I did not succumb to my first impulses to use hammer, the last few things I would probably try might include:

Rubber mallet :eek:

Again plug in the T4.1 while holding program button down, and release. Have teensy loader setup to update with blink (usually I edit to change the blink to a different speed, such that I know it programmed).
Turn on Verbose Info and see if anything looks interesting when you try to upload. Like is the board being seen by your PC...

Then if I have soldered on pins and the like, I go through and touch up all of the pins again with solder iron, looking for any shorts. Also looking for anything I might have knocked off. Likewise if I had soldered anything like PSRAM or the like again carefully check all of the wires/pads for possible shorts...

Then wash board with hot water and use toothbrush, sometimes I use alcohol (on the board ;) ) then again clear water, and then try the board. I might use hair dryer or the like, but usually let it sit above our always on stove for several hours and then see if the board works.

Or if I am lucky enough to have a spare one, I just start with new one.

Again I know you know all of these steps, but in case someone else stumbles onto this thread as theirs does not work...
 
EDIT: This is Dagu Encode I am using: https://www.sparkfun.com/products/12629

Looking at the data sheets both the Adafruit and Sparkfun hall sensor are tri-state devices and need pullup on output pin. Dagu data sheet actually mentions using the MCU's internal pullup on the input pin. I had never thought about using INPUT_PULLUP mode instead of external pullup resistor. I confirmed that INPUT_PULLUP worked with T4. For many years, Teensy attachInterrupt() required that you explicitly do a pinMode INPUT for the interrupt pin. Arduino took care of that inside attachInterrupt(). Eventually Paul added code to set pin to INPUT in attachInterrupt(). Luckily, the setting doesn't mess with PULLUP setting.
 
Looking at the data sheets both the Adafruit and Sparkfun hall sensor are tri-state devices and need pullup on output pin. Dagu data sheet actually mentions using the MCU's internal pullup on the input pin. I had never thought about using INPUT_PULLUP mode instead of external pullup resistor. I confirmed that INPUT_PULLUP worked with T4. For many years, Teensy attachInterrupt() required that you explicitly do a pinMode INPUT for the interrupt pin. Arduino took care of that inside attachInterrupt(). Eventually Paul added code to set pin to INPUT in attachInterrupt(). Luckily, the setting doesn't mess with PULLUP setting.

Thanks for the added info. Never realized the history with attachinterrupt and pinMode. I remember in the past I had always input_pullup and it always seemed to work. Now I know why. Thanks for helping me sort it out
 
Status
Not open for further replies.
Back
Top