Arduino Uno Serial Monitor (for MH sensor series)

Hi,

I am having an issue with Serial Monitor not showing the correct readings in serial monitor.

I have 2 MH sensor which is detecting the rpm/revolutions for my DC motor using encoder disks.

When i tested the sensors individually, it shows correct analog and digital reading as picture shown.

const int IN_A0 = A1; // analog input
const int IN_D0 = 5; // digital input
void setup() {
Serial.begin(9600);
pinMode (IN_A0, INPUT);
pinMode (IN_D0, INPUT);

}
int value_A0;
bool value_D0;
void loop() {
value_A0 = analogRead(IN_A0); // reads the analog input from the IR distance sensor
value_D0 = digitalRead(IN_D0);// reads the digital input from the IR distance sensor
Serial.print(" Analogue = "); 
Serial.print(value_A0);
Serial.print("\t Digital ="); 
Serial.println(value_D0);
delay(100);
}

testing of encoder individually.png

However, when i use both sensors simultaneously while i rotate the motor and using Serial Monitor, somehow either my

Right analog value is not changing(remain in 900s) while Right Digital value is changing,

or

Right analog value is changing (from 100s to 900s) while Right Digital value is the same (1)

const int Left_IN_A0 = A0; // analog input
const int Left_IN_D2 = 2; // digital input
const int Right_IN_A1 = A1; // analog input
const int Right_IN_D5 = 5; // digital input
void setup() {
Serial.begin(9600);
pinMode (Left_IN_A0, INPUT);
pinMode (Left_IN_D2, INPUT);
pinMode (Right_IN_A1, INPUT);
pinMode (Right_IN_D5, INPUT);
}
int value_A0;
int value_A1;
bool value_D2;
bool value_D5;
void loop() {
value_A0 = analogRead(Left_IN_A0); // reads the analog input from the IR distance sensor
value_D2 = digitalRead(Left_IN_D2);// reads the digital input from the IR distance sensor
value_A1 = analogRead(Right_IN_A1);
value_D5 = analogRead(Right_IN_D5);
Serial.print(" Left Analogue = "); 
Serial.print(value_A0);
Serial.print("   Left Digital = "); 
Serial.print(value_D2);
Serial.print("   Right Analogue = "); 
Serial.print(value_A1);
Serial.print("   Right Digital ="); 
Serial.print(value_D5);
Serial.println();
delay(100);
}

anyone have encountered similar issues or have any ideas?

test_encoder_x1.ino (540 Bytes)

test_encoder_x2.ino (997 Bytes)

testing of encoder individually.png

Hi I have another issue. I originally planned to use both encoders in Arduino pin 2 and 3 for ISR.

When i soldered the encoder to pin 3, while testing individually, analogue value is changing while digital value is not (not sure if pin issue as pin 5 is working as shown above individual testing.

encoder pin3.png

const int IN_A0 = A1; // analog input
const int IN_D0 = 3; // digital input
void setup() {
  Serial.begin(9600);
  pinMode (IN_A0, INPUT);
  pinMode (IN_D0, INPUT);
 
}
int value_A0;
bool value_D0;
void loop() {
  value_A0 = analogRead(IN_A0); // reads the analog input from the IR distance sensor
  value_D0 = digitalRead(IN_D0);// reads the digital input from the IR distance sensor
  Serial.print(" Analogue = "); 
  Serial.print(value_A0);
  Serial.print("\t Digital ="); 
  Serial.println(value_D0);
  delay(100);
}

encoder pin3.png

Hi anyone can advise? not issue with sensor as i bought another to try.

Hi,

I have a question regarding ISR.

I am currently building a vehicle to avoid obstacles + go back to home (starting point) once a period has passed. lets say 1min.

Currently i am using 2 motor encoders to detect motor revolution to determine where the robot is, so that it can go back to home.

I have heard that i will need to use ISR so that it can calculate the motor rev/distance while letting the robot move.

I am using Arduino Uno and know that interrupt pins are 2 and 3.

However, my pin 3 is having issue for some reasons when i was testing out my encoder as per below post. Am i able to calculate motor rev/distance without using interrupt?

https://forum.arduino.cc/index.php?topic=665941.msg4485050#msg4485050

Am i able to calculate motor rev/distance without using interrupt?

If you can read the encoder fast enough in loop() then you don't need to use an interrupt but even using an interrupt don't expect your calculation of current position and angle to be very accurate due to wheel slip and the slight difference in diameter of the wheels

I have 2 MH sensor which is detecting the rpm/revolutions for my DC motor using encoder disks.

what is an MH sensor? is it an encoder with 2 optical outputs?

i don't understand why you read one input as analog and the other as digital

anyang:
However, my pin 3 is having issue for some reasons when i was testing out my encoder as per below post. Am i able to calculate motor rev/distance without using interrupt?

Arduino Uno Serial Monitor (for MH sensor series) - #2 by anyang - Programming Questions - Arduino Forum

My first question is why have you started a second Thread about the same project? Please click Report to Moderator and ask to have the two Threads merged so we have all the data in one place.

Next, either figure out why Pin 3 is not working, and fix it, or get a new Arduino if it is damaged.

Finally, you can use PinChange interrupts on any I/O pin, but they are a bit less convenient to use.

...R

gcjr:
what is an MH sensor? is it an encoder with 2 optical outputs?

i don't understand why you read one input as analog and the other as digital

Please see pic below. should i just ignore the analog input and just focus on digital? since i only want to see the rising edge to determine if the encoder is turned and then do counter++?
But i am still having issues since digital value is not changing..

yet another 4 pin device

if it's an encoder, aren't both outputs digital?

Topics merged, as requested.

anyang:
Please see pic below. should i just ignore the analog input and just focus on digital?

Please post a link to the datasheet for the device. Without that we are all just guessing, and wasting time.

...R

Robin2:
Please post a link to the datasheet for the device. Without that we are all just guessing, and wasting time.

...R

i finally manage to found the datasheet after so long.

Below is the code im testing out. Just trying to make motor move forward by 30cm, stop and then reverse back to 0cm and stop.

Currently able to move by 30cm, stop and the program ends there.

I am not very good at programming and believe the problem is somewhere here.

void loop() {
 
 float rotation1 = (counter1 / diskslots);  // calculate RPS for Motor 1
  float distance1 = (rotation1); //circmference of wheel x rotation
  Serial.print(distance1);  
  Serial.println(" cm - ");
 if (distance1 <= 30.00)
 {
  moveForward();
  }
 else 
 motorStop();
 reverse();
}

void reverse(){

  
  attachInterrupt(digitalPinToInterrupt (MOTOR1), ISR_count2, RISING);
  float rotation2 = (counter2 / diskslots);  // calculate RPS for Motor 1
  float distance2 = (rotation2); //circmference of wheel x rotation
   Serial.print(distance2);  
  Serial.println(" cm - "); 
  moveBackward();
  if ( distance2 >= 30){
  motorStop();
  }
}

Main code

#include "TimerOne.h"
#include <AFMotor.h>      //add Adafruit Motor Shield library    
#include <NewPing.h>      //add Ultrasonic sensor library
#include <HCSR04.h>

// Initialize sensor that uses digital pins 13 and 12 for Ultrasonic sensor
int triggerPin = 12;
int echoPin = 13;
UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);

//For light encoder for motor
const int Left_IN_A0 = A0; // analog input
const int Left_IN_D2 = 2; // digital input 
const byte MOTOR1 = 2;  // Motor 1 Interrupt Pin - INT 0
//const byte MOTOR2 = 3;  // Motor 2 Interrupt Pin - INT 1
AF_DCMotor leftMotor (2, MOTOR12_1KHZ); //M2 in 
AF_DCMotor rightMotor (4, MOTOR12_1KHZ);
 
// Integers for pulse counters
unsigned int counter1 = 0;
unsigned int counter2 = 0;
// Float for number of slots in encoder disk
float diskslots = 20;  // Change to match value of encoder disk
void ISR_count1()  
{
  counter1++;  // increment Motor 1 counter value
} 
void ISR_count2()  
{
  counter2++;  // increment Motor 1 counter value
} 
/*void ISR_timerone()
{
  Timer1.detachInterrupt();  // Stop the timer
  Serial.print("Motor Speed 1: "); 
  float rotation1 = (counter1 / diskslots);  // calculate RPS for Motor 1
  float distance = (rotation1); //circmference of wheel x rotation
  Serial.print(distance);  
  Serial.println(" cm - "); 
  //counter1 = 0;  //  reset counter to zero
  Timer1.attachInterrupt( ISR_timerone );  // Enable the timer
}
*/
void setup() {
  Serial.begin(9600);
  
  Timer1.initialize(500000); // set timer for 1sec
  attachInterrupt(digitalPinToInterrupt (MOTOR1), ISR_count1, RISING);  // Increase counter 1 when speed sensor pin goes High
 // Timer1.attachInterrupt( ISR_timerone ); // Enable the timer
  leftMotor.setSpeed(195);
  leftMotor.run(RELEASE);
  rightMotor.setSpeed(205);
  rightMotor.run(RELEASE);
}

void moveForward() {
    //motorSet = "FORWARD";
    leftMotor.run(FORWARD);      // turn it on going forward
    rightMotor.run(FORWARD);     // turn it on going forward
 
}

void moveBackward() {
    //motorSet = "FORWARD";
    leftMotor.run(BACKWARD);      // turn it on going forward
    rightMotor.run(BACKWARD);     // turn it on going forward
 
}

void motorStop() {
  leftMotor.run(RELEASE);
  rightMotor.run(RELEASE);
}
void loop() {
 
 float rotation1 = (counter1 / diskslots);  // calculate RPS for Motor 1
  float distance1 = (rotation1); //circmference of wheel x rotation
  Serial.print(distance1);  
  Serial.println(" cm - ");
 if (distance1 <= 30.00)
 {
  moveForward();
  }
 else 
 motorStop();
 reverse();
}

void reverse(){

  
  attachInterrupt(digitalPinToInterrupt (MOTOR1), ISR_count2, RISING);
  float rotation2 = (counter2 / diskslots);  // calculate RPS for Motor 1
  float distance2 = (rotation2); //circmference of wheel x rotation
   Serial.print(distance2);  
  Serial.println(" cm - "); 
  moveBackward();
  if ( distance2 >= 30){
  motorStop();
  }
}

anyang:
https://www.e-gizmo.net/oc/kits%20documents/IR%20Speed%20Sensor/IR%20Speed%20sensor.pdf

i finally manage to found the datasheet after so long.

It seems to be a simple slotted optical switch. I can't think of any reason to use the analog output from it.

I suggest you write a short program that does nothing except count the pulses from the two devices and print the value to the Serial Monitor maybe once per second. There would be little point trying anything more complex if that does not work reliably.

...R