Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By Cleiton Alves
#93330 Hi guys.

I need to read an encoder using the interrupt function by the wemos d1 mini board. However, plotting the readings results in an irregular waveform.

So I tested doing the reading without the interrupt function, just with digitalRead, and when plotting, the waveform was as expected.

In my project I can't just use digitalRead because I can lose readings while executing other functions. Any solution to this problem?

I attached the waveforms that I obtained in each of the tests, through the Serial Plotter. Also, I attached the circuit.

https://drive.google.com/file/d/1WTYii8 ... sp=sharing.

Frequency of the waveform is 40hz.

Below is a simplified code that I used to do just these reading tests:

Code: Select all#define motor 4
#define rotary_encoder 14

boolean signal_encoder = 0;

ICACHE_RAM_ATTR void waveForm() {
  signal_encoder = !signal_encoder;
}

void setup() {
  Serial.begin(115200);
  pinMode(rotary_encoder, INPUT_PULLUP);
  pinMode(motor, OUTPUT);
 // attachInterrupt(digitalPinToInterrupt(rotary_encoder), waveForm, CHANGE);
}

void loop() {
  digitalWrite(motor, 1);

  if(digitalRead(rotary_encoder))signal_encoder=1;
  else signal_encoder = 0;
 
  Serial.println(signal_encoder);
  delay(1);
}
User avatar
By pratheek
#93332 Your signal_encoder variable should be declaread as volatile as you are you using it in an interrupt.

Checkout -
https://www.arduino.cc/reference/en/lan ... /volatile/

If you modify a variable inside an interrupt, then you should declare this variable as volatile.

Also in the digitalPinToInterrupt function, the mode is CHANGE. Check if this is what is you want.