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 IDEFunctions are the instructions which are used for performing computation and controlling Arduino board. Functions are divided into the following groups:
Digital I/O FunctionsDigital pins can be used for digital input, digital output and analog output in the form of PWM.
Syntax: pinMode(pin,mode)
Parameters:
pin: number of the pin want to use.
mode: INPUT, OUTPUT, INPUT_PULLUP, INPUT_PULLDOWN
Return: Nothing
Syntax: digitalWrite(pin,value)
Parameters:
pin: number of the pin
value: HIGH or LOW
Return: Nothing
Syntax: digitalRead(pin)
Parameters:
pin: number of the pin want to read
Return: HIGH or LOW
Analog I/O FunctionsThe 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.
Syntax: analogWrite(pin,value)
Parameters:
pin: label of the pin to write.
value: duty cycle value between 0 to 1023.
Return: Nothing
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 FunctionsDelay 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.
Syntax: delay(ms)
Parameters:
ms: time in millisecond to pause the program
Return: Nothing
Syntax: delayMicroseconds(us)
Parameters:
us: time in the microseconds to pause the program
Return: Nothing
Syntax: time = micros( )
Parameters: None
Return: Time in microseconds
Syntax: time = millis( )
Parameters: None
Return: Time in milliseconds
Advanced I/O FunctionsSyntax:
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
Syntax: noTone(pin)
Parameters:
pin: the number of pin
Return: Nothing
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
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
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
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 FunctionSyntax: interrupts ( )
Parameters: None
Return: Nothing
Syntax: noInterrupts( )
Parameters: None
Return: Nothing
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
Syntax: detachInterrupt (digitalPinToInterrupt (pin))
Parameters:
interrupt: the label of interrupt.
pin: the label of pin.
Return: Nothing
Communication FunctionSerial 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.
Syntax: Serial.begin (speed)
Parameters:
speed: Baud Rate is also known as bits per second.
Return: Nothing.
Syntax: Serial.end ( )
Parameters: None
Return: Nothing.
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 IDEAs 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 GPIO Label on NodeMCU silkscreen ArduinoIDE pins Index
Available modes
GPIO 0 D3 0 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 1 TX 1 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 2 D4 2 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 3 RX 3 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 4 D2 14 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 5 D1 15 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 12 D6 12 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 13 D7 13 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 14 D5 14 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 15 D8 15 OUTPUT, INPUT, INPUT_PULLUP or INTERRUPT GPIO 16 D0 16 OUTPUT, INPUT, INPUT_PULLDOWN ADC0 A0 A0 INPUT ESP8266 Libraries in Arduino IDEThe 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 LibrariesThere 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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4