Thursday, April 26, 2018

More I/O pins for your ESP-01


For some time something was nagging me.
I have always learned that the ESP-01 has only 2 I/O pins being GPIO-0 and GPIO-2.

The module has some more pins but they have dedicated functions. There is the Reset pin, the VCC and Ground pins and the CH-PD pin which has to be connected to VCC to let the chip function. Next to that there is the RX and TX pins but they are used for programming the chip.

Ehh are they ???

Well yes they are, but only in certain circumstances.

Lately I have been playing with ESP-Basic which is really great. It is a very usefull programming environment for the ESP8266 with loads of features and it gets the devellopment job done in a real short time. Really worth checking out.

Now ESP-Basic has one very convenient feature: it programs the ESP-8266 over the air (OTA). That means that when you have uploaded ESP-Basic to the ESP chip no more attachment is needed to your computer to get your Basic program into the chip. You only have to power it and then you can program it over wifi from your computer, notebook, tablet or even your phone. So there is no need anymore to carry around a notebook with a programming environment. The Basic interpreter is in the chip and your web-browser is your programming screen.



My usual devellopping setup is a Node-MCU which has many I/O pins. And I read on the ESP-Basic forum that it is possible to use the TX and RX lines as I/O lines. There: you might have learned something new here. They can be used as D9 and D10 and labeled GPIO-1 and GPIO-3.



Hey wait a minute.
The ESP-01 also has a TX and RX line. So would it be possible to use those as I/O lines.

The answer is YES but with some restrictions.

Above is the ESP-01 pin-out. You can see GPIO-0 and GPIO-2 and the TX and RX lines.

So let's make a simple setup on a breadboard.


The leds are connected through a 220 ohm resistor for delimiting power consumption.
GPIO-0 (yellow wire) is connected to the first led.
GPIO-1 (white wire) is the TX line connected to the second led
GPIO-2 (brown wire) is connected to the third led
GPIO-3 (green wire) is the RX line and connected to the fourth led.

It does not work !!!

Off course it did not work !!
The GND line is connected to the leds and supplies a minimal charge to the GPIO's. Therefore the ESP will not boot anymore when powered up. Remember when GPIO-0 is put to ground at start-up the ESP goes into programming mode. And we definitely do not want that. So we need the following setup.



As you can see the GND is interrupted by a switch. So we set the switch in the off-position at power-up time and after that switch it to the GND position.



And hell yeah everything works as expected.


 
I even tested it running on batteries.



timer 1000, [blink]
button "off", [gooff]
wait

[gooff]
end

[blink]
for i = 0 to 3
  io(po, i, 1)  
  delay 1000
  io(po, i, 0)  
next i
wait

To test this setup for yourself use the above small basic program.
The program is straightforward. Every second it calls the blink routine which blinks GPIO-0 to GPIO-3 by using a for-next loop. 




It has been running on my desk for many hours without crashing. So it definitely works.

Another test


button "0" , [pin0]
button "1" , [pin1]
button "2" , [pin2]
button "3" , [pin3]
button "alles off", [alloff]
button "off", [gooff]
wait

[gooff]
end

[pin0]
io(po, 0, 1)
wait

[pin1]
io(po, 1, 1)
wait

[pin2]
io(po, 2, 1)
wait

[pin3]
io(po, 3, 1)
[alloff]
for i = 0 to 3
  io(po, i, 0)  
next i
wait

Just to make sure that using the TX and RX lines as an output line would not interfere with normal Wifi operation of the ESP I wrote a simple Basic program that makes a web-page with buttons on it.


Pressing one of the buttons turns on the accompaning led. 


And this also worked flawlessly. As you can see. The picture shows that I turned on GPIO-1 and GPIO-3 over wifi. So the wifi communication does not interfere with the GPIO lines.

Any restrictions.

Well actually yes.
You can not use the extra pins as I/O pins. You can only use them as OUTPUT pins.
I tried using them as an input and they would just crash the ESP.

That is actually predictable as the ESP will look at the TX and RX lines for incoming communication and you will still need them when re-flashing the ESP for C, Lua, Python or other languages.

Not only Restrictions but Advantages

You can use GPIO-0 and GPIO-2 as input and GPIO-1 and GPIO-3 as output. This setup just gives you the possibility to have 2 inputs AND 2 outputs or 4 outputs on this tiny chip.

Summerising the setup:
- It will only work when programming OTA (over then air)
- GPIO-1 and GPIO-3 are only outputs
- Make sure there is no GND connection at booting

There are still some things to check:
- will this work with Python or Forth
- will this work when programmed with an FTDI (programming board) and then detached and operating as a stand-alone setup

So again something new to experiment with.

Till next time
have fun

Luc Volders