Hello everyone,
I have a super small non-contact rotary 12 bit absolute encoder (SSI, single ended, 5 V) and an Arduino 101. My goal is to read the angular position in the serial monitor if I turn my encoder. The encoder has 4 connections, one for power, one for ground, one for the clock (which i have connected to digital pin 13) and the other for the data (which I have connected to pin 12). I've written multiple iterations of arduino sketches but I have been unsuccessful in all of them - all of the bits have been high at every timestep even when the magnet on the motor is spinning. I have attached my most recent code as reference. I have also attached the datasheet (the encoder I am using is the RM08SD located on page 6). I am new to arduino and encoders so I am not sure if the problem is the wiring, the sensor, or if something is wrong in my code. I am hoping someone is able to help. Thank you!
const int CLOCK_PIN = 13;
const int DATA_PIN = 12;
const int BIT_COUNT = 13;
void setup() {
//setup our pins
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
//give some default values
digitalWrite(CLOCK_PIN, HIGH);
Serial.begin(19200);
}
void loop() {
float reading = readPosition();
if (reading >= -0.5) {
Serial.print("Reading: ");
Serial.println(reading,2);
}
delay(500);
}
//read the current angular position
float readPosition() {
// Read the same position data twice to check for errors
unsigned long sample1 = bitShift(DATA_PIN, CLOCK_PIN, BIT_COUNT);
unsigned long sample2 = bitShift(DATA_PIN, CLOCK_PIN, BIT_COUNT);
delayMicroseconds(25); // Clock must be high for 20 microseconds before a new sample can be taken
if (sample1 != sample2)
return -1.0;
return ((sample1 & 0x0FFF) * 360UL) / 4096.0;
}
//read in a byte of data from the digital input of the board.
unsigned long bitShift(const int data_pin, const int clock_pin, const int bit_count) {
unsigned long data = 0;
for (int i=0; i<bit_count; i++) {
data <<= 1;
digitalWrite(clock_pin,LOW);
delayMicroseconds(1);
digitalWrite(clock_pin,HIGH);
delayMicroseconds(1);
data |= digitalRead(data_pin);
}
return data;
}
ENCODERDATASHEET.pdf (1.41 MB)