3.3V <-> 5V bidirectional level shifter - exampple for AM2302 (DHT22)

Reiner1210
Posts: 39
Joined: Tue Mar 20, 2018 6:28 pm

3.3V <-> 5V bidirectional level shifter - exampple for AM2302 (DHT22)

Postby Reiner1210 » Tue Apr 10, 2018 6:32 am

For connecting a 5V device on the ESP32 you should use a level shifter to protect your ESP32 (ESP32 is not 5V tolerant)
There are a lot of false informations and rubbish in the net concerning this.

For building a level shifter you need only 3 parts - 2 resistors and one n-channel MOSFET. Costs some cents.
Here a circuit I use to connect an AM2302 (https://akizukidenshi.com/download/ds/aosong/AM2302.pdf )temperature/humidity sensor, which has a 1-wire communication, to an ESP32. I copy this from the header file of my AM2302 driver (which I will publish some times later):

Code: Select all


* ATTENTION: if you want to operate the sensor at the recommended 5V you need a "level shifer"
* because ESP32 uses 3.3V and the inputs are not 5V tolerant.
* I recommend the following wiring for protecting your ESP32:
 
 
                       +3.3V   +3.3V    +5.0V    +5.0V     GND
                         o       o        o        o        o
                         |       |        |        |        |
                         |       |        |        |        |
                         |       |        |        |        |
                        | |      |       | |       |        |
                        | | R2   |       | | R3    |        |
                        |_|      |       |_|       |        |
                         |  ____ |______  |        |        |
               ____      | |     G      | |        |        |
  GPIO o------|____|-----o-|S  2N7000  D|-o        |        |
 (ESP32)        R1         |____________| |        |        |
                                 T1       |        |---||---|
                                          |        |   C1   |
                                       ___|________|________|______
                                      |  SDA(2)   VDD(1)   GND(4) | 
                                      |                           |
                                      |                           |
                                      |          AM2302           |
                                      |          (DHT22)          |
                                      |                           |
                                      |___________________________|
                                        
                                        
Parts:
       T1:  n-channel MOSFET e.g. 2N7000, pay attention that you connect source (S) and drain (D) correctly !
       R1:  Resistor 330 Ohm
       R2:  Resistor 10 kOhm
       R3:  Resistor 10 kOhm
       C1:  Capacitor 100 nF

       The T1, R2, and R3 work as a bidirectional level shifter.
       
       R3 is an additional protection if e.g. GPIO is set accidentally to high and SDA is low.
       I only recommend it in experimental systems, in production systems you do not need it.
       
       C1 prevents the AM2302 from oscillating. Place it as near as possible to the sensor.
       
       As GPIO you have to take a pin wich can work as output AND input.


Who is online

Users browsing this forum: Bing [Bot] and 117 guests