Author Topic: Arduino control by electronic crank  (Read 1037 times)

0 Members and 1 Guest are viewing this topic.

Offline PlopicheTopic starter

  • Newbie
  • Posts: 4
  • Country: fr
Arduino control by electronic crank
« on: May 16, 2024, 04:19:56 pm »
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.
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3081
  • Country: us
Re: Arduino control by electronic crank
« Reply #1 on: May 16, 2024, 04:57:17 pm »
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).
 


Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3081
  • Country: us
Re: Arduino control by electronic crank
« Reply #3 on: May 17, 2024, 12:48:28 am »
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: [Select]
const int signalA = 6; // whatever you connect signal A to

void 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;
}
 

Online pqass

  • Frequent Contributor
  • **
  • Posts: 789
  • Country: ca
Re: Arduino control by electronic crank
« Reply #4 on: May 17, 2024, 03:15:53 am »
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 find this reference 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 like this one.

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).
 

Offline PlopicheTopic starter

  • Newbie
  • Posts: 4
  • Country: fr
Re: Arduino control by electronic crank
« Reply #5 on: May 17, 2024, 09:41:12 am »
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


 

Offline PlopicheTopic starter

  • Newbie
  • Posts: 4
  • Country: fr
Re: Arduino control by electronic crank
« Reply #6 on: May 18, 2024, 04:11:28 pm »
Voici mon dernier code :


```cpp
#include <Encoder.h>

// Création de l'objet encodeur
Encoder monEncodeur(2, 3);  // Pins A et B connectés respectivement aux pins 2 et 3

long positionActuelle = 0;               // Position actuelle de l'encodeur
long dernierePositionStable = 0;         // Dernière position stable de l'encodeur
unsigned long dernierTempsDebounce = 0;  // Dernier temps où un changement a été enregistré
const unsigned long debounceDelay = 50;  // Délai de l'anti-rebond en millisecondes

void setup() {
  Serial.begin(115200);  // Initialiser la communication série à 115200 bauds
}

void loop() {
  long nouvellePosition = monEncodeur.read();  // Lire la nouvelle position de l'encodeur
  if (nouvellePosition != dernierePositionStable) {
    dernierTempsDebounce = millis();
    dernierePositionStable = nouvellePosition;
  }

  // Vérifier si le délai de debounce est écoulé avant de considérer la position comme stable
  if ((millis() - dernierTempsDebounce) > debounceDelay && positionActuelle != dernierePositionStable) {
    positionActuelle = dernierePositionStable;  // Mettre à jour la position actuelle si stable
    Serial.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 4
I put 3.3 Kohm pullup resistors on interrupt inputs 2 and 3
What can be done to improve?

Thanks for your help






 

Online pqass

  • Frequent Contributor
  • **
  • Posts: 789
  • Country: ca
Re: Arduino control by electronic crank
« Reply #7 on: May 19, 2024, 11:18:39 am »
Quote
What can be done to improve?

You could add a small 10nF capacitor from each input pin to ground.

Add this define before the Encoder.h include:
      #define ENCODER_USE_INTERRUPTS

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf