AS5048B Hall Effect Sensors for Teensy 3.6 issues getting data.

Hello,
I'm in a bit of confusion about how to approach this implementation. What I am trying to do is use AS5048B Hall effect encoders to sense angle data and send it to my Teensy 3.6 Microcontroller programming with Teensyduino. The communication used is I2C for the AS5048B as opposed to the 5048A which uses SPI communication.

I have found a library GitHub - sosandroid/AMS_AS5048B: Arduino Lib for AMS AS5048B I2C - 14-bit magnetic rotary position sensor that has been very promising. I have decided to start with example code for detecting angles which is relevant to my project. The example code is below.

Here is the pinout for the Teensy 3.6. https://www.pjrc.com/teensy/card9a_rev1.pdf
I plan on using the following for pins. These pins correspond to the 7 pins on the encoder.

SCL Pin 7
SDA Pin 8
MOSI Pin 11
MISO Pin 12
GND
Vin (3.3 and 5V)

However, as much as I read into the library and datasheets for both the encoder and Teensy, I cannot get the sensor to read the change in an angle as I move the magnet that should activate the sensor. I have been stuck on this for a few days and would very much appreciate the help. I consider myself quite inexperienced so don't judge me if I'm missing something obvious.

#include <Wire.h> // Reference I2C bus library
#include <OneWire.h>
#include <ams_as5048b.h>

#define ONE_WIRE_BUS 4

//unit consts for AMS AS5048B sensor
#define U_RAW 1
#define U_TRN 2
#define U_DEG 3
#define U_RAD 4
#define U_GRAD 5
#define U_MOA 6
#define U_SOA 7
#define U_MILNATO 8
#define U_MILSE 9
#define U_MILRU 10

// Position sensor variables
AMS_AS5048B mysensor;
double EncoderAngle =0;
double deltaAngle = 0;
long steps = 0;

void setup() {
//Start serial
Serial.begin(11500);
while (!Serial) ; //wait until Serial ready
//init AMS_AS5048B object
mysensor.begin();
//consider the current position as zero
mysensor.setZeroReg();
}

void loop() {
getAngle();

Serial.print("Angle degree : ");
Serial.println(mysensor.angleR(U_DEG, false), DEC);
Serial.print("EncoderAngle: ");
Serial.println(EncoderAngle);
Serial.print("DeltaAngle: ");
Serial.println(mysensor.angleR(U_DEG, false)-EncoderAngle, DEC);
Serial.print("DeltaAngle: ");
Serial.println(mysensor.angleR(U_DEG, false)-EncoderAngle);
delay(2000);
}

void getAngle() {
// read sensor to get the encoder angle
EncoderAngle = mysensor.angleR(U_DEG, false);
delay(2000);
}

teensy.pdf (221 KB)

ams.jpg

The T3.6 I2C0 defaults to pins 18 and 19, not 7 and 8. Note the pin swap with SCL on the higher number pin. I’m always confused by the use of the bold and grey fonts used on the pinout card and just read the libraries to be sure. Also note you must use external 2k2 pull-ups to 3v3 on those two pins.

You’ve also got a dogs breakfast of sillyness in your post and program. Why all the unnecessary edits to the example program?

MOSI
MISO not used

OneWire.h
#define ONE_WIRE_BUS 4

Don’t recreate the wheel, just use the read_simple_angle example program as-is.

PS: when posting code, use code tags (the </> icon) so the fourm software doesn’t interpret the code as html. It also saves visual space in the post.

Thank you for the response. I do apologize for the clutter I had in the original post. I have trimmed the unnecessary parts of the example code. It makes sense for me that I don't have to MISO or MOSI in this configuration if that's what you were saying. However, I am stumped on how to let the code know that the pins for SDA and SCL are 18 and 19, respectively because that is my best guess to why I am still not sensing an angle change. The library and example code were designed for an Arduino board while I am using a Teensy 3.6, so there might be a slight difference causing the error. My big question is where should I look in the library or in my code to make it so the communication happens. Thanks again for all your help!

