Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ADC #338

Closed
fchanson opened this issue May 26, 2015 · 72 comments
Closed

Using ADC #338

fchanson opened this issue May 26, 2015 · 72 comments

Comments

@fchanson
Copy link

Hi,

I made some tests on the ADC using a NodeMCU 0.9.5 build 20150318.
My code just reads the value on the pin every 500ms.

Using a Arduino program :

  • pin A0 at GND --> analogRead(A0) = 700
  • pin A0 at 3.3V --> analogRead(A0) = 909

I am surprised to have the value 700 when voltage on pin A0 is GND. Should be near 0.

I made the same test with a LUA program using ESPlorer.
The result is the following :

  • pin A0 at GND --> adc.read(0) = 4
  • pin A0 at 3.3V --> adc.read(0) = 1023

These last results sound good.

Does anyone have the same problem ?

Need something special to use the ADC pin ?

Thanks.

@fchanson
Copy link
Author

Hi,

I found in the file 'core_esp8266_wiring_analog.c' the code used for the analogRead method.

extern int __analogRead(uint8_t pin) {
if(pin == 17){
return readvdd33() >> 2; // readvdd33 is 12 bit
}
return digitalRead(pin) * 1023;
}

Does anyone can explain me this part of code ?
Why the method returns the VDD value divided by 4 ?

Thanks a lot.

@igrr
Copy link
Member

igrr commented May 27, 2015

This seems to be related to having or not having esp_init_default_data.bin flashed.
Apparently if you have it (for instance from a previous NodeMcu install), readvdd33 returns the voltage on VDD pin. If you don't, readvdd33 reads the value from the TOUT pin.

We'll add proper esp_init_default_data.bin to this environment at some point in the future.

@fchanson
Copy link
Author

Thanks for your reponse.

I don't know how it is possible to "remove" this file during the flash step.

Could you please help me on this point ?

@bluewalk
Copy link

Having this exact same issue, is there anything we can do to work around this?
And could you please elaborate about "some point in the future"?

@czopas
Copy link

czopas commented May 29, 2015

Hello, I have the same issue like friends before. I am using voltage divider 10k / 33k so on TOUT (ADC pin) I have 850mV. The result of analogRead(A0) is about 20. I have flashed ESP12 with firmware v0.9.5.2 AT Firmware.bin.
As I think, I should reflash my ESP12 with esp_init_default_data.bin and then ADC should work correct, right ? Where I can find this file ?

@bluewalk
Copy link

bluewalk commented Jun 2, 2015

As I think, I should reflash my ESP12 with esp_init_default_data.bin and then ADC should work correct, right ?
I've just tried that, according to this file https://developer.mbed.org/media/uploads/sschocke/esp_flasher.zip (which includes esp_init_default_data.bin) you need to flash it at address 0x7C000. This just breaks the whole arduino code and esp won't boot until you reupload your sketch. Which, then again, has the same not working ADC bug.

@igrr
Copy link
Member

igrr commented Jun 2, 2015

This esp_flasher has a bug which causes the first few sectors of flash to
be erased when you program the sector at 0x7c000. This is why the sketch
will not boot after flashing init_default.bin.
Regarding how this affects ADC, well, too bad it's not fixed by flashing
this config.

On Tue, Jun 2, 2015 at 5:12 PM, bluewalk notifications@github.com wrote:

As I think, I should reflash my ESP12 with esp_init_default_data.bin and
then ADC should work correct, right ?
I've just tried that, according to this file
https://developer.mbed.org/media/uploads/sschocke/esp_flasher.zip (which
includes esp_init_default_data.bin) you need to flash it at address
0x7C000. This just breaks the whole arduino code and esp won't boot until
you reupload your sketch. Which, then again, has the same not working ADC
bug.


Reply to this email directly or view it on GitHub
#338 (comment).

@bluewalk
Copy link

bluewalk commented Jun 2, 2015

Yes, too bad indeed. Do you have any other idea's that might help us finding a solution?

@igrr
Copy link
Member

igrr commented Jun 2, 2015

