You are on page 1of 8

뾖႞耀ᑄ搡㷴撞鯰 〸㌸ .

 䈀
personal projects
Projects MariaMole Blog About Contact

Connecting your Arduino to WiFi via


an ESP-8266 module
Like 29

ESP-8266 is an easy and low-cost alternative to the expensive


Arduino WiFi shields. While those shields can cost over USD 50,
you can find an ESP module for less that USD 3 at ebay
There are several ESP module models around there. All of them
are based on the same IC. This article is based on the ESP-01,
which is, probably, the most common model.
All ESP-8266 modules are programable. You can use the default
factory firmware or you can write your own firmware and upload
it to your module. My module came with the firmware version
0018000902-AI03.
The default firmware provides a serial communication that you
can use to send AT commands to your module, just like in the old
telephone/modem days. These commands provide everything you
need to connect to an WiFi router and send/receive data.

Connecting the ESP to an Arduino
The logic connections between Arduino and ESP are very simple:
ESP-Rx goes to Arduino Tx, ESP-Tx goes to Arduino Rx. However,
all ESP-8266 run on 3.3V, while Arduino pins run on 5V. Before
connecting them, you shall provide a way to adapt these voltages,
or you could damage your ESP.
I'm using an CD4050 to adjust the voltage between the Tx/Rx
pins. Check it:
 
Note that I'm using the CD4050 only to adapt the Arduino-Tx to
ESP-Rx. There's no need to adjust the ESP-Tx to Arduino-Rx,
since the Arduino port can handle 3.3V.
Also note that the Arduino 3.3V regulated output (50 mA max)
can't drive the current requested by the ESP (Up to 200 mA).
Some tutorials on the Internet use the Arduino 3.3V to power the
ESP, but it didn't worked for me. I'm using an external power
supply (An old 5V Android charger).
I'm using a diode and resistor (17 kOhms, but any large value will
work) to decrease my 5V power supply (Here depicted as a
battery pack). Depending on the quality of your power supply,
you can use one or two diodes. As mine wasn't a good one, a
single diode was enough to get 3.7V when the circuit was
powered on.
I made a PCB with this circuit. You'll find the link at the end of
this post

Important:
Never power your ESP with 5V. You'll destroy it. The same
goes for any logic input (In this case, the Rx pin)
If your ESP receives less than 3.3V, you'll end up with a lot of
transmission errors (You'll see a lot of garbage on your serial
communication)
When using an external power, do not forget of
interconnecting both Arduino-GND and power supply GND, to
keep the voltage reference. If you don't do that, you'll end up
with a lot of transmission errors, again.
ESP­8266­01 terminals
The terminals for this ESP models are depicted here:

VCC shall be connected to the 3.3V power supply


GPIO0 and GPIO2 are general purpose digital ports. GPIO0
also controls the module mode (programming or normal
operation). In our case (normal operation), it shall be
connected to 3.3V (high). GPIO2 is not used in this example,
so I put it on 3.3V to simplify the connections
CH_PD: Chip enable. Keep it on high (3.3V) for normal
operation
RST: Reset. Keep it on high (3.3V) for normal operation. Put it
on 0V to reset the chip.
Tx: Goes to Arduino Rx
Rx: Goes to Arduino Tx (But needs a voltage adjusting)
GND is ground

You can see that, in this example, all pins go to VCC (3.3V),
except GND, Rx and Tx.
Note that the module also has two-color led (or two separate
leds, depending on your vendor)
Red led indicates the board is powered
Blue led indicates module activity (Initializing, Rx/Tx, etc)

Test code
In order to test your ESP, you'll need two serial ports:
One dedicated serial port, connected between Arduino and
ESP, where Arduino will send AT commands to ESP.
A second serial port, connected between Arduino and the
computer, where you can type your AT commands.

So, the best Arduino model for this task is Mega, since it provides
up to 4 serial ports. You can still try other models, using the
SoftSerial lib, but this library has some speed limitations, and it
may not work with some ESP models.
You can test your ESP connection with the following simple code:
You can test your ESP connection with the following simple code:

void setup()
{
    Serial.begin(9600);
    Serial1.begin(9600);
}
void loop()
{
    while (Serial1.available()) {
        Serial.write(Serial1.read());
    }
    while (Serial.available()) {
        Serial1.write(Serial.read());
    }
}

This code simply relays data from both Arduino serial. It gets the
AT commands you type at your computer and sends them directly
to your ESP. It also gets ESP return data and sends to your
computer.
 
Once you upload this code to your Arduino, you'll see this on
your terminal:

Note that, as there are some differences among modules from


different manufacturers, the information available at Internet is,

several times, contradictory. If your module doesn't work, try


