# Angle sensor SPI support / SC60228DC

**URL:** https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682
**Category:** hardware support
**Tags:** sc60228
**Created:** [February 7, 2022, 11:21pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682 "2022-02-07T23:21:02Z")
**Posts on this page:** 20
**Page:** 3

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 25, 2022, 7:44pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/41 "2022-02-25T19:44:04Z")

</div>

I’m sure you will make it work! 🙂

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 7:50pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/42 "2022-02-25T19:50:42Z")

</div>

> [@robotsmith](#):
>
> sure you will make it work

You’ve got more faith in me than myself…

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [February 25, 2022, 9:12pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/43 "2022-02-25T21:12:48Z")

</div>

Hmmm… I’m really itching try mine now too, but I shall still have to be patient a few more days…

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 10:05pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/44 "2022-02-25T22:05:44Z")

</div>

> [@robotsmith](#):
>
> clk frequency ok

The clock is fine, and the MISO send data is fine. The MOSI response however is coming at really low 200mV signal. I’m not sure what causes this. I can clearly see the MOSI on the oscilloscope however the MCU obviously cannot interpret signal that low. Therefore, it just reads 0.00.

The other thing I’m observing is that I cannot change the frequency. I’m using STM32F103. I tried different frequencies here:

SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI\_MODE1));

However it doesn’t seem to make any difference.

This is the raw code I’m running:

```auto
#include <SPI.h>

int CSSPin = PA4; // Pin CSS

const static uint16_t sendData = 0xAAAA; //Combing signal. Doesn't matter what we send;
uint16_t returnData;

void setup() {
  Serial.begin(115200);
  pinMode(CSSPin, OUTPUT);
  digitalWrite(CSSPin, HIGH); //Master
  SPI.begin(CSSPin); // Setup SPI to use CSSPin

  PI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE1));

  //SPI.setBitOrder(MSBFIRST); // Most significant bit first
  //SPI.setDataMode(CSSPin,1); // Use SPI Mode 1 (CPOL = 0, CPHA = 1)
  //SPI.setClockDivider(SPI_CLOCK_DIV2); // SPI.setClockDivider(CSSPin, 16); // Set SPI Clock divider
}

void loop() {
  //digitalWrite(CSSPin, LOW);
  //delay(1);  
  returnData = SPI.transfer16(CSSPin, sendData);
  //Serial.println(" Data: ");
  //Serial.println(returnData, HEX);
  //digitalWrite(CSSPin, HIGH);
  //delay(50);
}

```

Clock

 ![image](https://community.simplefoc.com/uploads/default/original/2X/e/e02464f41186023724c1917fa978d6a0aac1ddee.jpeg)

MISO, sending a binary comb signal (0xAAA)

 ![image](https://community.simplefoc.com/uploads/default/original/2X/c/ca244556f7187162e3e8ccc8cbbd67fa34efb587.jpeg)

MOSI

 ![image](https://community.simplefoc.com/uploads/default/original/2X/8/8686c47b46d0bb33c471bbae347572dcdeac6617.jpeg)

Mystery…

If anyone has any ideas please let me know.

---

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 25, 2022, 10:19pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/45 "2022-02-25T22:19:43Z")

</div>

I just rechecked the pullup configuration and mosi is pulldown??

 ![Capture d’écran 2022-02-25 231929](https://community.simplefoc.com/uploads/default/original/2X/8/88efdb6786c706286a2e7822c5c977cbd4fbaa73.png)

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 10:21pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/46 "2022-02-25T22:21:41Z")

</div>

> [@robotsmith](#):
>
> rechecked the pullup configuration and mosi is pulldown

Hmmm… look at that. My documentation says 3v3, this one says 5V? Could it be that simple? 5V supply instead of 3V to get the SPI chip logic running? Let me check.

Where did you get this picture from, please?

Mine came from a Chinese datasheet manual.

---

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 25, 2022, 10:23pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/47 "2022-02-25T22:23:55Z")

</div>

[This is the link of the datasheet](https://semiment.com/wp-content/uploads/2021/04/SC60228_EN-VA1.0.pdf)  
I’m would be surprised if 3.3V doesn’t work.

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 10:35pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/48 "2022-02-25T22:35:29Z")

</div>

> [@robotsmith](#):
>
> surprised if 3.3V doesn’t work

3v3 works fine for the PWM signal however perhaps SPI requires higher voltage to supply the tiny MCU that calculates the angle and runs the SPI logic? This is mystery Chinese stuff, so anything is possible. I’ll probably also have to burn some incense and pray to the ten gods to make it work.

---

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 25, 2022, 10:35pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/49 "2022-02-25T22:35:32Z")

</div>

Just to be sure. When you say MISO about 0xAAA, you meant MOSI right?

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 10:36pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/50 "2022-02-25T22:36:39Z")

</div>

> [@robotsmith](#):
>
> When you say MISO about 0xAAA, you meant MOSI right

Let me check. There so many things that could have gone wrong.

Yes, I mistyped, it’s MOSI.

5V didn’t help, let me check the resistors. The chip PWM definitely works both on 3v and 5v, so that’s good to know. Keep digging.

OK I managed to burn the chip. Time to burn incense and sacrifice a small animal. BRB.

---

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 25, 2022, 10:52pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/51 "2022-02-25T22:52:23Z")

</div>

I’m checking other datasheet of the same series. Pullup on CLK and CS and pulldown on MOSI seems to be usually 10K et read frequency 1Mhz. Strange that you have absolutely no readings.

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 10:53pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/52 "2022-02-25T22:53:23Z")