Sure.
Can you try reverting this change to see if that fixes ADC? On the module i have here both versions work reasonably well, but apparently not so on your side.

@bluewalk
Copy link

bluewalk commented Jun 2, 2015

Reverting the change provided fixes the issue I have with my boards!

@igrr
Copy link
Member

igrr commented Jun 2, 2015

Do you have any external resistors connected to the TOUT pin? Resistor divider?

@bluewalk
Copy link

bluewalk commented Jun 2, 2015

Yes, I'm using the following schematic:
adc_nodemcuboard
It's part of the NodeMCU dev board. I'm also using this schematic in one of my own designed pcb's.

@czopas
Copy link

czopas commented Jun 2, 2015

Great news :)

Where should I change core_esp8266_wiring_analog.c in my arduino folder ?

@bluewalk
Copy link

bluewalk commented Jun 2, 2015

You should change this file: C:\Users<>\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.4-673-g8cd3697\cores\esp8266\core_esp8266_wiring_analog.c

@igrr
Copy link
Member

igrr commented Jun 2, 2015

So we have two groups of users, and two versions. One version works for one group, another works for the rest. Have to figure out what the difference is.

@czopas
Copy link

czopas commented Jun 2, 2015

It's no working for me :( When I pull down ADC pin to ground and use
Serial.println(analogRead(A0));
I received values about 440 ...

Where is a problem ?

@bluewalk
Copy link

bluewalk commented Jun 2, 2015

I've just tried a ESP-12 besides my NodeMCU dev board and with ADC grounded I get readings around 380-430 and sometimes a 0.
When using the setup as shown in the schematic above, most readings are 0, but it has spikes to around 430. Applying 3.3V to the ADC pin using the above schematic gives me again most of the times 1023 but also readings around 430.

It seems that on an ESP that is not integrated on a dev-board the readings are sometimes off. Whilst in NodeMCU this same setup gives me a constant reading.

Grounded:

450
0
419
0
0
0
0
416
416

To VCC:

480
1023
1023
482
481
481
481
1023
548
1023

I've currently solved this by polling the ADC value twice with a three second delay, compare those, and if they are the same it's a good reading... not as it should, but kinda works atm.

@fchanson
Copy link
Author

fchanson commented Jun 3, 2015

I made the changes in the file core_esp8266_wiring_analog.c and the issue is fixed on my NodeMCU 0.9.5 build 20150318.
I made some tests on several NodeMCU (same firmware version) and the results are consistent and stable.

To ground:
NodeMCU 1 : 2
NodeMCU 2 : 5

To VCC:
NodeMCU1 : 1023
NodeMCU 2 : 1023

to VCC/2 (resistor divider):
NodeMCU1 : 534
NodeMCU2 : 539

@czopas
Copy link

czopas commented Jun 3, 2015

