使用霍尔效应传感器(A3144)测量电机转速的Arduino转速表
Arduino Tachometer using a Hall Effect Sensor (A3144) to Measure RPM of a Motor
我目前正在尝试设置一个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
变量更改为更高的数字,因为电机速度要快得多。
希望有帮助:(
相关文章:
- 在C++中测量postscript字体宽度
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 用时钟测量程序的运行时间
- 用于测量微秒的QueryPerformanceCounter的奇怪行为
- 为什么根据测量位置测量经过的时间时会有很大的不同?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 如何在 setup() 函数中使用数字引脚转动电机?
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 分别测量每个线程上花费的 CPU 时间(C++)
- 如何为伺服电机创建中断或返回值?
- 如何测量管道延迟?
- 测量时间以在 c++ 中生成更好的随机数
- OpenCL 内核计时测量 0 秒或导致 SIGABRT
- 给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?可能吗?
- 使用 Qt 测量过程的实际运行时间
- 为什么 std::chrono 在测量循环和编译器优化的并行 OpenMP 的执行时间时不起作用?
- V8垃圾收集器回调,用于测量GC活动
- 无法驱动带有 Adafruit 电机屏蔽 V2.3 的直流电机
- 测量精确的时间,单位为纳秒C++
- 如何测量cudaMalloc执行时间