Using an ERCFS Shaft Encoder for a project with Robert Tillaart Library. Our results from the serial monitor keep staying the same when we spin the shaft and do not know how to troubleshoot this. We used a multimeter to test if the shaft encoder is receiving voltage from the arduino and it is. We think it's a communication error between the shaft encoder and the arduino... please help. @robtillaart
Hello, please share your code and wiring, power and link to your encoder.
#include "ERCFS.h"
#include <SPI.h>
// -------------------------------------------------------------------------------
// Shaft encoder
// -------------------------------------------------------------------------------
ERCFS re(10); // hardware SPI -- digital pin 10
uint32_t lastDisplay = 0;
int16_t position = 0; //stores current encoder position
int16_t previous = 0; //allows arduino to calculate how much the encoder has rotated
float RPM = 0; //stores calculated roational speed
// -------------------------------------------------------------------------------
// Timing
// -------------------------------------------------------------------------------
unsigned long lastUpload = 0; //stores the time of the last upload
const unsigned long uploadInterval = 1000; //uploads every second
void setup() {
Serial.begin(115200);
SPI.begin();
if (!re.begin()) {
Serial.println("Encoder not found!");
while (1)
;
// prints an error if encoder is not found, stops the program forever
}
previous = re.getRawValue();
delay(1000);
}
void loop() {
uint32_t duration = micros() - re.lastRead();
if (duration >= 1000000) //reads every 1 second
{
position = re.getRawValue();
float delta = abs(position - previous);
previous = position;
RPM = (60e6 / 16384) \* delta / duration; //calculates rotational speed in RPM
}
if (millis() - lastDisplay >= uploadInterval) {
lastDisplay = millis();
float angle = position \* 360.0 / 16384.0;
//Print to serial monitor
Serial.print("Position: ");
Serial.print(position);
Serial.print(" Angle: ");
Serial.print(angle, 2);
Serial.print("° RPM");
Serial.println(RPM, 2);
}
}
We have the 3 wire ERCFS shaft encoder, which uses the 5V, so we connected the 5V to the power supply, GND to GND, pin 12 to DAT, pin 13 to CLK, and pin 10 to CS.
Which Arduino are you using? SPI pins differ from model to model.
Same as what?
Are GND joined ?
Did you run the test program that comes with the library? Results?
have your tried any of the example programs provided with the library?
e.g. File>Examples>ERCFS
We're using the Arduino UNO WiFi R2.
Yes, we tried using the example for the RPM, but the angle output would remain at 0 degrees even when rotating the shaft.
Yes, the GND on the shaft encoder and the GND on the arduino are joined.
@myfavoritefruitisapear2
The 3 wire SPI interface is somewhat confusing. I think you need a pull-up resistor on Arduino MISO as shown in the datasheet. Try 10K to 5V.
Is that supported by the current library? It has to be computed, not coming from the encoder!
The MISO and MOSI share a line when 3 wire. Here is the datasheet with the proper connections for SPI.
@myfavoritefruitisapear
The 3 wire SPI interface is somewhat confusing. I think you need a pull-up resistor on Arduino MISO. Try 10K to 5V.
In addition to the resistor, it appears you also need a transistor to convert the MOSI from a driven output to an open collector output.
It's not used in 3 wire. There is no data to send to the encoder.
What's this myfavoritefruitisapear2 and myfavoritefruitisapear stuff?
Are you the same person?
The MOSI line in 3 wire is used. The MOSI and MISO share a line to the encoder. That is why the conversion from a driven line to open collector with a pullup resistor.
Did you see the datasheet I posted?
So what data is sent to the encoder and why?