Motor encoder

hi guys
can anyone help with a code that can get a readings from h9700 encoder.

i keep getting fixed value no matter how much i change the speed of the motor i always get the same reading need a code that can give me continuous reading.
and is it possible to use digital pin instead of analog pin, “board nodemcu 1.0”.

thanks in advance.

Hello, welcome to the Blynk Forum.

Reading encoders is done in code, nothing specific to Blynk.

Manual Rotary and IR optical encoders will function pretty much the same way and there any many example programs and tutorials on Google… e.g.

http://playground.arduino.cc/Main/RotaryEncoders

http://www.electroschematics.com/10494/arduino-optical-position-rotary-encoder

Once you have it working properly, without Blynk, i.e. outputting to the serial monitor, then exchanging serial output with a display widget should be relatively easy… but you will need to do it with a timer, say every 500ms or so, NOT in the main loop.

EDIT - Also, I think the best method of reading the encoder would be hardware interrupts, as software polling will get harder to do with Blynk running in the background

Get it properly working first, then show your code here for assistance on displaying it with Blynk.

When posting your code, please format it as shown here:

2 Likes

thx mate i did that and it worked but when i connect it to the nodemcu i dont get any feedback from the encoder

as for the the code i used Arduino code and changed it to work for blynk and mcu

#include <SimpleTimer.h> //Timer to update encoder count

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int  pinEncoder=1;                                 //  Pin to receive XORed (double resolution) encoder pulses.
volatile unsigned int counter=0;                  //   Counter to hold counted pulses.

unsigned int motorSpeedRPM=0;                    //    Calculated motor speed from encoder value.
SimpleTimer timer;                              //     Timer object for polling counter value every 1 sec.
const long updateInterval=1000L;               //      Timer update value (1000 ms)
const float pulsesPerRevolution=200;  //       Pulses per revolution (Servo Motor-Proteus)

char auth[] = "0f915189************************";
char ssid[] = "Mohamed";
char pass[] = "rekman1234";

void updateSpeedEvent()
{
  motorSpeedRPM=((counter/(updateInterval/1000.0))*60.0)/pulsesPerRevolution; //RPM speed
  Blynk.virtualWrite(V2, motorSpeedRPM); //send RPM value to gauge
  Serial.println(counter);
  counter=0; //reset counter;
}


void setup()
{

  //Set Encoder pin as input
  pinMode(pinEncoder,INPUT);
   
  // Blynk will work through Serial
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  // Setup a function to be called every 1 sec.
  timer.setInterval(updateInterval, updateSpeedEvent);

  // Interrupt for accurate counting of encoder pulses
  attachInterrupt(digitalPinToInterrupt(5), encoderCounter, RISING);

}

// This function corresponds to changes in slider 
// Which will change motor speed via PWM


BLYNK_WRITE(V0)  // Slider widget - this function gets called every time slider is moved.

{
  
  int slideValue = param.asInt();  // Get the slider value.
   analogWrite(2, slideValue);  // Send value to digital pi
  Blynk.virtualWrite(V4, slideValue*12/1000);  // Send value to digital pin
}


void loop()
{
  // All the magic is here
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

// Interrupt Service Routine
void encoderCounter()
{
    counter+=1; //a pulse received
}
1 Like

Have you tried setting pin to PINMODE(D5, INPUT_PULLUP)? I’m not sure it’s needed, depends on the hardware I guess. There are some guidelines on using interrupt here: https://www.arduino.cc/en/Reference/attachInterrupt but I’m not sure it also goes to the nodemcu since I don’t have that hardware.

2 Likes

:joy:There aren’t enough emojis for your code :stuck_out_tongue_winking_eye: