Notifications
Clear all

ESP32 and 5V I2C sensors

9 Posts
5 Users
1 Reactions
3,116 Views
(@leansol)
Member
Joined: 5 months ago
Posts: 1
Topic starter  

I have a question that has really confused me. I connected an ESP32 to an AHT20 (an I2C-based temperature and humidity sensor) powered by 5V.

image

I connected the sensor directly to the ESP32 and checked the SCL and SDA pins, which showed a voltage of 5V. My question is, why wasn't the ESP32 damaged by this 5V connection? I looked at the datasheet for the development board, but I couldn't find any level shifters or protection circuits.

image

 

Then I added a MOSFET level shifter between the ESP32 and AHT20 sensor, and it works too.

image

 

Can I connect 5V I2C sensors directly to an ESP32 S3 Mini if I make my own PCB board, or should I use a MOSFET-based level shifter?

 



   
Quote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 7 years ago
Posts: 1169
 

That is confusing. I would have thought that the 5-volt input would have trashed the ESP32. Perhaps not using pull-up resistors is what saved it.

I would definitely go with the logic level converter in your final design.

😎

Bill


"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2006
 

Hi @leansol,

   To expand upon Bill (@dronebot-workshop)'s answer. I completely agree that a ESP32 GPIO should never directly be connected to a source of greater than 3.3V. Hence, using a logic level converter board, is appropriate when the source is a 5V logic output.

----------------

  I do not have any 'insider' information on the ESP32's internal design, but the 'common' approach for logic chip input and outputs (including GPIOs), is to include a pair of diodes, connected in series, at the junction of the diodes, to each I/O pin, with the other diode connections to VDD and GND. The diodes are connected such that the appropriate one, will conduct if the I/O pin goes more than about 0.6V negative of GND or more than 0.6V positive of VDD.

(Some chips have pins 'special' I/O pins that are specially designed to withstand voltages outside the range GND to VDD, but these will be described as a 'special feature' in the data sheet.)

These diodes are designed to protect the internals of the device from voltages generated by static electricity, which (in particular) can easily damage the gate insulating layer of MOS transistors. As the diodes are only intended to discharge small static electricity charges, they might easily be damaged by an unintended current flow, such as driving from a 5V logic output, into a logic chip whose VDD pin is connected to a 3.3V power supply.

Unfortunately, we can only guess whether your ESP32 has actually been damaged, even though it may appear to still be working correctly. In some cases, the respective diode may have been blown open-circuit, so that it can no longer provide any static electricity protection, but the rest of the chip is still intact, so continues to function normally, at least until it is subjected to a static charge.

In other cases, the input may be damaged, so that it draws a higher current than it should, reducing the magnitude of the incoming signal, but the natural 'resilience' of digital circuits, which incorporate wide voltage margins, allows the fault to remain 'hidden'. These cases sometimes show up as circuits that work 99% of the time, but occasionally fail or crash, typically at the most unfortunate or embarrassing moment!

And sometimes, it just continues to work ...

---------

Looking at the AHT20 data sheet, which I found on the AdaFruit site at

https://cdn-learn.adafruit.com/assets/assets/000/123/394/original/Data_Sheet_AHT20.pdf

I found the table:

image

 (click on image to enlarge)

Saying, it is specified to work with power supply voltages in the range 2.2V to 5.5V

I also spotted an example circuit in the same data sheet:

image

So while I note that I have no personal experience of this AHT20 device, if I was going to use it, my first approach would be to power it from the 3.3V power output of the ESP32 board. This would remove the need for logic level voltage conversion, as the output voltage on SDA and SCL lines will not exceed 3.3V.

Please note the above circuit suggested by the manufacturer of the AHT20, includes 3 resistors and a capacitor. If you are using an AHT20 mounted on a PCB, I suggest you check it includes these components, or consider adding them if they are missing.

(Most ESP32 boards, including the circuit you provided, have an onboard 5V to 3.3V voltage regulator, which can be used as 3.3V power source for external sensors like the AHT20 that only require a small current.)

-----

Sorry if this is a long answer to a short question, but I hope you find it useful. Best wishes, Dave



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 1 year ago
Posts: 428
 

The circuit diagram image appears to be a ESP32-DOIT-DEVKIT-V1-Board. The pinout I have for that board is (esp32-pinout-reference)

ESP32 DOIT DEVKIT V1 Board Pinout 36 GPIOs updated

If that is the board then the GPIO used appears to be a 3.3v pin.

What doesn't add up is the measured 5v on the SDA and SCL pins.


The one who has the most fun, wins!


   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2006
 

Hi @tfmccarthy (& @leansol),

    On closer examination, I agree that the circuit diagram and wiring diagram that @leansol provided do not match, so that it is unclear what configuration was actually connected. My answer was based on the wiring diagram and my personal experience with ESP32 boards. However, as such boards sold on AliExpress and similar marketplaces, often appear to be made in small batches, with variations in the exact board design between batches, probably caused by component availability, etc., I tend to assume such variations are 'normal'.

   Sorry, obviously, I can't see which board @leansol is using, nor can I verify what voltage was present on the SDL and SCL pins, so that my reply assumed:

  1. @leansol had actually observed 5V (or at least substantially more than 3.3V) on the SDL and SCL pins
  2. @leansol's wiring diagram was accurate ... which showed a pin labelled +5V connected to the red wire to the AHT20 module, and the adjacent pin was GND, and wired as the black wire shows.
  3. ESP32 dev modules usually have (at least one) 5V pin, that can be used as 5V output, and (at least one) 3.3V pin, that can be used as a 3.3V output for low current modules like the AHT20; both of these apply whilst the USB connector is supplying 5V. The actual board pin numbers for these supplies can vary, but the board markings are usually accurate.

If any of these assumptions are incorrect, then apologies if some or all of my comments are inappropriate or misleading, but were provided in good faith.

Hopefully, @leansol can review the actual board connections in conjunction with comments to decide the true situation, which as you @tfmccarthy point out, is presently confusing.

Best wishes to you both, Dave



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 1 year ago
Posts: 428
 

@davee,

Hopefully this won't muddy the waters any further but the 30 pin ESP32-DOIT-DEVKIT-V1 has the ground pin next to the 3,3v pin

ESP32 DOIT DEVKIT V1 Board Pinout 30 GPIOs

I'm still not clear how the measured 5v is possible. What is the logic level voltage of the GPIO pins when powered by the 5v. USB, regardless of the 3,3v pin usage? is it 5v?

@leansol,

Could you specify the actual ESP32 device you have and verify the pin connections?

- Tim


The one who has the most fun, wins!


   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2006
 

Hi Tim @tfmccarthy,

   I agree with your comments and questions ... clearly the original question included conflicting evidence ... a point I missed until I saw your first contribution.

My limited experience of ESP32 (including ESP32-xxxx variants) boards from AliExpress, etc., is that they all tend to be slightly different with each order, almost regardless of the description. This is compounded by Espressif, whose circuits presumably form the basis of most of the clones, also publish multiple variants of dev boards for the same ESP32 device.

Hence, I treat circuits and pinout diagrams of a specific model, such as the SP32-DOIT-DEVKIT-V1, as a 'useful starting point guide', not a 'hard specification', whilst almost expecting to find one or more small variations, such as pins in a different order, onboard LEDs connected to different GPIOs (which means Blink needs tweaking to work), and different USB chips (which sometimes means finding the right Windows/Linux driver can be a challenge.)

Thus, your suggestion that the two pins identified as 5V and GND on the original wiring diagram may actually have been 3.3V and GND is plausible, but conflicts with  @leansol's statement "I connected the sensor directly to the ESP32 and checked the SCL and SDA pins, which showed a voltage of 5V".

Clearly, @leansol is the only one of us who can clarify these contradictions.

 Thanks for your input, which I think illustrates the challenges we all face from time to time, and hopefully gives hints to others, when they face similar problems.

Best wishes and take care, Dave 



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 1 year ago
Posts: 428
 

Posted by: @davee

Thanks for your input, which I think illustrates the challenges we all face from time to time, and hopefully gives hints to others, when they face similar problems.

"Occupational hazard"

Circuit errors, wiring diagrams, etc., are required skills that take constant practice to master; even then we slip-up. II burnt out my first ESP32 board due to 5v intolerance. My impression is it's a "rite of passage." I also think they're the "Occam's razor" of circuit analysis. If I find myself trying to explain the impossible (measuring 5v on a ESP32 pin) I go back and recheck the circuit because that's the more likely culprit. Needless to say, I've had far more success doing that than convincing myself and others that the impossible is really possible.

And Dave, I'm not saying you don't know this ('cuz I know you do) but others may benefit.

YMMV

- Tim

 


The one who has the most fun, wins!


   
DaveE reacted
ReplyQuote
(@snoopy)
Member
Joined: 4 months ago
Posts: 3
 

@leansol Looking at your circuit diagram, you used 5V as Vcc for the sensor and connected the SDA/SCL lines without level converters. I2C ports by design are open drain and require pull-ups. Without any pull-ups there should be at most 3.3V on those lines, coming from the internal (weak) pull-ups of the ESP32. My best guess is that those pull-ups  are present on the sensor board. If your sensor comes from AdaFruit, there are 10k pull-ups present. Please check.

The value of those pull-ups is usually in the 10 k range, hence I would expect that the ESP32 SDA/SCL lines would survive the 5V coming in via those pull-ups. Ask the experts on the Espressif forum. I'm sure that they can tell you.

But is is possible that you fried the SDA/SCL ports on your ESP32. That doesn't necessarily mean that the entire ESP 32 has been fried. Try a simple 'blinky' program (obviously with the sensor disconnected) to see if there is any life left in the ESP32. If its still working you can (try to) simply re-assign other ports (assuming that not all ports have been fried) to the SDA/SCL function.

Conc. the question about level shifters: unless there is a compelling reason to use 5V for the sensor, just use the 3.3 V output from the ESP32 to feed the sensor. It is 3.3V compliant and uses only a few µAmps.

In my ESP32 designs I always add a protection circuit to the I/O ports (resistor + zener diode) if there is any risk that the input signal might exceed 3.3 V due du spikes (like from coils), shorts on wires or connectors or component failure.



   
ReplyQuote