User Tools

Site Tools


esp8266:how-to-use-all-esp01-gpio-pins

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
esp8266:how-to-use-all-esp01-gpio-pins [2019/10/06 14:33]
Ilias Iliopoulos created
esp8266:how-to-use-all-esp01-gpio-pins [2024/02/02 21:49] (current)
Ilias Iliopoulos
Line 1: Line 1:
-====== How to use all ESP-01 GPIO pins ======+====== How to use "all" ​ESP-01 GPIO pins ======
  
 ===== Introduction ===== ===== Introduction =====
Line 9: Line 9:
   - RXD and TXD are used for interfacing purposes, ​   - RXD and TXD are used for interfacing purposes, ​
   - the Reset (RST) and Enable (CH_PD) pins cannot be used for any other purpose, ​   - the Reset (RST) and Enable (CH_PD) pins cannot be used for any other purpose, ​
-  - GPIO0 is used during flashing the operating software, ​+  - GPIO0 and GPIO2 are used during flashing the operating software, ​
  
-Finally, only GPIO2 pin seem to remain ​as a pin to be used for general purpose input/​output. In practice, even GPIO2 has some peculiarities that make it hard to use.+Finally, only GPIO2 pin remains ​as a pin to be used for general purpose input/​output, ​and even this under specific conditions.
  
-There are of course several ways to bypass the problem. The easiest by far is to dump ESP-01 and use one of the several modules, such as the ESP-07 or ESP-12, or one of the development platforms such as NodeMCU, ​WeeMos ​D1 etc. But, engineers are notorious for trying to squeeze every drop of juice, especially when our project requires only minimal resources.+There are of course several ways to bypass the problem. The easiest by far is to dump ESP-01 and use one of the several modules, such as the ESP-07 or ESP-12, or one of the development platforms such as NodeMCU, ​WeMos D1 etc. which provide a larger number of GPIO pins. But, engineers are notorious for trying to squeeze every drop of juice, especially when our project requires only minimal resources, or when we have several spare ESP-01 modules in our lab bins.
  
