Reference for ESP8266 using Arduino IDE

Before getting started with the hardware this tutorial is used to understand the fundamentals of Arduino Programming Language and Language reference for ESP8266 for Arduino IDE. If you wonder how to install Arduino IDE for ESP8266 feel free to visit our tutorial on that here.

Keep in mind that we are using ESP8266 it is a 3.3 volts microcontroller, so anywhere we refer HIGH means 3.3 volts and LOW means 0 volts or ground. ESP8266 is not 5 volts tolerant. Any voltage above 3.6 volts is dangerous to the microcontroller. Also, we can drive the only 12mA of current from every pin on the microcontroller. There are three main parts for Arduino Programming Language which are functions, variables, and structure.

Functions for ESP8266 in Arduino IDE

Functions are the instructions which are used for performing computation and controlling Arduino board.  Functions are divided into the following groups:

Digital I/O Functions

Digital pins can be used for digital input, digital output and analog output in the form of PWM.

  • pinMode ( ): This function is used to describe the behavior of the pin as an input or output.

Syntax: pinMode(pin,mode)

Parameters:

pin: number of the pin want to use.

mode: INPUT, OUTPUT, INPUT_PULLUP, INPUT_PULLDOWN

Return: Nothing

  • digitalWrite ( ): This function writes the value of HIGH or LOW to a predefined digital pin.

Syntax: digitalWrite(pin,value)

Parameters:

pin: number of the pin

value: HIGH or LOW

Return: Nothing

  • digitalRead ( ): This function reads the status of the pin whether it is HIGH or LOW.

Syntax: digitalRead(pin)

Parameters:

pin: number of the pin want to read

Return: HIGH or LOW

Analog I/O Functions

The generic ESP8266 does not have any analog input pin while all other development boards based around ESP-12 module have only one analog input pin. The following functions are used analog input and output pins.

  • analogWrite ( ): This function is used to generate the square wave of a given value of duty cycle.

Syntax: analogWrite(pin,value)

Parameters:

pin: label of the pin to write.

value: duty cycle value between 0 to 1023.

Return: Nothing

  • analogRead ( ): This function is used to read analog pin

Syntax: analogRead(pin)

Parameters:

pin: label of the analog pin to read for esp8266 it is A0

Return: Return a value between 0 to 1023

Time Functions

Delay functions are used to pause the execution of the program for a given duration of time. The Purpose of the delay is to get a response from the Wi-Fi or some other server. Delays allow the Wi-Fi and TCP/IP task to execute. While timing functions are used to measure the time passed the program running on the chip after it boots up.

  • delay ( ): This function is used to pauses the flow of program from the defined time in milliseconds

Syntax: delay(ms)

Parameters:

ms: time in millisecond to pause the program

Return: Nothing

  • delayMicroseconds ( ): This function is used to pauses the flow of program from the defined time in the microseconds

Syntax: delayMicroseconds(us)

Parameters:

us: time in the microseconds to pause the program

Return: Nothing

  • micros ( ): Return the time elapsed since the program is running in microsecond

Syntax: time = micros( )

Parameters: None

Return: Time in microseconds

  • millis ( ): Return the time elapsed since the program is running in millisecond

Syntax: time = millis( )

Parameters: None

Return: Time in milliseconds

Advanced I/O Functions

  • tone ( ): This function is used to generate a PWM signal with 50% duty on a described pin.

Syntax:

tone(pin,frequency)

tone(pin,frequency,duration)

Parameters:

pin: pin number to generate tone.

frequency: frequency of tone in Hertz.

duration: duration of tone in milliseconds

Return: Nothing

  • noTone ( ): This function is used to stop the generation of the tone

Syntax: noTone(pin)

Parameters:

pin: the number of pin

Return: Nothing

  • shiftIn ( ): This function is a software implementation used to shift in a byte of data one bit at a time.

Syntax: byte incoming = shiftIn(dataPin, clockPin, bitOrder)

Parameters:

dataPin: input pin

clockPin: pin to toggle to signal a read from input pin.

bitOrder: order to shift LSBFIRST or MSBFIRST

Return: Value read in byte

  • shiftOut ( ): This function is a software implementation used to shift out a byte of data one bit at a time.

Syntax: shiftOut(dataPin, clockPin, bitOrder, value)

Parameters:

dataPin: output pin

clockPin: pin to toggle when dataPin set to correct value.

bitOrder: order to shift LSBFIRST or MSBFIRST

value: data to shift out from byte.

Return: Nothing

  • pulseIn ( ): This function is used to read pulse HIGH or LOW on a pin. Read a pulse from 10 microseconds to 3 minutes duration long.

Syntax:

pulseIn(pin,value)

pulseIn(pin, value, timeout)

Parameters:

pin: label of the pin to read pulse

value: status of the pulse to read HIGH or LOW

timeout: time in microseconds to wait to start pulse

Return: Duration of pulse in microseconds

  • pulseInLong ( ): This function is alternative of pulseIn( ) which is used to handle long pulses effectively

Syntax:

pulseInLong(pin,value)

pulseInLong(pin, value, timeout)

Parameters:

pin: label of pin to read pulse

value: status of the pulse to read HIGH or LOW

