Sending Data From an Arduino to the ESP8266 via Serial

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: 

If you like the ESP WiFi module and you want to build more projects you can download my eBook called “Home Automation using ESP8266” hereLet’s get started!

Watch the video demonstration below

Parts List

Here’s the hardware that you need to complete this tutorial example:

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);
}

View raw code

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.ESP-Bitcoin-price_bb

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:

  1. Click here to download ESPlorer
  2. Unzip that folder
  3. Go to the main folder
  4. Run ESPlorer.jar
  5. Open the ESPlorer (as shown in the Figure below). esplorer start

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)

View raw code

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:

  1. Connect your FTDI programmer to your computer
  2. Set bad raute as 9600
  3. Select your FTDI programmer port (COM3, for example)
  4. Press Open/Close
  5. Select NodeMCU+MicroPtyhon tab
  6. 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.

esplorer

Final Circuit

Follow the next schematics to complete this tutorial.

esp8266 talks via serial with arduino s

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.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

38 thoughts on “Sending Data From an Arduino to the ESP8266 via Serial”

  1. 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).

    Reply
  2. 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

    Reply
  3. 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

    Reply
  4. 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

    Reply
  5. 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

    Reply
    • Hi Utpal.
      It seems like we are heading in the same direction with a project.
      Please see my post right at the bottom.
      Regards.

      Reply
  6. 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

    Reply
    • 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

      Reply
  7. 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…

    Reply
  8. 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!

    Reply
  9. 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.

    Reply
  10. 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

    Reply
  11. 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

    Reply
  12. 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

    Reply
  13. 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.

    Reply
  14. 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!

    Reply
  15. 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

    Reply
    • 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

      Reply
  16. 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.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.