使用霍尔效应传感器(A3144)测量电机转速的Arduino转速表

Arduino Tachometer using a Hall Effect Sensor (A3144) to Measure RPM of a Motor

本文关键字:测量 电机 Arduino A3144 传感器      更新时间:2024-09-21

我目前正在尝试设置一个Arduino Uno和一个霍尔效应传感器(A3144(来测量电机的转速。我已经测试了传感器;0";当感测磁体时;1〃;当磁体被移除时。我也编写了我认为可以工作的代码,但当我测试程序时,串行监视器上什么都没有显示。如果有人对我如何更改编码以使其发挥作用有任何想法,我将非常感谢您的建议!

这是我的代码:

int sensorPin = 2;            //Hall Effect Sensor at Digital Pin 2
float hall_thresh = 100.0;    //Set number of hall trips for RPM reading
void setup() {
Serial.begin (115200);      // setup serial - diagnostics - port 
pinMode (sensorPin, INPUT); // setup pins
}
void loop() {
// preallocate values for tachometer
float hall_count = 1.0;
float start = micros();
bool on_state = false;
// counting the number of times the hall sensor is tripped
while (true) {
if (digitalRead(sensorPin) == 0) {
if (on_state == false) {
on_state = true;
hall_count += 1.0;
}
} else {
on_state = false;
}
if (hall_count >= hall_thresh) {
break;
}
}
// print information about Time and RPM
float end_time = micros();
float time_passed = ((end_time-start)/1000000.0);
Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (hall_count / time_passed) * 60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
delay(1);                   // set delay in between reads for stability
}

这是您的代码的再创造,如果它不起作用,不要恨我。首先尝试,我只是确认它编译了,没有完成调试。我有一个非常类似的设置,它在Teensy 4.0上作为发动机控制单元(ECU(操作,因此使用类似的方法获得RPM被证明是有效的。

int sensorPin = 3; // interupt pin on UNO, adjust according to board.
unsigned long value = 0;
int hall_thresh = 100;    //Set number of hall trips for RPM reading
unsigned long startTime = 0;
void setup() {
Serial.begin(115200);
pinMode(sensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(sensorPin), addValue, RISING);
}
void loop() {
if (value >= hall_thresh) { // Math copied from StackOverflow question. 
unsigned long endTime = micros();
unsigned long time_passed = ((endTime - startTime) / 1000000.0);
Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (value / time_passed) * 60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
value = 0;
startTime = micros();
}
}
void addValue() {
value++;
}

如果你有任何问题,请发布你的问题,我很乐意帮助调试。

我遇到了同样的问题,但我使用了stm32f103c8t6(但这并不重要(。

问题在于";CCD_ 1";变量由于你从数字1(float hall_count = 1.0;(开始计数,while循环等待了99次(将磁铁放在传感器附近和远离传感器的99次(;hall_thresh";。

如果hall_count等于hall_thresh,则while循环将中断并向串行监视器发送时间和RPM。

因此,尝试将hall_thresh变量更改为10或5,并用手中的磁铁进行尝试。

下次使用电机时,您将把hall_thresh变量更改为更高的数字,因为电机速度要快得多。

希望有帮助:(