Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 17 of 17

Thread: AS5047, SPI, and Teensy 4.0 always outputs 0

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    9

    AS5047, SPI, and Teensy 4.0 always outputs 0

    Hey guys.

    I'm writing a high-speed rotary encoder for Teensy 4.0 and as I'm sure you guessed, running into issues.

    my code is this:

    Code:
    // Include the library
    #include <AS5X47.h>
    
    // Define where the CSN Pin in connected. 
    int slaveSelectPin = 10;
    
    // Start connection to the sensor.
    AS5X47 as5047p(slaveSelectPin);
    
    void setup() {
      // Initialize a Serial Communication in order to
      // print the measured angle.
      Serial.begin(9600);
    }
    
    void loop() {
      // Read the measured angle
      float angle = as5047p.readAngle();
    
      // Show the measure angle on the Serial Port
      Serial.println(angle);
    
      // Wait 1 second (1000 milliseconds) before reading again.
      delay(1000);
    }
    I'm using Teensy 4.0, it's in 24mhz mode.

    The pin diagram is as follows:
    Teensy -> AS5047
    5V -> 5V
    Gnd ->
    Pin10 -> CSn
    Pin11-> CLK
    Pin12-> MOSI
    Pin13-> MISO

    Picture:

    Now, on the standard arduino board I uploaded this to, the serial monitor outputted values from 0 to 360 as expected as the magnet rotated, as expected. However, in the teensy 4.0 there is only 0 values coming down. The debug system also reported 0 values on everything.

    Anyway, I would appreciate assistance figuring this out. Thanks a lot!

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    7,570
    Quote Originally Posted by StefanB View Post
    The pin diagram is as follows:
    Teensy -> AS5047
    5V -> 5V
    Gnd ->
    Pin10 -> CSn
    Pin11-> CLK
    Pin12-> MOSI
    Pin13-> MISO
    First of all - are you sure the encoder output is max 3.2 Volts? Any voltage above that can destroy the Teensy in millisconds..

    Then, your PIN-Mapping sounds a bit strange to me. If you take a look at the card, you'll see this mapping for SPI:
    MOSI: Pin 11
    MISO: Pin 12
    SCK: Pin 13

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Hey Frank!

    Thanks for the response. The encoder is not putting out any voltage at all, it's taking power from the Teensy. I also tried on independent power with no luck.

    You're right. the pinout that I wrote down is incorrect, the actual pinout that you wrote down is the right one. You can actually see this in the image.

    Thank you for the response, and I hope this helps diagnose my issue!

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,526
    The T4.0 is not 5V tolerant so you must have the AS5047 board configured for 3V3 operation which involves feeding it the 3V3 supply from the T4.0 and setting the jumper at JP1 correctly.

    Pete

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Hey Pete,

    I re-wired the board to use 3.3V only. Same issue still presents.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    115
    If you have used the AS5047 with 5v with the teensy 4 , then because the o/p pins of the AS5047 to the teensy would have a logic 1 level of 4.5v (see AS5047 datasheet) it is highly likely that those pins on the teensy have been destroyed. Try removing your circuit from the teensy and try reading input 0 and 1 (with 3.3v as a 1) on the pins you have been using with a simple program. If you can detect changing states on the teensy pins then it might be ok. Otherwise it has been damaged.

  7. #7
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Bri,

    All 3 pins checked good. I checked them by println to serial if they were high. They all outputted correctly.

  8. #8
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    115
    Did you also check them outputting low? They could be stuck high.

  9. #9
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    340
    The AS5X47.h library, https://github.com/Adrien-Legrand/AS.../AS5X47Spi.cpp, does not seem to use SPI settings, that might create problems with the Teensy 4.0

  10. #10
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Mlu,

    That sounds like what might be causing my issues, as I'm using that library! Do you know what settings were changed and how I should compensate?

  11. #11
    Senior Member
    Join Date
    Jul 2020
    Posts
    544
    The T4 default SPI speed is way faster than most Arduinos. The AS5047 has a max SPI rate of 10MHz...

  12. #12
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Do you know a quick and dandy way to limit that?

  13. #13
    Senior Member
    Join Date
    Jul 2020
    Posts
    544
    SPI library docs?

  14. #14
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    if you're referring to this:

    Code:
     SPISettings(14000000, MSBFIRST, SPI_MODE0)
    I've varied that value from 1/100th its preprogammed value to 100 times its preprogrammed value and got no response, unfortunately.

    But I really do think the problem might have something to do with that.

  15. #15
    Senior Member
    Join Date
    Jul 2020
    Posts
    544
    You need to check the mode is correct - that means reading the timing diagrams in the datasheet
    usually as they don't like to make it easy for some reason. SPI has 4 modes and they are all different,
    although sometimes the wrong one appears to work (unreliably perhaps)

    I'd go for 4MHz initially till its working, don't expect whacky clock rates to work, choose plausible ones.

  16. #16
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Ah, I see. According to the datasheet, it wants SPI_MODE1. 4MHz did not work.

    Thank you for taking the time to work through this.

    Alternatively, I've had some success reprogramming the AS5047P to output PWM. However, you must reprogram it through SPI to accept PWM, and apparently it loses its programming values when you unplug it, so I'm back to square one - for the record, I'm able to connect through SPI through the Uno and reprogram it but the register must be volatile. It has a non-volatile memory but I just am too dumb to make it work lol

  17. #17
    Junior Member
    Join Date
    Nov 2020
    Posts
    9
    Yup as an update to the second thing, I tried following the OTP reprogramming instructions but the settings still aren't being burned it seems.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •