I am using this code to collect the data from the hall sensor and oscilloscope to compare if I am missing some data.
I have assign 1412 revolutions for the gear motor, this is for clockwise and counterclockwise.
While I start reading the data from the oscilloscope for counter wise the cycles are 1490 and for the clockwise, it is 1451.
How can I get more accurate data to compare the data im getting through the serial monitor and oscilloscope
I am using this code to collect the data from the hall sensor and oscilloscope to compare if I am missing some data.
I have assign 1412 revolutions for the gear motor, this is for clockwise and counterclockwise.
While I start reading the data from the oscilloscope for counter wise the cycles are 1490 and for the clockwise, it is 1451.
How can I get more accurate data from the gear motor and hall sensor to compare it oscilloscope
where ever you play with half_revolutions outside the ISR, you should ensure this is in a critical section (protected against Interrupts) as an interrupt during the loop could set you off - especially as your variable is on 2 bytes.
So I would not trust blindly the data printed by your program
Hi,
Ah... you are trying to make the ANALOG and DIGITAL worlds agree with each other. A daunting task.
Tell us more (photo?) of your setup. How is the magnetic sensor arranged? Hmm.. Is the oscilloscope able to count pulses?? Sorry, I don't understand the details...
Please Pardon my presumption, but I wanted to try to understand the code. Also, please use CODE tags to show your work, and use Auto Format.
CODE:
//Define pin setup
#define motor_in1 7
#define motor_in2 8
#define motor_enA 9
#define sensorPin 2
volatile int half_revolutions = 0;
int revolutions_per_pos;
int pos = 0;
void setup()
{
// Set pin mode
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
Serial.begin(115200);
// Hallsensor connected to the interrupt pin (Pin 2 on Arduino Uno)
attachInterrupt(digitalPinToInterrupt(sensorPin), magnet_detect, RISING);// attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
}// END SETUP
void loop()
{
// Run full cycle to each position and back to 0
revolutions_per_pos = 706;
move_amount(1);
pos = 1;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(-1);
pos = 0;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(2);
pos = 2;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(-2);
pos = 0;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(3);
pos = 3;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(-3);
pos = 0;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(1);
pos = 4;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(-1);
pos = 0;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(2);
pos = 5;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(-2);
pos = 0;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(-3);
pos = 6;
Serial.print(pos);
delay(2000);
revolutions_per_pos = 706;
move_amount(3);
pos = 0;
Serial.print(pos);
delay(2000);
} // END OF LOOP
//--------( USER WRITTEN FUNCTIONS )--------------------
void magnet_detect()
{
half_revolutions++;
}
void move_amount(int no_pos)
{
half_revolutions = 0;
if (no_pos < 0)
{
// Set rotation direction
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
no_pos *= -1;
}
else
{
// Set rotation direction
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
while (half_revolutions < (no_pos * (2 * revolutions_per_pos)))
{
Serial.println(half_revolutions);
analogWrite(motor_enA, 100);
}
analogWrite(motor_enA, 0);
}
when you do maths or take decision based on half_revolutions, do those on a local temporary copy that you obtain in a protected section using cli() and sei() (or noInterrupts() and interrupts() which are basically the same thing in plain english).
noInterrupts();
int hr_local_copy = half_revolutions;
interrupts();
// use hr_local_copy for what you want to do, this way it does not change from under you
also depending how fast your motor goes, an int might not be the best variable to hold the data.
If the speed can affect the data what will be the best option to get the data. How accurate the data will be from the hall sensor and then can see the same data on the oscilloscope
I am trying to compare the data I received from the hall sensor to an oscilloscope. I have attached the file in which we can see the data from the oscilloscope.
It is not uncommon for DC motors to run slightly faster in one direction vs the other, say you set analogWrite(254), your motor might run 2000 RPM CW and 2060 CCW, a 3% difference. You might try to equalize by setting the faster direction 3% slower:
The fun part is a little later, when you use the sensor output to CONTROL the DC motor so it goes exactly as you want. But you have the right basics, including good instrumentation.
In this type of experiment, think about having your CODE use a digital output to mark specific events in the code, and also look at that digital pin with your oscilloscope. I have done things (with a multichannel logic analyzer) with several code timing points along with signals from the hardware being tested. If there is a known occasional error you are looking for, many logic analyzers can trigger on a specific pattern. Some high end scopes also have a word recognizer add-on.