timeout: time in microseconds to wait to start pulse

Return: Duration of pulse in microseconds

Interrupts Function

  • interrupts ( ): This function is used to perform some important task by disrupting the flow of the execution of the sketch. This function is also used to re-enables interrupts after disabled by nointerrupt function.

Syntax: interrupts ( )        

Parameters: None

Return: Nothing

  • noInterrupts ( ): This function is used to disable the interrupt function that allows performing the important task by disrupting the flow of the execution of the sketch. Interrupts are enabled by default.

Syntax: noInterrupts( )

Parameters: None

Return: Nothing

  • attachInterrupt ( ): This is a function is used to use digital pins with interrupts.

Syntax: attachInterrupt (digitalPinToInterrupt (pin), ISR, mode) 

Parameters:

interrupt: the label of interrupt.

pin: the label of pin.

ISR: Interrupt Service Routine request interrupt to hardware.

mode: LOW, HIGH, CHANGE, RISING, FALLING.

Return: Nothing

  • detachInterrupt ( ): This function is used to disables the external given interrupts

Syntax: detachInterrupt (digitalPinToInterrupt (pin))

Parameters:

interrupt: the label of interrupt.

pin: the label of pin.

Return: Nothing

Communication Function

Serial communication with ESP8266 works similar to Arduino. UART 0 is used GPIO1 as TX pin and GPIO3 as RX pin for Serial communication. The following function will help to start, end and to set the baud rate.

  • begin ( ): Enables serial communication and sets the baud rate.

Syntax: Serial.begin (speed)

Parameters:

speed: Baud Rate is also known as bits per second.

Return: Nothing.

  • end ( ): Disables the serial communication and allow to use RX and TX as GPIOs.

Syntax: Serial.end ( )

Parameters: None

Return: Nothing.

  • print ( ): This function is used to print data to the serial port in ASCII text.

Syntax:

Serial.print(val)

Serial.print(val, format)

Parameters:

val: data to print.

Return: The numbers of bytes which have printed.

How to reference pins in Arduino IDE

Reference ESP8266 using Arduino IDE

As you already know from our previous tutorial that pins label on the silkscreen of NodeMcu, ESP8266 and index pin for Arduino IDE differs from each other so the following table and picture will help you to assign the general purpose input/output pin of ESP-12 module in your sketch in Arduino IDE.

ESP8266 GPIOLabel on NodeMCU silkscreenArduino

IDE pins Index

 

Available modes

GPIO 0D30OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 1TX1OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 2D42OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 3RX3OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 4D214OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 5D115OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 12D612OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 13D713OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 14D514OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 15D815OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT
GPIO 16D016OUTPUT, INPUT, INPUT_PULLDOWN
ADC0A0A0INPUT

 ESP8266 Libraries in Arduino IDE

The collection of procedures, subprograms, and modules that used frequently can be saved as a Library files. Library file particular are routines and function that used in sketches frequently so we don’t have to re-invent the wheel. Libraries are quite helpful to get starts with any new subject like Firebase, ThingSpeak, etc.

There are some libraries that are preinstalled in Arduino IDE some other libraries objective to the user can be install afterwards. The following are the list of libraries that will help you in beginner’s projects, home automation, robotics, etc.

Pre-Installed Libraries

  • Bridge
  • Ethernet
  • EEPROM
  • GSM
  • Keyboard
  • LiquidCrystal
  • Mouse
  • Robot Control
  • Robot Motor
  • SD
  • Servo
  • Stepper
  • TFT
  • WiFi

Libraries available to install manually

There is a total of 2233 libraries are registered in the Arduino Library Manager and increasing daily. Here are a few important libraries that used commonly:

  • Adafruit ESP8266: a collection of example codes for ESP8266 chipset.
  • ArduinoJson: Easy to used JSON examples.
  • ArduinoOTA: To program the ESP8266 and other Wifi microcontrollers over the air.
  • Blynk: To interface microcontroller with Blynk Application.
  • DHT: Sample codes for DHT11 and DHT22 sensor.
  • ESP8266: Collection of basic codes for ESP8266 microcontroller.
  • ESP8266SDUpdater: To interface SD card with ESP8266.
  • ESP8266WebServer: To create a web server with esp8266.
  • EspSoftwareSerial: Arduino software serial implementation for ESP8266/ESP32.
  • FirebaseArduino: To interface ESP devices with Google Firebase.
  • OLED: Example code to interface OLED displays.
  • PubSubClient: MQTT messaging library for ESP8266.
  • Ping: To generate ping to other remote devices using ESP8266.
  • RFID: To interface RFID reader with ESP8266.
  • SD (esp8266): To interface SD card module with ESP8266.
  • Servo (esp8266): To interface Servo with ESP8266.
  • SPI: To use Serial Peripheral Interface using ESP8266.
  • ThingSpeak: To interface Wifi devices with ThingSpeak server to send and read data.
  • WiFi Picker: To provide a list of Wifi access points.
  • WiFi101OTA: To program the ESP devices using Wifi.
  • YouTubeApi: Library to use YouTube APIs.

1 thought on “Reference for ESP8266 using Arduino IDE”

Leave a Comment