ESP-7 Pin outs in arduino code

Hopefully some one can help me with this as i'm tearing my hair out as quote new to this!!

I have an esp-1 and i can use the 2 gpio on it, one as an ouput for a relay and the other being watched by the bounce command that when it goes low it publishes to a mqtt server which in turn then sends back to the subscription on the same esp altering the realy, if that makes sense

i would like to run 2 relays off one esp so i have a esp-7 on a break out board but for the love of god can i work out what pins equate to in relative terms on the unit.

at the moment in my code i have a relay plugged into to what it says is gpio13 and the switch into gpio3 but in my code i have relay set as pin 13 and switch as pin 4 and it works!!!! (i think its pin 3 on board and 4 in code, might be gpio4 on board and 3 in code, i'm at work at the min so cant check but defo the one of those way round.)

but when i use say pin 15 and 5 for another relay and swtich nothing works. soooo my question is what number would you place in the code to equate to each gpio on the board seeing as the button one makes no sense!!!

thanks a lot and i'll try to explain better if there is something that makes no sense.

The picture in this link may help ESP-07 (or ESP-12) with NodeMCU going into deep sleep -
GPIO5 equates to digital pin 5 etc. in the Arduino IDE

How are you using your ESP07? With the arduino programming environment for esp8266 or as a Serial to wifi connection using AT commands / a library?

hi both.

6v6gt. from looking at the picture and the code you would expect that gpio4 is 4 in the code but on my code it’s not for some reason. but pin 13 is

J-M-L with the arduino and uploading the code direct to the unit. I’ve found the original code on the net that i’ve altered so i’ll past that in the next window so you can see what it does


It connects to an MQTT server then:

  • on 0 switches off relay

  • on 1 switches on relay

  • on 2 switches the state of the relay

  • sends 0 on off relay

  • sends 1 on on relay

It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the ‘mqtt_reconnect_nonblocking’ example for how to
achieve the same result without blocking the main loop.

The current state is stored in EEPROM and restored on bootup


#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <EEPROM.h>

const char* ssid = “YourSSID”;
const char* password = “YourPASS”;
const char* mqtt_server = “YourMQTTBroker’sIP”;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const char* outTopic = “Sonoff1out”;
const char* inTopic = “Sonoff1in”;

int relay_pin = 12;
int button_pin = 0;
bool relayState = LOW;

// Instantiate a Bounce object :
Bounce debouncer = Bounce();

void setup_wifi() {

// We start by connecting to a WiFi network
Serial.print("Connecting to ");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
for(int i = 0; i<500; i++){
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
Serial.println(“WiFi connected”);
Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived [”);
Serial.print("] ");
for (int i = 0; i < length; i++) {

  • }*

  • Serial.println();*

  • // Switch on the LED if an 1 was received as first character*

  • if ((char)payload[0] == ‘0’) {*

  • digitalWrite(relay_pin, LOW); // Turn the LED on (Note that LOW is the voltage level*

  • Serial.println(“relay_pin → LOW”);*

  • relayState = LOW;*

  • EEPROM.write(0, relayState); // Write state to EEPROM*

  • EEPROM.commit();*

  • } else if ((char)payload[0] == ‘1’) {*

  • digitalWrite(relay_pin, HIGH); // Turn the LED off by making the voltage HIGH*

  • Serial.println(“relay_pin → HIGH”);*

  • relayState = HIGH;*

  • EEPROM.write(0, relayState); // Write state to EEPROM*

  • EEPROM.commit();*

  • } else if ((char)payload[0] == ‘2’) {*

  • relayState = !relayState;*

  • digitalWrite(relay_pin, relayState); // Turn the LED off by making the voltage HIGH*

  • Serial.print("relay_pin → switched to ");*

  • Serial.println(relayState);*

  • EEPROM.write(0, relayState); // Write state to EEPROM*

  • EEPROM.commit();*

  • }*
    void reconnect() {

  • // Loop until we’re reconnected*

  • while (!client.connected()) {*

  • Serial.print(“Attempting MQTT connection…”);*

  • // Attempt to connect*

  • if (client.connect(“ESP8266Client”)) {*

  • Serial.println(“connected”);*

  • // Once connected, publish an announcement…*

  • client.publish(outTopic, “Sonoff1 booted”);*

  • // … and resubscribe*

  • client.subscribe(inTopic);*

  • } else {*

  • Serial.print(“failed, rc=”);*

  • Serial.print(client.state());*

  • Serial.println(" try again in 5 seconds");*

  • // Wait 5 seconds before retrying*

  • for(int i = 0; i<5000; i++){*

  • extButton();*

  • delay(1);*

  • }*

  • }*

  • }*
    void extButton() {

  • debouncer.update();*

  • // Call code if Bounce fell (transition from HIGH to LOW) :*

  • if ( debouncer.fell() ) {*

  • Serial.println(“Debouncer fell”);*

  • // Toggle relay state :*

  • relayState = !relayState;*

  • digitalWrite(relay_pin,relayState);*

  • EEPROM.write(0, relayState); // Write state to EEPROM*

  • if (relayState == 1){*

  • client.publish(outTopic, “1”);*

  • }*

  • else if (relayState == 0){*

  • client.publish(outTopic, “0”);*

  • }*

  • }*
    void setup() {

  • EEPROM.begin(512); // Begin eeprom to store on/off state*

  • pinMode(relay_pin, OUTPUT); // Initialize the relay pin as an output*

  • pinMode(button_pin, INPUT); // Initialize the relay pin as an output*

  • pinMode(13, OUTPUT);*

  • relayState =;*

  • digitalWrite(relay_pin,relayState);*

  • debouncer.attach(button_pin); // Use the bounce2 library to debounce the built in button*

  • debouncer.interval(50); // Input must be low for 50 ms*

  • digitalWrite(13, LOW); // Blink to indicate setup*

  • delay(500);*

  • digitalWrite(13, HIGH);*

  • delay(500);*

  • Serial.begin(115200);*

  • setup_wifi(); // Connect to wifi*

  • client.setServer(mqtt_server, 1883);*

  • client.setCallback(callback);*
    void loop() {

  • if (!client.connected()) {*

  • reconnect();*

  • }*

  • client.loop();*

  • extButton();*

Any one got any ideas from the above code?

just be careful GPIO15 is special as are GPIO0 and GPIO2

see GPIO pins

Odd. Clear is that you are successfully using the Arduino IDE to load this code. It is obviously C/C++ and not LUA.
When you load software, what board have you defined in the Arduino IDE ? Generic ESP8266 or something else ?

generic esp board.. but i think i have sussed it!!

having looked at the board and doing a continuity test from the pins on the break out board and the pins on the chip it would seem that which ever donkey labelled the break out board was unable to get it right. i think that gpio4 on the board goes to 5 and a couple of others seem wrong which was throwing me out. i'm going to alter some stuff on the code and see if it then works on the wrong pins!!!

thanks for your help guys i'll report back in a bit :slight_smile:

Sorted. it was a mis labeled breakout board.

manged to find out another set of pins so can have 2 buttons and 2 relays of the single esp-7

Cool - have fun now!