DEV Community

loading...

在 Arduino IDE 中 ESP32 的 hallRead() 傳回亂數

黃昕暐
An engineer who's interested in explaining technology.
・1 min read

如果你在 Arduino IDE 中開發 ESP32 的程式, 可能會遇到內建的霍爾感測器值亂跳, 根本無法使用的情況, 像是這樣:

2021-02-02

這很可能是因為 ESP32 套件版本的關係:

Hall Sensor returns random values - unaffected by presence of strong magnet #4866

Since v1.0.5 update, hallRead() return random data. (Was working ok in V1.0.4)

When a strong magnet is present near chip random data persists as though magnet has no effect on hall sensor.

Same results when trying 2 different Sparkfun ESP32 Thing Plus boards.

using Mac OSX as build device


int val = 0; void setup() { Serial.begin(115200); }

void loop() { // put your main code here, to run repeatedly: val = hallRead(); // print the results to the serial monitor: //Serial.print("sensor = "); Serial.println(val);//to graph

delay(100); }

Debug Messages:

4:51:56.272 -> -170 14:51:56.272 -> 71 14:51:56.272 -> -268 14:51:56.272 -> -263 14:51:56.272 -> -263 14:51:56.347 -> -263 14:51:56.347 -> -294 14:51:56.347 -> -295 14:51:56.347 -> -297 14:51:56.347 -> -53 14:51:56.347 -> -172 14:51:56.347 -> -170 14:51:56.347 -> -170 14:51:56.347 -> 93 14:51:56.347 -> -167 14:51:56.347 -> -167 14:51:56.347 -> -168 14:51:56.347 -> -172 14:51:56.347 -> -166 14:51:56.347 -> -167 14:51:56.347 -> -166 14:51:56.347 -> 72 14:51:56.347 -> -173 14:51:56.347 -> -171 14:51:56.347 -> -170 14:51:56.347 -> -167 14:51:56.347 -> -164 14:51:56.347 -> -168 14:51:56.347 -> -168 14:51:56.347 -> 71 14:51:56.347 -> -234 14:51:56.347 -> -238 14:51:56.347 -> -261 14:51:56.347 -> -264 14:51:56.347 -> -266 14:51:56.347 -> -266 14:51:56.347 -> -291 14:51:56.347 -> -60 14:51:56.347 -> -170 14:51:56.347 -> -170 14:51:56.347 -> -162 14:51:56.347 -> 68 14:51:56.347 -> -234 14:51:56.347 -> -145 14:51:56.347 -> -168 14:51:56.347 -> -169 14:51:56.347 -> -171 14:51:56.347 -> -169 14:51:56.347 -> -171 14:51:56.347 -> -166 14:51:56.347 -> -168 14:51:56.347 -> 93 14:51:56.347 -> -170 14:51:56.347 -> -168 14:51:56.347 -> -170 14:51:56.347 -> -169 14:51:56.347 -> -169 14:51:56.347 -> -170 14:51:56.347 -> -169 14:51:56.347 -> 71 14:51:56.347 -> -269 14:51:56.347 -> -266 14:51:56.347 -> -263 14:51:56.347 -> -263 14:51:56.347 -> -263 14:51:56.347 -> -296 14:51:56.347 -> -295 14:51:56.347 -> -48 14:51:56.347 -> -170 14:51:56.347 -> -168 14:51:56.347 -> -172 14:51:56.347 -> 67

根據我自己的實測, 在 1.0.4 或更早的版本是沒有問題的, 像是以下的展示影片:

如果使用 1.0.5 或是 1.0.6 就會發生上述的問題。解決方法很簡單, 在沒有修正前, 就是降版本囉。或者, 你也可以自己研究一下原始碼, 找出問題, 回饋社群囉。

補充:經過反映後, 官方回應如下:

I'll fix this in v2.0. For 1.0.x, you can use this:

#include <driver/adc.h>
void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.printf("idf read: %d\n", hall_sensor_read());
  delay(100);
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)