-In this article, I will show you how to use all the available pins of ESP-01, in a couple of use cases. The article ​is intentionally small, because its purpose is only to present ​"​how"​ it is done and not the details of "​why"​ it is done like this. Please consult the official documentation. Unfortunately,​ the popularity of the ESP8266 has made most of us learn the system ​mostly through articles and experimentation,​ rather than studying the lengthy documentation,​ and therefore, it is not my intention to present information that is not 100% correct.+In this article, I will show you how to use all the available pins of ESP-01, in a couple of use cases. The article ​present ​only "​how"​ it is done and not the details of "​why"​ it is done like this. Please consult the official documentation ​to find additional information. Unfortunately,​ the popularity of the ESP8266 has made most of us learn the ESP-8266 platform ​mostly through articles and experimentation,​ rather than studying the lengthy documentation,​ and therefore, it is not my intention to present information that is not 100% correct.
  
 In addition, bear in mind that __there is no such thing as a free lunch__. There are dangers in the process. Make sure that you have read and fully understand the [[esp8266:​how-to-use-all-esp01-gpio-pins#​WARNING|WARNING]] section below. **You have been warned!!!** In addition, bear in mind that __there is no such thing as a free lunch__. There are dangers in the process. Make sure that you have read and fully understand the [[esp8266:​how-to-use-all-esp01-gpio-pins#​WARNING|WARNING]] section below. **You have been warned!!!**
Line 50: Line 50:
 Pin GPIO0 needs to be in a HIGH state (that is, its voltage should be near Vcc) so that the stored program runs normally. If, for any reason, ​ the voltage at the pin is at a level that is considered by the processor as logic LOW, the processor will enter flash mode. Therefore, in order to use the pin for our purposes, we must make sure that the voltage is logic HIGH at start-up. Control circuits that are based on a "​positive"​ logic, such the one presented below, result in pulling the pin voltage to LOW and therefore are inappropriate. Please note that the circuit below **DOES NOT WORK**.  ​ Pin GPIO0 needs to be in a HIGH state (that is, its voltage should be near Vcc) so that the stored program runs normally. If, for any reason, ​ the voltage at the pin is at a level that is considered by the processor as logic LOW, the processor will enter flash mode. Therefore, in order to use the pin for our purposes, we must make sure that the voltage is logic HIGH at start-up. Control circuits that are based on a "​positive"​ logic, such the one presented below, result in pulling the pin voltage to LOW and therefore are inappropriate. Please note that the circuit below **DOES NOT WORK**.  ​
  
 +{{ :​esp8266:​esp01_gpios_do_not_do.png|}}
  
 +The solution is to implement a **negative logic** in our code. This means that HIGH voltage on the pin will signify an **"​Off"​** state, whereas a LOW voltage will be the **"​On"​** state. The circuit to implement this method employs a PNP transistor, followed by an NPN transistor. In this example circuit, a relay is driven by GPIO0. When we want to activate the relay, we write LOW to pin 0:
  
-The solution is to implement a negative logic in our code. This means that HIGH voltage on the pin will signify an **"​Off"​** statewhereas a LOW voltage will be the **"​On"​** state. The circuit to implement this method employs a PNP transistor, followed by an NPN transistor, as shown below.+<code c++> 
 +digitalWrite(0, LOW); 
 +</​code>​
  
 +And when we want to de-activate the relay, we write HIGH.
  
-Using two transistors instead of onemay be considered a burden. Yet, this implementation has the advantage of separating the power source of the processor from the power source of the load. It is easier to use relays, buzzers and other devices specified for use at 5V, 12V or higher, rather than at 3.3V. (__Most of us have plenty of such equipment in our lab bins__). Therefore, the momentary spikes in the supply voltage that are caused by the switching from on to off and vice versa, do not interfere with the processor.+<code c++> 
 +digitalWrite(0HIGH)
 +</​code>​
  
-The values of the resistors may be modified ​and optimised according to the current consumed by the load.+Because I try to write code that can be re-used in several projects, I introduce a constant such as **gpioLogic** ​and the code goes like:
  
-The same control circuit can be used with pin GPIO2, giving us one more valuable pin.+<code c++> 
 +/* 
 +  Set true for a positive logic or false for a negative logic 
 +*/ 
 +const bool gpioLogic = false; 
 + 
 +void setup() { 
 + 
 +... 
 +   ​pinMode(2,​ OUTPUT); 
 +....    
 +
 + 
 +void relayOn(int pin) { 
 +  if (gpioLogic) 
 +    digitalWrite(pin,​ HIGH); 
 +  else 
 +    digitalWrite(pin,​ LOW);   
 +
 + 
 +void relayOff(int pin) { 
 +  if (gpioLogic) 
 +    digitalWrite(pin,​ LOW); 
 +  else 
 +    digitalWrite(pin,​ HIGH);  
 + 
 +
 + 
 +</​code>​  
 + 
 +{{ :​esp8266:​esp01_gpios.png |}} 
 + 
 +Using two transistors instead of one, may be considered a burden. Yet, this implementation has the additional advantage of separating the power source of the processor from the power source of the load. It is easier to use relays, buzzers and other devices specified for use at 5V, 12V or higher, rather than at 3.3V. (__Most of us have plenty of such equipment in our lab bins__). Therefore, the momentary spikes in the supply voltage that are caused by the switching from on to off and vice versa, do not interfere with the processor. 
 + 
 +The transistors are selected depending on the load consumption and the load operating voltage. The values of the resistors may be modified and optimised according to the current consumed by the load as well as the characteristics of the transistors. 
 + 
 +A similar ​control circuit can be used with pin GPIO2, giving us one additional ​valuable pin.
  
 ===== Use case: Using pins TXD and RXD as I2C pins===== ===== Use case: Using pins TXD and RXD as I2C pins=====
Line 70: Line 113:
  
 See the circuit below. The series resistors provide some safety from short-circuiting a pin in case both sides operate as outputs, before our code takes over. See the circuit below. The series resistors provide some safety from short-circuiting a pin in case both sides operate as outputs, before our code takes over.
 +
 +{{ :​esp8266:​esp01_txrx.png |}}
  
 In terms of software, in order to force pins TXD/GPIO1 and RXD/GPIO3 to operate as I2C pins functioning as SDA and SCL respectively,​ we must set in our code: In terms of software, in order to force pins TXD/GPIO1 and RXD/GPIO3 to operate as I2C pins functioning as SDA and SCL respectively,​ we must set in our code:
  
-<​code>​+<​code ​c++>
 Wire.begin(1,​ 3) Wire.begin(1,​ 3)
 </​code>​ </​code>​
Line 79: Line 124:
 The syntax is: The syntax is:
    
-<​code>​+<​code ​c++>
 Wire.begin(sda_pin,​ scl_pin). Wire.begin(sda_pin,​ scl_pin).
 </​code>​ </​code>​
Line 85: Line 130:
 ===== Conclusion ===== ===== Conclusion =====
  
-I have presented ways to exploit an ESP-01 as much as possible and utilize all four pins, instead of just one (GPIO2). The above methods can mostly be utilized in hobby projects, due to dangers of frying the pins. For more serious or commercial projects, it will be better to select another more appropriate ​module. ​+I have presented ways to exploit an ESP-01 as much as possible and utilize all four available GPIO pins. The above methods can mostly be utilized in hobby projects, due to dangers of frying the pins. For more serious or commercial projects, it will be better to select another more suitable ​module. ​
  
 Have fun... ​ Have fun... ​
 +
 +~~DISQUS~~
esp8266/how-to-use-all-esp01-gpio-pins.1570361617.txt.gz · Last modified: 2019/10/06 14:33 by Ilias Iliopoulos