Advertisement
microrobotics

Encoder for JGY-370 Series Motor

Sep 6th, 2022
4,315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * The purpose of this code is to count the ouput pulses or
  3.  * the encoder outputs as you rotate the Motor shaft. You can run the
  4.  * same code on the Arduino Uno, Arduino Nano, Arduino Mega, etc.
  5.  */
  6. #define Encoder_output_A 2 // pin2 of the Arduino
  7. #define Encoder_output_B 3 // pin 3 of the Arduino
  8. // these two pins has the hardware interrupts as well.
  9.  
  10. int Count_pulses = 0;
  11. void setup() {
  12. Serial.begin(9600); // activates the serial communication
  13. pinMode(Encoder_output_A,INPUT); // sets the Encoder_output_A pin as the input
  14. pinMode(Encoder_output_B,INPUT); // sets the Encoder_output_B pin as the input
  15. attachInterrupt(digitalPinToInterrupt(Encoder_output_A),DC_Motor_Encoder,RISING);
  16. }
  17.  
  18. void loop() {
  19.   Serial.println("Result: ");
  20.   Serial.println(Count_pulses);
  21.   delay(100);
  22. }
  23.  
  24. void DC_Motor_Encoder(){
  25.   int b = digitalRead(Encoder_output_B);
  26.   if(b > 0){
  27.     Count_pulses++;
  28.   }
  29.   else{
  30.     Count_pulses--;
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement