mongoose-os-libs / adc

ADC support
Other
3 stars 7 forks source link

ADC.read() on ESP8266 always returns 65535 reading VCC voltage. #2

Open ghost opened 6 years ago

ghost commented 6 years ago

I am using the ADC library in order to read the VCC voltage of an ESP8266 device. I followed these instructions:

  1. As per ESP8266 reference guide recommendations ("To read VCC voltage, ADC pin must be kept unconnected...") I left the ADC pin unconnected (floating without resistors);
  2. As per MongooseOS recommendations , I added MGOS_ADC_MODE_VDD: 1 to build_vars in mos.yml;
  3. I added ADC.enable(0) instruction to init.js;
  4. I added ADC.read(0) instruction to init.js;

Unfortunately ADC.read() fails. It always returns 65535.

I tested it on both ESP-12F (4M flash) and ESP-01S (1M flash), but I had the same result: 65535.

For more details see attached files. With best regards, Filippo Balestrini

blynk-demo-js-1M.zip

cpq commented 6 years ago

I've just flashed demo-js on ESP8266 NodeMCU: mos flash esp8266

Then wrote this in init.js:

load('api_adc.js');
load('api_gpio.js');

// ADC.enable(0);
GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
  print('Button press, pin: ', ADC.read(0));
}, null);

Pressing the flash button, this is what I see:

[Jan 31 16:49:18.423] Button press, pin:  8 
[Jan 31 16:49:18.723] Button press, pin:  8 
[Jan 31 16:49:18.957] Button press, pin:  2 

Could you confirm that you see the same?

ghost commented 6 years ago

I will try it soon. Meantime a couple of questions: Why is ADC.enable(0) commented? Why is the 3th result 2 instead of 8?

On Jan 31, 2018 17:50, Sergey Lyubka notifications@github.com wrote:

I've just flashed demo-js on ESP8266 NodeMCU: mos flash esp8266

Then wrote this in init.js:

load('api_adc.js'); load('api_gpio.js');

// ADC.enable(0); GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) { print('Button press, pin: ', ADC.read(0)); }, null);

Pressing the flash button, this is what I see:

[Jan 31 16:49:18.423] Button press, pin: 8 [Jan 31 16:49:18.723] Button press, pin: 8 [Jan 31 16:49:18.957] Button press, pin: 2

Could you confirm that you see the same?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/mongoose-os-libs/adc/issues/2#issuecomment-361995396, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AYpdVqk38DWRoucJsLNwI07GYH25ORN4ks5tQJntgaJpZM4R0PRh.

ghost commented 6 years ago

I did the same test using an ESP8266-01S (the ADC pin unconnected by factory default).

The init.js

load('api_adc.js');
load('api_gpio.js');
load('api_timer.js');

// ADC.enable(0);
Timer.set(5000, Timer.REPEAT, function() {
  print('Button press, pin: ', ADC.read(0));
}, null);

Output (build with MGOS_ADC_MODE_VDD: 1)

[Feb  2 16:21:02.356] Button press, pin:  65535 
[Feb  2 16:20:57.339] Button press, pin:  65535
[Feb  2 16:21:32.338] Button press, pin:  65535

Output (build without MGOS_ADC_MODE_VDD: 1)

[Feb  2 16:24:51.996] Button press, pin:  68 
[Feb  2 16:24:56.998] Button press, pin:  68 
[Feb  2 16:25:01.999] Button press, pin:  68 
[Feb  2 16:25:07.000] Button press, pin:  68 
[Feb  2 16:25:12.001] Button press, pin:  67 
[Feb  2 16:25:12.001] Button press, pin:  67 
[Feb  2 16:25:22.003] Button press, pin:  66 
dawnstrider commented 6 years ago

So far I have the same result as @ZenDIY . Either 65535 or some random number. Used an ESP8266 NodeMCU unit.

ghost commented 6 years ago

@dawnstrider As per ESP8266 recommendation, the ADC pin must be "floating" to measure VDD. No resistors have to be connected. Unfortunately, in ESP8266 boards (like NodeMCU or WeMos) the ADC pin is usually connected to 2 resistors acting as voltage divider. So, I desoldered all resistors connected to the ADC pin (making it "floating") before running my test. I suggest you to double check your board, desolder resistor if needed and run the test again.

However, mind that, as far I know, desoldering resistors (the voltage divider) your board will not be able to measure analog inputs anymore.

dawnstrider commented 6 years ago

Hi @ZenDIY thanks for the tip. I did some more digging and found a "bug" in the mongoose build system.

According to the ESP8266 documentation, you can switch the analog pin to either read an external voltage or the internal 3.3 V status. To do the latter, according to the documentation, you need to set byte 107 ( hex 6B ) of the esp_init_data_default.bin file that is written at flash time to 0xFF in order to tell the ESP8266 to use that feature.

Funny enough, the ADC module documentation mentions that you have to set MGOS_ADC_MODE_VDD to 1 to enable this feature. The makefiles even look for this flag and pick another esp_init_data_default.bin for this purpose to be packed into the fw.zip.

Sadly this file does not have the 107th byte set to 0xFF!!

I switched to a local build instead of using the mongoose cloud. This downloads the dependencies to my machine. I edited the esp_init_data_default.bin which is actually called _esp_init_data_defaultv08.bin in the mongoose-os package. After compiling with this change, I get the correct voltage when calling

ADC.read(0)

Job done! Now how can I tell Mongoose to change that in their core repository?

dawnstrider commented 6 years ago

Oh well I created a pull request to fix this bug. See https://github.com/cesanta/mongoose-os/pull/441 for details.

ghost commented 6 years ago

Hi @dawnstrider, you did a great job. Excellent! I hope mongoose-os team will fix the issue soon. Many thanks.