In this tutorial you’re going to learn how to send data from an Arduino to the ESP8266 via serial communication.
Before you continue reading this project, please complete the following tutorials:
- How to get started with the ESP8266
- How to flash your ESP8266 with NodeMCU
- How to Level Shift 5V to 3.3V
If you like the ESP WiFi module and you want to build more projects you can download my eBook called “Home Automation using ESP8266” here. Let’s get started!
Watch the video demonstration below
Parts List
Here’s the hardware that you need to complete this tutorial example:
- 1x ESP8266 – read Best ESP8266 Wi-Fi Development Boards
- Arduino UNO – read Best Arduino Starter Kits
- 1x FTDI programmer
- 1x Breadboard
- 1x LED
- 1x 220 Ohm Resistor
- 1x 1K Ohm Resistor
- 1x 2k Ohm Resistor
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Uploading Your Arduino Sketch
The Arduino sketch for this tutorial is very simple.
You begin a serial communication in the setup() function at a baud rate of 9600. Then in the loop() function it prints “HI!” continuously every 1 second (that message will be received by your ESP later).
Copy the sketch below to your Arduino IDE and upload it to your Arduino board.
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
// Send Data From Arduino to ESP8266 via Serial @ baud rate 9600
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("HI!");
delay(1000);
}
Now if you open your Arduino serial monitor at a baud rate of 9600, you’ll see a message appearing in your window saying “HI!” every 1 second.
Schematics (3.3V FTDI Programmer)
Having your ESP8266 flashed with NodeMCU, follow the next schematics to establish a serial communication between your FTDI programmer and your ESP8266 to upload some code.
Downloading ESPlorer IDE
I recommend using the ESPlorer IDE which is a program created by 4refr0nt to create and save Lua files into your ESP8266. Follow these instructions to download and install ESPlorer:
- Click here to download ESPlorer
- Unzip that folder
- Go to the main folder
- Run ESPlorer.jar
- Open the ESPlorer (as shown in the Figure below).
Writing Your ESP8266 Script
Copy and paste the code below into ESPlorer IDE window.
-- Rui Santos
-- Complete project details at https://randomnerdtutorials.com
ledOn = 0
pin=4
gpio.mode(pin,gpio.OUTPUT)
uart.on("data", 3,
function(data)
print("Received from Arduino:", data)
if data=="HI!" then
if ledOn==0 then
ledOn = 1
gpio.write(pin,gpio.HIGH)
print("LED On")
else
ledOn = 0
gpio.write(pin,gpio.LOW)
print("LED Off")
end
end
end, 0)
Summary: The ESP is configured to listen to serial communications. Every time that receives the string “HI!” at a baud rate of 9600, it will turn the GPIO 2 on or off.
Uploading Your Script
When you open the ESPlorer IDE you should see a window similar to the preceding Figure, follow these instructions to send commands to your ESP8266:
- Connect your FTDI programmer to your computer
- Set bad raute as 9600
- Select your FTDI programmer port (COM3, for example)
- Press Open/Close
- Select NodeMCU+MicroPtyhon tab
- Copy the your Lua script into ESPlorer
Then you simply click the button Save to ESP and save your file with the name “init.lua”. Everything that you need to worry about or change is highlighted in red box in the following Figure.
Final Circuit
Follow the next schematics to complete this tutorial.
Note: I’m using a voltage divider to shift the TX signal of the Arduino from 5V to 3.3V. This works well for slow baud rates, but it might not work at faster baud rates. Read this blog post for more information about lowering the voltage of signals.
Demonstration
Now your LED should be blinking every one second. This means that your Arduino is sending the string “HI” and your ESP is receiving that data. Watch the video at the beginning of this post for a live demonstration.
Now instead of sending a string saying just “HI!”, you can attach sensors to your Arduino and send that data to your ESP instead. Later you can build a web server that displays that data.
Read Next…
You might also find interesting trying one of these tutorials:
Do you have any questions? Leave a comment down below!
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.
Olá,
é possível “colher” dados de uma variável, no Arduino, e mostrá-los em dois displays de 7 segmentos?
Sim, é possível fazer isso
Thank you very much for another great tutorial.
Yves Arbour
You’re welcome! Thank you for reading my projects!
Would love to see the ESP8266 configured like a Bluetooth module in Serial mode so I can send serial messages to/from my smart phone to a microcontroller (I prefer the Propeller over the Arduino, but the process would be the same).
As you said that’s totally possible to do. But why would you need a bluetooth module? you can send the same messages from your smartphone to your ESP only using the ESP8266
Could you please help me with ESPlorer IDE, I have downloaded it, done all the unzipping etc but have no idea how to run it. I am stuck at step 5 in your instructions, Open the ESPlorer (as shown in the Figure below). How do I “Open the ESPlorer”? Could you please point me in the right direction?
Best Regards
Melvyn
Are you using Windows? Open the “ESPlorer” file that has the type windows batch file.
Do you have java installed in your computer? Make sure you download and install java
Thanks Rui, that solved it, I have a lot to learn.
Melvyn
Awesome Melvyn!
I’m happy to hear that
I’m pretty much in the same boat.. But, running both Windows & Linux (preferably linux..)
Hello Rui !
Your tutorials are very helpful and easy to understand.
I was wondering why do you use ESPlorer IDE instead of Arduino IDE with ESP plug in in your latest tutorials ?
I didn’t try ESPlorer IDE yet but it seems to me that Arduino IDE is more simple to use.
Best regards !
Davor
Hi Davor,
Thank you for asking. I prefer to program the ESP using the NodeMCU firmware and writing Lua code.
But both ways should work just fine and are very easy to use.
Thanks for asking!
Rui
Mr. Rui. Can you help me do this using arduino ide instead of esplorer ide? i badly need it for a school project. 🙂
Yes, It is possible… But I don’t have a working example for the Arduino IDE…
Dear Rui,
Have you used Code::Blocks IDE to code ESP8266 in C?
Is there any definite pointer document to guide on the same?
Regards,
–Utpal
Good evening Rui,
Is there any way by which the uart.on() does not go indefinitely?
The data to be received on serial is a number.
I want to turn on the UART listening and turn it off as per my logic rather than “listening” continuously to UART.
Best regards,
–Utpal
Hi Utpal.
It seems like we are heading in the same direction with a project.
Please see my post right at the bottom.
Regards.
Thanks for this awesome tutorial, it’s awesome!
I was wondering if it was possible to do the opposite of what you did, aka changing the arduino pin value from the esp8266? I want to do a project where I will need more than the 12 GPIO availlable on the ESP and this would be a great solution, logic on the ESP, and just use the arduino to read values/update outputs!
Thanks for the feedback!
-David
Hi David,
Yes, it is possible to do that… Instead of doing the read with the ESP8266 you do uart.write()…
Please checkout the NodeMCU firmware page: nodemcu.readthedocs.org/en/dev/en/modules/uart/#uartwrite
is it possible to use arduino ide for programming the esp8266 instead of esplorer ide?
Rui, can you make a tutorial for connecting esp8266 and android and arduino, which esp as hotspot, and make the android app with app inventor? the result is sending a data from android and appear in arduino serial monitor. thanks…
I’ve added that project to my to-do list, but it will take a while to complete.
Thanks for the suggestion,
Rui
Hi Rui,
Thanks for the tutorial but I can’t quite find the code that is supposed to be copied into ESPlorer IDE. Can you check and re-post it maybe? Thanks!
Hi,
I’ve fixed the blog post. I hope you can see the code now.
Thanks for letting me know,
Rui
Good day Rui.
I hope you still read this.
If you were to expand the code in the ESP8266 to do more, different things how would you go about it. Lets say I want to check the GPIO’s for a pulse that is coming in, and then write something to a file that I have created .
It seems to me like Utpal above is also doing more or less the same that I am trying.
I want to monitor a GPS, read a specific string, then check the pulse width (that comes from a depth sounder in a boat on a large dam) on the GPIO, and then write that information to a file.
A data logger for GPS location and depth saved to a file.
I transferred data from ESP8266 now i want to check that data in my system. is it possible to check it transfered to the mobile now i need to check in my system
Hi Rui,
Let us assume that I want to send some readings in json format like {“Temp”:”23″,”Hum”:”12″} from Arduino or some other mcu via GPIO interface to ESP8266 and post it to a webserver from ESP.Can you suggest the right approach to transfer this data from Arduino to ESP8266 via GPIO?
Thanks in advance.
Regards
Manas
Hey Rui,
I just wanted to leave a comment saying thank you!
You have helped me with my project I’ve been working on for nearly a year.
Cheers!
Josh
Thank you Josh.
I’m glad I could help!
Rui
How can i get the data of alcohol sensor through esp8266 to message/email
Hi.
Unfortunately, we don’t have any tutorial on that subject.
Regards,
Sara
My “Save to ESP” button is grayed out !
I have installed it on MacBook Air and I have connected the FTDI Programmer to the ESP01.
Would appreciate if you let me know what’s happening.
There is a small red led lit up on the FTDI programmer as well as the LED on the ESP01 also lit indicating the power to the ESP 01 has been provided.
I tried on both the com ports of my mac . No response.
Also in the right panel of the ESPlorer there is an Open button, if I click on it – I see a “Communication with MCU” and then nothing happens. (The Open button changes to Close thats all) when I click on the Close – then I see a “PORT CLOSED” message
Hello, I find your article interesting and useful.
I would still need clarification on the steps to follow.
I am trying to do the sensor data acquisition on an atmega328 the same as on the Arduino uno.
These data will then be transmitted to the esp for my part an esp-wroom-02 based on esp8266Ex which will be responsible for connecting to the internet to display my data in an application.
Questions:
– is the FTDI only used to load the code that will allow the processing of data in the ESP?
– the ESP and the Arduino are well connected by the TX and RX pins?
– how could I put the esp to sleep to wake it up only when data is present on the serial bus (TX, RX).
Thank you.
Nice layout… but you should add the real schematics and not only the Fritzing… which resistor goes on which pin? The paragraph called “Final Circuit” is only a Fritzing schema and doesn’t show this essential information. Add this and the (good) article will be complete!
Hello Rui,
I would like to read out and evaluate the RFID reader RDM6300 with the ESP8266.
Hoped this script would work for it, it doesn’t work.
Do you know a Lua script that works with the RDM6300 RFID reader?
greeting
Manuel
Hi Manuel.
I would suggest following other examples to send data over serial.
This tutorial is outdated.
I would recommend examples using Arduino IDE instead of LUA.
Regards,
Sara
I found this example of UART communication because I need it for application in my Arduino project, but there is one problem, the ESP board is programmed on the Lua platform instead of the Arduino platform i need.