Ok I will try this on NodeMCU firmware (not v0.9.5.2 AT Firmware.bin) but I remember I had a problem with programming from Arduino IDE when I flashed NodeMCU firmware :( There was a problem with communication.

@fchanson
Copy link
Author

fchanson commented Jun 3, 2015

I used the NodeMCU flasher (https://github.com/nodemcu/nodemcu-flasher) with the internal binary file (INTERNAL://NODEMCU). This config uses nodemcu_integer_0.9.5_20150318.bin.

@bluewalk
Copy link

bluewalk commented Jun 3, 2015

Doesn't the Arduino IDE overwrite parts of the firmware anyway? Address 0x0 and 0x1? So if I understand correctly, you've flashed NodeMCU first and then flashed your generated bins from Arduino IDE through the upload button itself and that makes a difference?

@fchanson
Copy link
Author

fchanson commented Jun 3, 2015

yes you are right.
First I flashed NodeMCU.
Next I uploaded my sketch from Arduino IDE.

@bluewalk
Copy link

bluewalk commented Jun 3, 2015

I'll give that a try later when I get home. See if I can replicate it.

[edit]
Didn't do any change to my results, readings are still all over the place.

@czopas
Copy link

czopas commented Jun 3, 2015

On what speed you upload your sketch from Arduino IDE after flashing NodeMCU ? I have "error: espcomm_open failed" :(

@fchanson
Copy link
Author

fchanson commented Jun 3, 2015

Upload Speed : 115200
CPU Frequency : 80Mhz

[edit]
Are you sure about the port ?

@bluewalk
Copy link

bluewalk commented Jun 3, 2015

It must be my ESP-12 that is broken somehow, tried an ESP-07 with the above fix and that one works like a charm! Or is it because it had the AI-thinker firmware on at first? Will try to flash the ESP-12 with AI-thinker firmware and report back.

[edit]
Flash the ESP with ESP_8266_BIN0.92.bin, then upload your sketch. After that the readings are spot on!

@Testato
Copy link
Contributor

Testato commented Jun 6, 2015

i have same rpoblem on nodemcu dev board whit the original resistor divider.
we need this divider because the maximum analog value that the chip can receive is 1.0V.

If I put gnd directly on chip, whithout divider, i read correctly 0V

@czopas
Copy link

czopas commented Jun 9, 2015

Any new solution for this problem ? I still have various values from ADC even if I flash ESP_8266_BIN0.92.bin before uploading sketch.

@Testato
Copy link
Contributor

Testato commented Jun 9, 2015

Arduino Ide completely rewrite the flash, so there is no difference if you upload a .bin before Arduino upload.

@czopas
Copy link

czopas commented Jun 9, 2015

Yes I know, but someone wrote this resolve problem. I have no idea how to fix my issue :(

@igrr
Copy link
Member

igrr commented Jun 18, 2015

If the context here is the Arduino environment, placing that file in the SDK directory will not make Arduino upload esp_ini_data_default.bin to the chip. This is not currently handled in this core...

Edit: you can upload this file manually using esptool.py or esptool-ck. The address should be the size of your flash (as you set it in the IDE) minus 0x4000 (which is 0x7c000 for 512k sized flash).

@Testato
Copy link
Contributor

Testato commented Jun 18, 2015

ok thanks, so we must manually upload the file

i think this is an important feature to add to this core, because this byte is used also on the new function that adjust the RF power reading powersupply value on this pin, so actually, whit the original esp_ini_data_default.bin, that have Zero on 107th Byte, this function do not work.

From Documentation:
system_phy_set_tpw_via_vdd33
Function:
Adjust RF TX Power according to VDD33, unit : 1/1024 V
Note:
When TOUT pin is suspended, VDD33 can be got by system_get_vdd33;

@Testato
Copy link
Contributor

Testato commented Jun 18, 2015

ok, i tested many times today,

  • if i use the offical system_adc_read() the ADC value is correct.
  • if I use the offical system_get_vdd33 the value is always 65535

so i Think that from factory we have the 33value in the init .bin file that it is burned.

So for now untill in this core there is no init bin file uploading we can use only the analog read.
I modified the core_esp8266_wiring_analog.c (there is not necessary return the FFFF) and prepared a pullup request
#443

@igrr
Copy link
Member

igrr commented Jun 25, 2015

5b00eba adds a way to select ADC mode for the sketch — either TOUT or VDD.

@pRoFlT
Copy link

pRoFlT commented Jul 8, 2015

Thanks bluewalk i modified that file and it is working for me now. We shall see what other pitfalls it will have once i start the wifi communication part of my code.

@genguskahn
Copy link

I have been assembling an environment monitor within the Arduino IDE to email various parameters and used the LDR on the ADC(TOUT) on the ESP8266-201, this resulted in the device regularly crashing....after some tinkering with the circuit with no success I used the advice above indicated by Testato and made no other changes and this has been stable for more than 24 hours...

•if i use the offical "system_adc_read()" the ADC value is correct.

                              /-----[LDR]---GND

I use Vcc<--[22K]---<
----[10K]-----GND

This is the output without the MQ or PIR they are triggers for the email....

Logging Temperature: 19.30 deg Celsius - Humidity: 56.40% - Time: 10:55:31 - 28/7/2015 - Distance = 1cm - Light Value = 99% Saturation
Logging Temperature: 19.30 deg Celsius - Humidity: 56.30% - Time: 10:56:35 - 28/7/2015 - Distance = 1cm - Light Value = 6% Saturation
Logging Temperature: 19.30 deg Celsius - Humidity: 56.50% - Time: 10:57:39 - 28/7/2015 - Distance = 1cm - Light Value = 14% Saturation

The ADC Line in the Arduino IDE...

int LightV = map((system_adc_read()), 40, 900, 0, 100) ;
Serial.print(100-LightV);

Hope this is of help.......

@igrr
Copy link
Member

igrr commented Jul 28, 2015

@genguskahn are you using the latest stable build? ADC should work there without further changes.

@genguskahn
Copy link

Arduino IDE 1.64 with the board manager addition only.......
But this is now very stable........and accurate.....

@genguskahn
Copy link

It is the use of analogRead(A0) that results in the device crashing the only change was the line of code and this has been put back to check........I tried delays, smoothing caps, high value dividers feeding the LDR nothing changed until the line in the post above.....

@igrr
Copy link
Member

igrr commented Jul 28, 2015

Could you please clarify which version you are using (version number is shown in the boards manager) and which line you are referring to? This one?

@genguskahn
Copy link

I have just updated with the board manager and when I attempt to compile this error occurs?

Build options changed, rebuilding all
ESP_TH_Logger_Email_LCD_1.ino:51:25: fatal error: ESP8266WiFi.h: No such file or directory
compilation terminated.
Error compiling.
Any Ideas?

@genguskahn
Copy link

Version 1.6.5-947
int LightV = map((system_adc_read()), 40, 900, 0, 100) ;
place this line in.......

sorry there was a file protection error....this now compiles successfully will try the analogRead(A0).....

@genguskahn
Copy link

The update has corrected this as there have been enough that it would have crashed by now......

Allocated storage for 1268 data points.
Distance = 1cm - Light Value = 35% Saturation
Distance = 1cm - Light Value = 36% Saturation
Distance = 1cm - Light Value = 4% Saturation
Distance = 1cm - Light Value = 0% Saturation
Distance = 2cm - Light Value = 99% Saturation
Distance = 1cm - Light Value = 36% Saturation

Many thanks.........

@igrr igrr closed this as completed Jul 28, 2015
@kevin789900
Copy link

Can someone post recommended circuit for connecting light dependent resistor on ESP8266? Especially looking for which pin to pull up to. I'm using an adafruit huzzah. Thanks in advance!

@genguskahn
Copy link

Vcc to 22K ----> ADC Pin (tout) <-------LDR & 10K(LDR Resistance dependant) in parallel to GND (very stable), then use Map to set the final value...

@Testato
Copy link
Contributor

Testato commented Nov 4, 2015

Adc pin accept only a max value of 1V, so it is important chose a partitor value that at it' maximum value do not break this limit

@eta-sys
Copy link

eta-sys commented Dec 21, 2017

ADC or RF - choose yourself: http://www.esp8266.com/viewtopic.php?f=32&t=3905

@devyte
Copy link
Collaborator

devyte commented Dec 21, 2017

@eta-sys what is the point of your comment? The referenced discussion is over 2 years old, the relevant SDKs mentioned were superseded ages ago, and this issue is closed. My own app does adc reads with wifi up, without issues.

@eta-sys
Copy link

eta-sys commented Dec 22, 2017

I personally found this discussion because I have a few ESP8266 modules with 8Mb of memory and wanted to use them. I couldn't use SDK above 10.1 as my program simply doesn't fit into mem. I'm sorry if I made someone angry but the topic I quoted was very helpful for me.

@devyte
Copy link
Collaborator

devyte commented Dec 22, 2017

@eta-sys not angry, I asked seriously. I've seen several issues about the adc, but most reference old discussions or issues or code that is no longer relevant to the current state of the core, or were due to a bad power supply. At this point, I'm wondering why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests