Components and supplies
DFS60 incremental encoder
Arduino UNO
RS422 / RS485 Shield
Tools and machines
Multitool, Screwdriver
Apps and platforms
Arduino IDE
Project description
Code
Code for Arduino UNO
arduino
1/* 2 * Application note: incremental encoder and RS422/RS485 Shield 3 * Version 1.0 4 * Copyright (C) 2020 Hartmut Wendt www.zihatec.de 5 * 6 * used encoder: SICK DFS60 www.sick.com 7 * 8 * 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program. If not, see <http://www.gnu.org/licenses/>. 21*/ 22 23 24// Pin definitions 25const byte ZeroPin = 2; // RS422 input for Z channel (zero detection) 26const byte COSPin = 3; // RS422 input for A channel (cos signal) 27const byte SINPin = 4; // RS422 input for B channel (sin signal) 28 29// variables 30enum {ENC_STOP, ENC_CLOCKWISE_ROTATION, ENC_COUNTERCLOCKWISE_ROTATION}; // encoder operation modes 31volatile byte encoder_state = ENC_STOP; 32volatile int encoder_position = 0; 33volatile int encoder_oldpos = 0; 34 35 36 37void setup() { 38 Serial.begin(115200); //Use serial monitor for debugging 39 40 // Define pins for input and output 41 pinMode(SINPin, INPUT); 42 43 // set internal pullup resistor for interrupt pin 44 pinMode(COSPin, INPUT_PULLUP); 45 pinMode(ZeroPin, INPUT_PULLUP); 46 47 48 // set 1st interrupt service routine to COSPin and 'RISING' edge 49 attachInterrupt(digitalPinToInterrupt(COSPin), encoder_isr, RISING); 50 51 // set 2nd interrupt service routine to ZeroPin and 'HIGH' level 52 attachInterrupt(digitalPinToInterrupt(ZeroPin), zero_detection_isr, HIGH); 53} 54 55 56void loop() { 57 // Detect Encoder Stop 58 if (encoder_oldpos == encoder_position) encoder_state= ENC_STOP; 59 60 // output encoder incremental and status 61 Serial.print("Encoder position: "); 62 Serial.print(encoder_position); 63 Serial.print(", Encoder state: "); 64 65 if (encoder_state==ENC_CLOCKWISE_ROTATION) { 66 Serial.println("Clockwise Rotation"); 67 68 } else if (encoder_state==ENC_COUNTERCLOCKWISE_ROTATION) { 69 Serial.println("Counter-Clockwise Rotation"); 70 71 } else { 72 Serial.println("Stop"); 73 } 74 75 encoder_oldpos = encoder_position; 76 77 delay(500); 78} 79 80 81void encoder_isr() { 82 83 if (digitalRead(SINPin) == LOW) { 84 // clockwise rotation 85 encoder_state=ENC_CLOCKWISE_ROTATION; 86 encoder_position++; 87 } else { 88 //counter-clockwise rotation 89 encoder_state=ENC_COUNTERCLOCKWISE_ROTATION; 90 encoder_position--; 91 } 92} 93 94 95void zero_detection_isr() { 96 // detect pulse on zero channel 97 encoder_position = 0; 98}
Code for Arduino UNO
arduino
1/* 2 * Application note: incremental encoder and RS422/RS485 Shield 3 * Version 1.0 4 * Copyright (C) 2020 Hartmut Wendt www.zihatec.de 5 * 6 * used encoder: SICK DFS60 www.sick.com 7 * 8 * 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program. If not, see <http://www.gnu.org/licenses/>. 21*/ 22 23 24// Pin definitions 25const byte ZeroPin = 2; // RS422 input for Z channel (zero detection) 26const byte COSPin = 3; // RS422 input for A channel (cos signal) 27const byte SINPin = 4; // RS422 input for B channel (sin signal) 28 29// variables 30enum {ENC_STOP, ENC_CLOCKWISE_ROTATION, ENC_COUNTERCLOCKWISE_ROTATION}; // encoder operation modes 31volatile byte encoder_state = ENC_STOP; 32volatile int encoder_position = 0; 33volatile int encoder_oldpos = 0; 34 35 36 37void setup() { 38 Serial.begin(115200); //Use serial monitor for debugging 39 40 // Define pins for input and output 41 pinMode(SINPin, INPUT); 42 43 // set internal pullup resistor for interrupt pin 44 pinMode(COSPin, INPUT_PULLUP); 45 pinMode(ZeroPin, INPUT_PULLUP); 46 47 48 // set 1st interrupt service routine to COSPin and 'RISING' edge 49 attachInterrupt(digitalPinToInterrupt(COSPin), encoder_isr, RISING); 50 51 // set 2nd interrupt service routine to ZeroPin and 'HIGH' level 52 attachInterrupt(digitalPinToInterrupt(ZeroPin), zero_detection_isr, HIGH); 53} 54 55 56void loop() { 57 // Detect Encoder Stop 58 if (encoder_oldpos == encoder_position) encoder_state= ENC_STOP; 59 60 // output encoder incremental and status 61 Serial.print("Encoder position: "); 62 Serial.print(encoder_position); 63 Serial.print(", Encoder state: "); 64 65 if (encoder_state==ENC_CLOCKWISE_ROTATION) { 66 Serial.println("Clockwise Rotation"); 67 68 } else if (encoder_state==ENC_COUNTERCLOCKWISE_ROTATION) { 69 Serial.println("Counter-Clockwise Rotation"); 70 71 } else { 72 Serial.println("Stop"); 73 } 74 75 encoder_oldpos = encoder_position; 76 77 delay(500); 78} 79 80 81void encoder_isr() { 82 83 if (digitalRead(SINPin) == LOW) { 84 // clockwise rotation 85 encoder_state=ENC_CLOCKWISE_ROTATION; 86 encoder_position++; 87 } else { 88 //counter-clockwise rotation 89 encoder_state=ENC_COUNTERCLOCKWISE_ROTATION; 90 encoder_position--; 91 } 92} 93 94 95void zero_detection_isr() { 96 // detect pulse on zero channel 97 encoder_position = 0; 98}
Downloadable files
Wiring diagram
Wiring diagram
Comments
Only logged in users can leave comments