However, I am stumped on how to let the code know that the pins for SDA and SCL are 18 and 19, respectively because that is my best guess to why I am still not sensing an angle change.

You don't do anything to make pins 18/19 function, those are the default pins for the Teensy 3.2/3.5/3.6 and it is all handled behind the scenes by the Teensy specific Wire library.

When you install Teensydinuo, Teensy specific libraries are installed into the IDE environment which are used when the Teensy board type is specified in the tools->board selection in the IDE. Those Teensy libraries are typically in the arduino install directory under contents/java/hardware/teensy/avr/libraries and then in the wire subdirectory. You'll find the files of interest being WireKinetis.cpp and WireKinetis.h. The actual pin numbers are buried in an array named TwoWire::i2c0_hardware. You really don't need to go down the rabbit hole with the libraries, I assure you the pins are SDA on 18 and SCL on 19.

I would troubleshoot by connecting your hardware and then open file->examples. Scroll down carefully and read the top at each section break, you'll find one entitled "Examples for Teensy 3.1/3.2" and scroll down until you find "Wire". Slide your cursor to the right and select "Scanner". Open it, compile it and download to your Teensy 3.6.

Open the serial monitor at 9600 baud and after a brief pause, you should see:

Scanning...
Device found at address 0x40 (PCA9685,Si7021)
done

If the pads A0 and A1 on the AS5048B are not at logic zero, the address discovered by the scanner will be 0x40 plus the values of the logic levels on A0 and A1.

If the AS5048B is not found, you've got a wiring error or you need pull-up resistors on SDA and SCL. If you're wiring to a bare AS5048B chip, you absolutely need pull-ups, if it is on a breakout board, it may have pull-ups, I have no way of knowing.

Once the scanner can see the part, then you can run your demo. There is no point in doing so until the part can be seen on the bus.

So the scanner example is not receiving any I2C devices, so that means there is something wrong with my wiring. I have the Vin(3.6 to 6.0V) pin to + rail to 5V on the encoder, GND (right below pin 13) to - rail to GND on the encoder, and my two SCL and SDA pins to respective encoder wires. As for the pull-up resistors, I just used a pinMode(PIN_A4, INPUT_PULLUP) to set pullups the scanner function because Teensy allows you to do that for all of its pins. Is this sufficient or do I need physical resistors? I also assume my issue has something to do with my Vcc and will probably need to use 3.3V pin on the encoder too and attach that somehow to supply voltage but I'm not 100% how. I must be missing something trivial but I would and do appreciate all the help. Thanks again.

Have you read section 4.1 on page 9 of the datasheet? It must be wired up correctly for 5V or 3.3V operation and they aren't the same.

Pete

Teensy 3.x internal pull-ups are approximately 30 to 50k. 4k7 external resistors are the recommended value for I2C pull-ups, 2k2 is better for long leads and better noise immunity.

Your original post said you’re using using an AS5048B. Now you’re calling it an encoder. I’ve danced around what the part might actually be, a bare chip or a breakout board and you’ve never explained what it is. So, it’s time to stop the guessing. Please provide a link to the part if it is anything other than a bare chip, which as El_Supremo mentions, must be wired per the 5V operation left side of figure 11 of the datasheet. Also note you need a 10uf bypass cap on the VDD3V pin to stabilize the regulator. This is not optional.

So I got my sensor to read i2c device at address 0x40, but am having trouble getting actual angles by the read_simple_angle function. It says the angle is zero and turning the magnet in front of the black chip does not change that. I have pullup resistors at 2k, bypass capacitors hooked up in 5V operation, have AS5048's A1, A2 pins grounded to reduce any possible noise, so I am stumped why I'm still not sensing any change.

Also an update to what else I'm doing, I am also using the example code program_address, to set new addresses to my multiple sensors in order for the teensy to recognize which sensor is activating because that is important in my project.

Once again, any input or tips would be appreciated.