Will this rotary encoder work with Arduino?

Do these go in the UNO IO pins? Because I did not see pins defined for those wires in the code. I'm really a beginner at this, so it is difficult for me to understand some of these concepts.

This is how I understand the picture:

There is a 3 wire SPI option,

  • CS (or SS), Chip Select of Slave Select, to enable the SPI communication
  • SCLK, Serial Clock, to time the pulses of the communication
  • MOSI, one pin that is used in a bidirectional way. This is an option I have seen not often and it reduces the nr of pins / wires. Drawback of three wires is that the communication is half duplex (either master is sending or the slave is sending). However as the device only produces data and does not receive data (e.g. config) this problem does not exist.

IMHO the connection of MOSI with the transistor / GND / MISO line is not needed as there is no data to send to the rotary encoder.

What UNO do you have?

I have the UNO R3.

In the article below - First row, second column describes the pins used for SPI.

https://docs.arduino.cc/language-reference/en/functions/communication/SPI/

You can skip MOSI for the reason @robtillaart gave.

Maybe this could be included in the description of your lib

For 3 wire SPI
1-Supply (Red) 2-Ground (Grey) 3-MOSI/ MISO (Grey) 4-Clock (Grey) 5-Chip select (Grey)
For 4 wire SPI
1-Supply (Red) 2-Ground (Grey) 3-Clock (Grey) 4-MOSI (Grey) 5-MISO (Grey) 6-Chip select (Grey)

@ledsyn

Will add those lines.

So this code would work for my application? Why is only one Arduino pin being used?


//
//    FILE: ERCFS_HW_SPI.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: demo dump millis + position.
//     URL: https://github.com/RobTillaart/ERCFS
//

#include "ERCFS.h"

ERCFS re(9);  //  hardware SPI

void setup()
{
  while(!Serial);
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("ERCFS_LIB_VERSION: ");
  Serial.println(ERCFS_LIB_VERSION);
  Serial.println();

  SPI.begin();

  bool b = re.begin();
  Serial.print("BEGIN:\t");
  Serial.println(b);
}


void loop()
{
  uint16_t position = re.getRawValue();
  Serial.print(millis());
  Serial.print("\t");
  Serial.println(position);
  delay(50);
}



//  -- END OF FILE --

Because the library uses as default SPI (pin 11,12,13 for the UNO).

You could also write
ERCFS re(9, &SPI); // explicit hardware SPI

Some boards have multiple SPI ports, they could write e.g.
ERCFS re(CS_PIN, &SPI1); or ERCFS re(CS_PIN, &SPI3);


in the library .h file

ERCFS(uint8_t select, __SPI_CLASS__ * mySPI = &SPI);
                                            ^^^^^^

this is a default parameter, so you do not need to mention it in the sketch

1 Like

made a table of the wires, how does that look?

Name pin Colour 3 wire SPI 4 wire SPI
Supply +V Red 1 1
Ground Grey 2 2
MOSI Grey NC 3
MISO Grey 3 4
CLOCK Grey 4 5
SELECT CS Grey 5 6
1 Like

Should MOSI be connected, in 3 wire?

mmm, indeed my table is in conflict with the picture from the datasheet.
It is not a direct connection, so better remove it from the table.

Thanks


update: table updated


update: updated table in develop branch

1 Like

I tried your example code in the library, and it outputs jibberish.

I have attached the wires directly to the Arduino as follows:
1 supply, red to 5V power supply on Arduino
2 GND to GND port on Arduino
3 O/P I did not connect since uncessary?
4 Clock to pin13
5 Chip select to pin 10

This is the code that I ran:

//
//    FILE: ERCFS_HW_SPI.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: demo dump millis + position.
//     URL: https://github.com/RobTillaart/ERCFS
//

#include "ERCFS.h"

ERCFS re(9);  //  hardware SPI

void setup()
{
  while(!Serial);
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("ERCFS_LIB_VERSION: ");
  Serial.println(ERCFS_LIB_VERSION);
  Serial.println();

  SPI.begin();

  bool b = re.begin();
  Serial.print("BEGIN:\t");
  Serial.println(b);
}


void loop()
{
  uint16_t position = re.getRawValue();
  Serial.print(millis());
  Serial.print("\t");
  Serial.println(position);
  delay(50);
}

Ok looks indeed as garbage, however.

You connected chip select to pin 10 of the Arduino.
The code of the constructor in the program must then use pin 10 too.

So please change the 9 to 10 in the code and rerun the sketch and post the output.

You must connect MISO to the sensors 3

This is the output after changing to re(10) at the beginning of the code.

Can you post as text instead of a screenshot?

�!9!}��iV��Ҹ�� ��P�����R��Ka�KiX p ��Y a�[ �@Q� ��@�aP)�P�R�)�#J(�`)

� aKi� �A p(�QJS

 I�I � Ja�!XpH���KP�� ��QA�pPPa#q h�� (I�BI�RЃ �qЉPp�Q��H�pIYa��[Rp a�X p�JĴ��B����Å�����@����Ņ ������g������Y�����IY������Ʋ�������������Q��EP������Ł���������� ���Q���Ű�����BS���ǰ��

���Y�ä�� � ��������YAPA���P��������������"#���"c����������������A�ɴ�@��ǵ�Q�X �ư�����������

�� �� ���AIҠ��������Q���0Q�� ��J������� KYJ� X�@H������a������������� ����������������������!����������������" ����������#Y���� I����R��R������������������������������"a�����#

Should I connect wire 3 O/P to something?

So it starts with a constant output 16383 (== all bits HIGH)
and after some time it turns into garbage?