├── README.md └── absoluteEncoderSSI.ino /README.md: -------------------------------------------------------------------------------- 1 | # Absolute Encoder SSI 2 | 3 | This repository provides an Arduino code for reading data from an absolute encoder with Serial data output SSI protocol. The code has been modified to be compatible with some absolute encoders that have a Chip Select (CS) pin. 4 | 5 | ## Inspiration 6 | The code is inspired by a forum post on the Arduino Forum, which can be found [here](https://forum.arduino.cc/t/absolute-rotary-encoder-ssi-spi-how/153103/15). 7 | 8 | ## Supported Encoder and Arduino Board 9 | The code has been tested with the following components: 10 | - Encoder: CALT HAE28 (HAE28U5V12A0.22) 12bit 5V Absolute Rotary Encoder 11 | - Arduino Board: Arduino Uno R3 12 | 13 | Please note that this code might also work with other SSI Absolute Encoders that use the same principle. 14 | 15 | ## Wiring Instructions 16 | To connect the absolute encoder to your Arduino board, follow these wiring instructions: 17 | 18 | ![image](https://github.com/atarbabgei/absoluteEncoderSSI/assets/7202497/8203545b-0f10-45ac-9f74-a0d0de23568b) 19 | 20 | 21 | | CALT HAE28 Wire Color | Encoder Pin | Arduino Pin | 22 | |--------------------|-------------|-------------| 23 | | Red | Power | +5V | 24 | | Black | GND | GND | 25 | | Blue | Clock | Pin 5 | 26 | | Green | Data | Pin 6 | 27 | | Yellow | Chip Select | Pin 7 | 28 | 29 | Make sure to check the specific pinout and connections of your encoder if it differs from the example above. 30 | 31 | ## Usage 32 | 1. Connect the absolute encoder to your Arduino board following the wiring instructions provided. 33 | 2. Upload the code to your Arduino board using the Arduino IDE or any other compatible software. 34 | 3. Open the serial monitor in the Arduino IDE or any other serial monitor software. 35 | 4. Run the code and observe the data read from the absolute encoder on the serial monitor. 36 | 37 | Feel free to modify the code to suit your specific requirements or integrate it into your own projects. 38 | 39 | If you have any questions or encounter any issues, please don't hesitate to open an issue on the GitHub repository. 40 | -------------------------------------------------------------------------------- /absoluteEncoderSSI.ino: -------------------------------------------------------------------------------- 1 | /* Tested for CALT HAE28 Absolute Rotary Encoder 2 | * 3 | * Encoder Red (Power +) <-> Arduino +5V 4 | * Encoder Black (GND) <-> Arduino GND 5 | * Encoder Blue (Clock) <-> Arduino Pin 5 6 | * Encoder Green (DATA) <-> Arduino Pin 6 7 | * Encoder Yellow (CS) <-> Arduino Pin 7 8 | */ 9 | 10 | const int CLOCK_PIN = 5; // Blue Pin 11 | const int DATA_PIN = 6; // Green Pin 12 | const int CS_PIN = 7; // Yellow Pin 13 | const int BIT_COUNT = 12; // 12 Bit Mode 14 | 15 | void setup() 16 | { 17 | //setup our pins 18 | pinMode(DATA_PIN, INPUT); 19 | pinMode(CLOCK_PIN, OUTPUT); 20 | pinMode(CS_PIN, OUTPUT); 21 | //give some default values 22 | digitalWrite(CLOCK_PIN, HIGH); 23 | digitalWrite(CS_PIN, HIGH); 24 | Serial.begin(115200); 25 | } 26 | 27 | void loop() 28 | { 29 | float reading = readPosition(); 30 | if (reading >= -0.5) 31 | { 32 | Serial.print("Reading: "); 33 | Serial.println(reading, 2); 34 | } 35 | delay(50); 36 | } 37 | 38 | //read the current angular position 39 | float readPosition() 40 | { 41 | // Read the same position data twice to check for errors 42 | unsigned long sample1 = shiftIn(DATA_PIN, CLOCK_PIN, CS_PIN, BIT_COUNT); 43 | unsigned long sample2 = shiftIn(DATA_PIN, CLOCK_PIN, CS_PIN, BIT_COUNT); 44 | delayMicroseconds(20); // Clock must be high for 20 microseconds before a new sample can be taken 45 | if (sample1 != sample2) return -1.0; 46 | return ((sample1 & 0x0FFF) * 360UL) / 4096.0; 47 | } 48 | 49 | //read in a byte of data from the digital input of the board. 50 | unsigned long shiftIn(const int data_pin, const int clock_pin, const int cs_pin, const int bit_count) 51 | { 52 | unsigned long data = 0; 53 | 54 | digitalWrite(cs_pin, LOW); 55 | for (int i = 0; i < bit_count; i++) 56 | { 57 | data <<= 1; 58 | digitalWrite(clock_pin, LOW); 59 | delayMicroseconds(1); 60 | digitalWrite(clock_pin, HIGH); 61 | delayMicroseconds(1); 62 | 63 | data |= digitalRead(data_pin); 64 | } 65 | digitalWrite(cs_pin, HIGH); 66 | return data; 67 | } 68 | --------------------------------------------------------------------------------