Arduino Nano ESP32 stops communicating

Hello Everyone,

3 Years ago I used the code shown below on an Arduino Nano Every Board which is linked to a LM393 speed sensor to obtain the speed of a motor on a model train. The code works fine.

// Speed Sensor 1 V0.1

#include <EveryTimer.h>

float dia = 0.021;                                              // Diameter of IR LED path around center 21mm
volatile byte rotation; 
float timetaken, rpm, dtime; 
int speed;
unsigned long pevtime;

void setup() {
  Serial.begin(9600);                                           // Sets the data rate to 9600 bits per second (baud) for serial data transmission
  attachInterrupt(digitalPinToInterrupt (3), ISR1, RISING);      // Active on a rise
}

void loop() {
  if(millis()-dtime>500){                                       // Train speed is zero when unit is stationary
![ESP32 to Speed Sensor|690x411](upload://aIxjanKXlcr2xLnH11IRVEInR7D.jpeg)

    dtime=millis(); 
    rpm = speed = 0;
  }
  Serial.print("Bogie Speed is ");    
  Serial.println(speed);                                        // Print the axle speed
  delay(1000);
} 

void ISR1(){
  rotation++; 
  dtime=millis();

  if(rotation>=24) {                                            // 12 slots in encoder disc. Number of interruptions is 2*12 = 24 
    timetaken = millis()-pevtime;                               // time taken to complete one revolution
    rpm=(1000/timetaken)*60;                                    // angular velocity of the axle
    pevtime = millis();                                         // Time when the decoder wheel has started to move
    rotation = 0;
    speed = round(rpm*dia*0.18849);                          // axle speed
  }
}

Using the same code this year on an Arduino Nano ESP32 makes the board stop! I have added a delay of 1 second which helped; however as soon as I use the Speed Sensor to take a reading; the communication drops!

I have spent the past 3 days working on this and I am not sure what I am doing wrong. Any ideas?

Thank you in advance for your time and support.

I think you accidentally "pasted" an image over some of your code.

![ESP32 to Speed Sensor|690x411](upload://aIxjanKXlcr2xLnH11IRVEInR7D.jpeg)

Please, re-post the sketch. And the image.

Hello @xfpd,

Thank you for spotting that. Please find the code below:

// Speed Sensor 1 V0.1

#include <EveryTimer.h>

float dia = 0.021;                                              // Diameter of IR LED path around center 21mm
volatile byte rotation; 
float timetaken, rpm, dtime; 
int speed;
unsigned long pevtime;

void setup() {
  Serial.begin(9600);                                           // Sets the data rate to 9600 bits per second (baud) for serial data transmission
  attachInterrupt(digitalPinToInterrupt (3), ISR1, RISING);      // Active on a rise
}

void loop() {
  if(millis()-dtime>500){                                       // Train speed is zero when unit is stationary
    dtime=millis(); 
    rpm = speed = 0;
  }
  Serial.print("Bogie Speed is ");    
  Serial.println(speed);                                        // Print the axle speed
  delay(1000);
} 

void ISR1(){
  rotation++; 
  dtime=millis();

  if(rotation>=24) {                                            // 12 slots in encoder disc. Number of interruptions is 2*12 = 24 
    timetaken = millis()-pevtime;                               // time taken to complete one revolution
    rpm=(1000/timetaken)*60;                                    // angular velocity of the axle
    pevtime = millis();                                         // Time when the decoder wheel has started to move
    rotation = 0;
    speed = round(rpm*dia*0.18849*48);                          // axle speed which is x48 to scale it from o-gauge
  }
}

And the wiring diagram can be seen below:

Thank you for your time and support.

Verify your interrupt pin jumper is in Pin 3. And "clean and reseat" that jumper at both ends (remove any possible oxidation caused by galvanic corrosion).

This is written strangely...

Every half-second a one-second delay occurs, pushing the next "half-second delay out to 1500+ms.

Are you looking to zeroize rpm and speed every half second, or about 1.5 seconds?

Hi @xfpd the parts used are all brand new. The code runs on Arduino Nano Every however for reasons beyond me it doesn't work on Arduino Nano ESP32! Very odd.

You are right; there is a typo on my end; it should be 1000 not 500. I think i need to work on the maths. I want to calculate the rpm. Any suggestions on how to write that best?

Thank you for your time and support.

I assume the optocoupler pin is nominally "LOW" and is producing "HIGH" when triggered. I think you should have a pinMode() line to set your OptoCoupler pin to its function (INPUT).

pinMode(optocoupler, INPUT); // assuming optocoupler ties this line LOW

Here is my experimental sketch for you to try...

int opto = 3;             // opto-interrupter
int rpm = 0;              // rev per min
float speed, dia = 0.021; // speed and diameter in meters
unsigned long dtime;      // previous "start time"
volatile int rotation;    // counter

void setup() {
  Serial.begin(115200);
  // pinMode(opto, INPUT_PULLUP); // xfpd using button tied high
  pinMode(opto, INPUT);
  // attachInterrupt(digitalPinToInterrupt (opto), ISR1, FALLING); // xfpd using button tied high
  attachInterrupt(digitalPinToInterrupt (opto), ISR1, RISING);
}

void loop() {
  if (millis() - dtime >= 1000) { // timeout
    dtime = millis();             // reset timer

    // calculate outside the ISR because calculating is not fast
    rpm = rotation * 60;          // rotations per second x 60 seconds per minute
    speed = (PI * dia) * (rpm);   // "linear circumferences" per "time measured"

    // print outside the ISR because printing is very slow
    Serial.print("Bogie Speed is ");
    Serial.print(speed);
    Serial.print(" m/s at ");
    Serial.print(rpm);
    Serial.print(" RPM over");
    Serial.print(rotation);
    Serial.print(" rotation");
     if (rotation != 1) Serial.print("s");
    Serial.println(".");

    rpm = speed = rotation = 0; // reset counters
  }
}

void ISR1() {
  rotation++; // count a rotation only, then calculate outside the ISR
}

I was using a button to try to "rotate" one time per second (where you see 60 RPM), but I did not debounce my button (which I assume the optocoupler does), so you see in my output below, 120 RPM and more when the bouncing/ringing occurred.

Bogie Speed is 0.00 m/s at 0 RPM over 0 rotations.
Bogie Speed is 0.00 m/s at 0 RPM over 0 rotations.
Bogie Speed is 3.96 m/s at 60 RPM over 1 rotation.
Bogie Speed is 3.96 m/s at 60 RPM over 1 rotation.
Bogie Speed is 3.96 m/s at 60 RPM over 1 rotation.
Bogie Speed is 19.79 m/s at 300 RPM over 5 rotations.
Bogie Speed is 79.17 m/s at 1200 RPM over 20 rotations.

NanoESP32 uses 3.3v (NanoEvery uses 5v). Maybe the issue is making the optocoupler function. Perhaps a logic level shifter between the NanoESP32 and the optocoupler, and powering the optocoupler externally with 5vdc?

Good morning @xfpd, thank you for going all out and writing the code and the comments which help me understand the dos and don'ts of coding.

I have simply copied and pasted your code into the Arduino Nano ESP32 and I have no idea why...it just worked!!! The ESP32 is stable and the LM393 Speed Sensor is producing good data. A million thanks for your time and support. Really apricate it.