micropython / micropython-esp32

Old port of MicroPython to the ESP32 -- new port is at https://github.com/micropython/micropython
MIT License
673 stars 216 forks source link

esp32/modesp.c: Added hall_sensor() function to esp module #211

Closed hcharan closed 5 years ago

hcharan commented 6 years ago

ADC1 needed to be configured for 12 bit output before calling the IDF's hall sensor function. It works, only issue might be ambiguity with machine module's ADC implementation. Board - WEMOS Lolin32 V1.0. It would be great if someone tests and reviews it.

dpgeorge commented 6 years ago

This functionality might find a better home in the ADC class as a special channel (eg the 8th channel), something like:

import machine
hall = machine.ADC(8)
hall.read()
nickzoic commented 6 years ago

Yeah, maybe.  The hall sensor, and the low-noise-preamp (LNA) mode which uses the SENS_VN and SENS_VP pins, are a special case not fully supported by ESP-IDF just yet.  I'm following up with Espressif ...

hcharan commented 6 years ago

I added it to the esp module because hall sensor is esp specific functionality, similar to #192 ... The wrapper for hall sensor was added to the Arduino port as well, so I thought the IDF implementation was complete enough to port it here. Internally, the IDF's hall_sensor_read() function I'm calling uses channels 0-3 of the ADC1. As in http://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/adc.html?highlight=hall.

MrSurly commented 6 years ago

Tried it, and it works. Might want to return a signed integer. With no magnet, I get 110-ish. With a small strong neodymium, I can get around 1500, but flipping the other way I got 4294965952, which works out to about -1500 w/ two's complement.

robert-hh commented 6 years ago

Confirmed. According to Eric's comment, I changed the last line of the function to:

    return MP_OBJ_NEW_SMALL_INT(hall_sensor_read());

The base level here is is in the 30's, and the reading is quite noisy, and dose not change with the orientation of the board. Flipping an external magnet changes the sign.

nickzoic commented 6 years ago

OK so there's something interesting about the Hall Effect Sensor: there's a differential "Low Noise Amplifier" in the ESP32, switchable onto the same pins as ADC0 and ADC3, which should allow much greater accuracy in hall effect sensor readings. I think it'll likely allow external differential input too (eg: for strain gauges and stuff). There's some hardware registers documented which can switch the LNA on and off and so on, but the details are pretty unclear. But the hall_sensor_read function in the current ESP-IDF doesn't currently use the LNA, and that feature is still in development at Espressif. The current version is somewhat of a hack, reading ADC0 and ADC3 separately and subtracting one from the other. The API is unlikely to change much but the range and resolution may. In short: this is a worthy addition, and the PR is appreciated but I think it'd be wise to hold it here until the situation clarifies, and we also need to think about the API for accessing the LNA for other purposes.

nickzoic commented 6 years ago

( @hcharan and @robert-hh : Sorry for the delay, I'm continuing to follow this up with Espressif and hope we'll see a better hall_sensor_read() in the near future, at which point we can re-test this and hopefully merge it in!)

omarahmad81 commented 6 years ago

@nickzoic Hi, any progress with a better hall_sensor_read()? Specifically with using LNA for the Hall sensor input? Eagerly waiting for it.

dpgeorge commented 5 years ago

Support for reading the hall sensor was added upstream as esp32.hall_sensor() in commit https://github.com/micropython/micropython/commit/5e5aef53fb45dff57db9f4e054089a15e87ebb1b