Arduino control by electronic crank - Page 1
Forum section: electronics / general

author: Plopiche
author_rank: Newbie
timestamp: May 16, 2024, 04:19:56 pm
content: Hi,I would like to order a stepper motor with an electronic crank.Signals A and B from the crank will be sent to 2 digital inputs of an Arduino Uno.When I connect outputs A and B of the crank (with or without bounce management, to 2 interrupt inputs or not, it does not work correctly.Is there a need for an interface circuit between A and B and the inputs of the Arduino Uno?And if so, which circuit?Thanks for your help.

author: ledtester
author_rank: Super Contributor
timestamp: May 16, 2024, 04:57:17 pm
content: It is possible that the A and B signals are "open collector". Try putting a 5K or 10K resistor from each signal line to Vcc (+5 in the case of the Arduino Uno).

author: Plopiche
author_rank: Newbie
timestamp: May 16, 2024, 06:43:42 pm
content: La manivelle :https://fr.aliexpress.com/item/33031046760.html?spm=a2g0o.order_list.order_list_main.22.1c275e5bQGccTL&gatewayAdapt=glo2fra

author: ledtester
author_rank: Super Contributor
timestamp: May 17, 2024, 12:48:28 am
content: Did adding the pullup resistors work?Can you monitor one of the signal lines with a scope? Otherwise I'd use a simple program like the following to see if the input is changing as you expect:Code:const int signalA = 6; // whatever you connect signal A tovoid setup() {Serial.begin(9600);Serial.println(("Starting..."));pinMode(signalA, INPUT);}static bool oldA = false;void loop() {bool newA = digitalRead(signalA);if (oldA != newA) {Serial.println(newA ? "1" : "-");}oldA = newA;}

author: pqass
author_rank: Super Contributor
timestamp: May 17, 2024, 03:15:53 am
content: I initially didn't understand your "electronic crank" reference. But given the Aliexpress link, I'm sure you mean rotary or quadrature encoder.I tried searching for a datasheet but only managed to findthisreference to "Output NPN" which likely means open collector and therefore requires a 10K (or so) pull-up resistor to Vcc. Given that the encoder also has a Vcc input (probably because it uses optical slot switches), you should confirm that the pull-ups are present. That is, use a multimeter on A then B outputs to see if they can produce both Vcc and 0V as you turn the wheel slowly.There are many examples of interfacing encoders with Arduino likethisone.If you want to use interrupts (vs polling), then it's only necessary to connect one of the encoder outputs to an Arduino interrupt pin. The interrupt routine will read the state of both encoder outputs to determine direction. To debounce, from each encoder output put a 10K series resistor then 10nF cap to ground before the Arduino input pin (see attached).

author: Plopiche
author_rank: Newbie
timestamp: May 17, 2024, 09:41:12 am
content: Bonjour,Thanks for your help.Someone also suggested me to use the "Encoder.h" library and I'm going to try:https://www.pjrc.com/teensy/td_libs_Encoder.html

author: Plopiche
author_rank: Newbie
timestamp: May 18, 2024, 04:11:28 pm
content: Voici mon dernier code :```cpp#include <Encoder.h>// Création de l'objet encodeurEncoder monEncodeur(2, 3); // Pins A et B connectés respectivement aux pins 2 et 3long positionActuelle = 0; // Position actuelle de l'encodeurlong dernierePositionStable = 0; // Dernière position stable de l'encodeurunsigned long dernierTempsDebounce = 0; // Dernier temps où un changement a été enregistréconst unsigned long debounceDelay = 50; // Délai de l'anti-rebond en millisecondesvoid setup() {Serial.begin(115200); // Initialiser la communication série à 115200 bauds}void loop() {long nouvellePosition = monEncodeur.read(); // Lire la nouvelle position de l'encodeurif (nouvellePosition != dernierePositionStable) {dernierTempsDebounce = millis();dernierePositionStable = nouvellePosition;}// Vérifier si le délai de debounce est écoulé avant de considérer la position comme stableif ((millis() - dernierTempsDebounce) > debounceDelay && positionActuelle != dernierePositionStable) {positionActuelle = dernierePositionStable; // Mettre à jour la position actuelle si stableSerial.print("Position stable: ");Serial.println(positionActuelle);}delay(100); // Délai pour réduire la charge du processeur}It almost works but I generally advance 4 steps for one notch of the crank. Unfortunately it happens that the number of steps is sometimes different from 4I put 3.3 Kohm pullup resistors on interrupt inputs 2 and 3What can be done to improve?Thanks for your help

author: pqass
author_rank: Super Contributor
timestamp: May 19, 2024, 11:18:39 am
content: QuoteYou could add a small 10nF capacitor from each input pin to ground.Add this define before the Encoder.h include:#define ENCODER_USE_INTERRUPTS