</div>

> [@robotsmith](#):
>
> Strange that you have absolutely no readings

Let me replace the chip. It will take an hour to re-solder the setup.

 ![image](https://community.simplefoc.com/uploads/default/original/2X/8/8371fa90b335e00101811cc5327c954bb9f929b7.jpeg)

---

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 25, 2022, 10:54pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/53 "2022-02-25T22:54:58Z")

</div>

> [@Valentine](#):
>
> OK I managed to burn the chip. Time to burn incense and sacrifice a small animal. BRB.

Maybe burn some components instead of animals, not sure that the god of chinese components understand otherwise… 😛  
I’ll check your answer tomorrow 🙂

BTW, in case you want to inspect other [datasheet of the series](https://semiment.com/product/magnetic-encoder-ic/), maybe some are more complex. And I don’t believe they have large differences.

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 11:03pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/54 "2022-02-25T23:03:30Z")

</div>

> [@robotsmith](#):
>
> don’t believe they have large differences

Well, I used a datasheet from a different website. I’ve seen things in Chinese documentation that would chill your bones.

Have you made that chip work on your setup?

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 25, 2022, 11:43pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/55 "2022-02-25T23:43:19Z")

</div>

> [@robotsmith](#):
>
> burn some components instead of animals

I believe that was the case, I had to sacrifice two of the chips. The third worked. I’ve no idea what the problem was however now I’m getting raw data with the code above. Let me continue with converting the raw data to angle. Stay tuned. Also, there was no need for pullups and pulldowns.

Edit: The raw sensor readings give values between 0 and 2^15. Looking at the documentation, and the way @runger had done it, we ignore the last bits so it seems correct so far. This is the exact code I used without pulls:

```auto
#include <SPI.h>

int CSSPin = PA4; // Pin CSS

const static uint16_t sendData = 0xAAAA; //Combing signal. Doesn't matter what we send;
uint16_t returnData;

void setup() {
  Serial.begin(115200);
  pinMode(CSSPin, OUTPUT);
  digitalWrite(CSSPin, HIGH); //Master
  SPI.begin(CSSPin); // Setup SPI to use CSSPin

  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE1));

  //SPI.setBitOrder(MSBFIRST); // Most significant bit first
  //SPI.setDataMode(CSSPin,1); // Use SPI Mode 1 (CPOL = 0, CPHA = 1)
  //SPI.setClockDivider(SPI_CLOCK_DIV2); // SPI.setClockDivider(CSSPin, 16); // Set SPI Clock divider
}

void loop() {
  digitalWrite(CSSPin, LOW);
  //delay(1);  
  returnData = SPI.transfer16(CSSPin, sendData);
  Serial.print(" :: ");
  Serial.println(returnData, DEC);
  digitalWrite(CSSPin, HIGH);
  delay(300);
}

```

The code was executed on STM32F103

The dev branch code however didn’t work. This was the code I ran

```auto
// Open loop motor control example
#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include "encoders/sc60228/MagneticSensorSC60228.h"

BLDCMotor motor = BLDCMotor(1);

BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PB13, PA9, PB14, PA10, PB15, PB12);

MagneticSensorSC60228 sensorSC60228(PA4);

void setup() {

  sensorSC60228.init();

  driver.voltage_power_supply = 12;

  driver.pwm_frequency = 15000;

  driver.init();

  motor.linkDriver(&driver);

  motor.voltage_limit = 3; // [V]
  motor.velocity_limit = 10; // [rad/s] cca 50rpm
 
  motor.controller = MotionControlType::velocity_openloop;

  motor.init();

  Serial.begin(115200);
  Serial.println("Motor ready!");
  Serial.println("Set target velocity [rad/s]");
  _delay(1000);
}

void loop() {

  _delay(100);
  Serial.print(" :: ");
  Serial.println(sensorSC60228.getAngle());
  motor.move(2);
}

```

So the good news is that we have a working chip and raw SPI angle pull that pulls all 16 bits. It has to be fairly trivial to make the SimpleFOC angle code work. The dev branch code simply doesn’t read any angle, just gets stuck on one number. Let me try digging into it.

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [February 26, 2022, 1:36am UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/56 "2022-02-26T01:36:23Z")

</div>

That seems like kind of good news! I wonder why the first two didn’t work?

The MOSI line should read 0, always, actually. The MISO line should carry the signal.

For the second code example, you have to add a:

`sensorSC60228.update();`

to the main loop. Otherwise the sensor value isn’t refreshed…

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 26, 2022, 1:40am UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/57 "2022-02-26T01:40:54Z")

</div>

> [@runger](#):
>
> first two didn’t work

I’m not sure of the very first didn’t work because I tried with the dev code. After I confirm the code works I’ll plug back the first one because I didn’t want to touch it, its buried inside a very expensive planetary gear and I’m afraid to break anything.

The second plain stopped working after I ran it on 5V. It worked then it went silent.

`sensorSC60228.update();`

Let me try this now.

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 26, 2022, 1:46am UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/58 "2022-02-26T01:46:28Z")

</div>

> [@runger](#):
>
> second code example, you have to add a:

Yes it works. This is the main loop:

```auto
void loop() {
  _delay(100);
  Serial.print(" :: ");
  Serial.println(sensorSC60228.getAngle());
  sensorSC60228.update();
}

```

All right, so that’s really good news. Let me try plugging in the first sensor. It has a 1 meter cable so that could be another reason why I didn’t get a signal, perhaps the cable, even though I used very high quality TP, may be too much for the sensor to handle. Or the magnet is too far. So many things could go wrong. Stay tuned. It could just be I need to shorten the cable. Now I have something to compare against.

Excellent news, the dev code worked without any changes. Let me post a small example we could add to the codebase. I suck at git so you may copy/paste or write your own example.

Well, time to celebrate.

```auto
// Open loop motor control example
#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include "encoders/sc60228/MagneticSensorSC60228.h"

BLDCMotor motor = BLDCMotor(1);

BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PB13, PA9, PB14, PA10, PB15, PB12);

MagneticSensorSC60228 sensorSC60228(PA4);

void setup() {

  sensorSC60228.init();

  driver.voltage_power_supply = 12;

  driver.pwm_frequency = 15000;

  driver.init();

  motor.linkDriver(&driver);

  motor.voltage_limit = 3; // [V]
  motor.velocity_limit = 10; // [rad/s] cca 50rpm
 
  motor.controller = MotionControlType::velocity_openloop;

  motor.init();

  Serial.begin(115200);
  Serial.println("Motor ready!");
  _delay(1000);
}

void loop() {

  _delay(10);
  Serial.print(">> ");
  Serial.println(sensorSC60228.getAngle());
  sensorSC60228.update();
  motor.move(2);
}

```

Of course the real test would be using it in a closed loop but I’m too burned out. Tomorrow is another day.

@runger, thank you, excellent job. Also, thanks to @robotsmith for the document and suggestions.

Cheers,  
Valentine

---

<div class="post-metadata">

### Author: ![robotsmith](https://community.simplefoc.com/user_avatar/community.simplefoc.com/robotsmith/32/1929_2.png) [@robotsmith](https://community.simplefoc.com/u/robotsmith)
#### Post date: [February 26, 2022, 7:15pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/59 "2022-02-26T19:15:50Z")

</div>

Hi!  
Congrats! Very cool!  
Not sure I was really helping 😃 but I was emotionally here 😉

Did you test it on a real setup with a motor? Do you think it is worth it?

Cheers!

---

<div class="post-metadata">

### Author: ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)
#### Post date: [February 26, 2022, 9:58pm UTC](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682/60 "2022-02-26T21:58:02Z")

</div>

I have some very busy family stuff now but will when I get the time

[Previous page](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682.md?page=2)

[Next page](https://community.simplefoc.com/t/angle-sensor-spi-support-sc60228dc/1682.md?page=4)