several times, contradictory. If your module doesn't work, try
other different baud rates
The "ready" text on your terminal means that your ESP is ready to
receive your AT commands. You can get a list of them at the
bottom of this page.
However, you'd rather use a high level library, since using AT
commands may be tricky. A good one is also available at the
bottom links. An example of what you can achieve with this
library:

The code for this example can be found at the bottom


(Portuguese tutorial)

Links
My projects with ESP826 at github (Includes all circuits shown
at this page)
List of ESP AT commands
ESP-8266 Library for Arduino
A great tutorial, in Portuguese
CD45050 Datasheet
A very good reference doc for ESP8266 (PDF)

13 Comments dalpix.com 
1  Login

  Recommend  4 ⤤ Share Sort by Best

Join the discussion…
Join the discussion…

Ujjwal Kumar • 2 months ago
i have done all connection but using arduino uno. But there is an error
while loading program if i connect Rx to Tx and Tx to Rx.But if i have
done vice versa ,program get uploaded but my led not blinked this
time.What is the problem ?
I have used github library for esp8266,but i am confused which library
i select generic esp,nodemcu esp and many more?? i havve esp8266
which i bought for $3.
If I am using serial monitor to send “AT ” directly to essp8266,it is not
responding.What is the probblem????i have done all connection but
using arduino uno. But there is an error while loading program if i
connect Rx to Tx and Tx to Rx.But if i have done vice versa ,program
get uploaded but my led not blinked this time.What is the problem ?
I have used github library for esp8266,but i am confused which library
i select generic esp,nodemcu esp and many more?? i havve esp8266
which i bought for $3.
If I am using serial monitor to send “AT ” directly to essp8266,it is not
responding.What is the probblem????
△   ▽ • Reply • Share ›

Maria Smith • 4 months ago
When I uploaded the code to the Arduino, send an AT command, THe
Rx led on Arduino blink on my send button click on computer, but
nothing shown on the Serial Monitor (I am using Arduino IDE) Can you
please let me know what is wrong with my first attempt to Arduino?

Thank you.
△   ▽ • Reply • Share ›

Abbas • 5 months ago
How to send data back from arduino to esp 
△   ▽ • Reply • Share ›

shadab abedin • 5 months ago
noting is printed in response like no FM verion printed no ip printed in
response..

setup begin

FW Version:

to station + softap ok

Join AP success

IP:

single ok

setup end

create tcp ok

release tcp ok
release tcp ok

create tcp ok
△   ▽ • Reply • Share ›

erk 7020 • 6 months ago
I'm compilation error Serial1 was not declared in this scope help??
△   ▽ • Reply • Share ›

Alex > erk 7020 • 6 months ago
This code is for Arduino Mega, which as a lot of Serial ports.
On Uno and other simpler Arduinos, you just have a single
Serial port, so you don't have "Serial1", only "Serial" on these
devices. An alternative would be using SoftSerial, which
extends the number of serial ports on your device by using
other pins
△   ▽ • Reply • Share ›

erk 7020 > Alex • 6 months ago
thanks
△   ▽ • Reply • Share ›

Nawfal Raed • 9 months ago
can we connect arduino UNO to wifi ESP ??
△   ▽ • Reply • Share ›

Rishi Rao > Nawfal Raed • 7 months ago
Yes
△   ▽ • Reply • Share ›

Euclides Rezende • a year ago
Great Alex !!! Does the colorblind also make electronic assemblies?
He did not yield to read the resistor value.

The pin 4050 (entry and exit are not reversed? pin 3 to 2).

­­­ Portuguese ­­­

Grande Alex!!! Será que os Daltônicos tambem fazem montagens
eletrônicas? Não deu pra ler o valor do resistor.

Os pinos do 4050 (entrada e saída não estão invertidos? Pino 3 para
o 2).
△   ▽ • Reply • Share ›

Alex > Euclides Rezende • a year ago
Os pinos do 4050 estavam invertidos mesmo. Corrigi na
imagem. Valeu.
A entrada é no pino 3, a saída é no pino 2
△   ▽ • Reply • Share ›

Euclides Rezende > Alex • a year ago
Datasheets are the same politicians; it's always good
suspect, suspicious ...
­­­ Portuguese ­­­

Datasheets são iguais políticos; sempre é bom
desconfiar, desconfiando...
△   ▽ • Reply • Share ›

Alex > Euclides Rezende • a year ago
17 k pro resistor, mas qualquer valor grande deve funcionar. É
só pra dividir a tensão da fonte.
△   ▽ • Reply • Share ›

YOU MAY ALSO BE INTERESTED IN THE FOLLOWING


TEXTS
Internet das Coisas: Monitor de Horta
MariaMole with support for Leonardo
OpenSource Automatic Cat Fountain
MariaMole - Tutorial
Computer Vision on Edison with OpenCV

© alexporto.com 2016 |
Arclite theme by digitalnature | powered by WordPress
TOP