{"id":12733,"date":"2015-10-08T11:30:03","date_gmt":"2015-10-08T11:30:03","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=12733"},"modified":"2019-04-02T10:23:34","modified_gmt":"2019-04-02T10:23:34","slug":"sending-data-from-an-arduino-to-the-esp8266-via-serial","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/sending-data-from-an-arduino-to-the-esp8266-via-serial\/","title":{"rendered":"Sending Data From an Arduino to the ESP8266 via Serial"},"content":{"rendered":"<p>In this tutorial\u00a0you\u2019re going to learn how to send data from an Arduino to the ESP8266 via serial communication.<!--more--><\/p>\n<p><strong>Before you continue reading this project, please complete\u00a0the following tutorials:\u00a0<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-esp8266-wifi-transceiver-review\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to get started with the ESP8266<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-nodemcu-firmware-on-the-esp8266-using-windows\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to flash your ESP8266\u00a0with NodeMCU<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-level-shift-5v-to-3-3v\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Level Shift 5V to 3.3V<\/a><\/li>\n<\/ul>\n<p>If you like the ESP WiFi module and you want to build more\u00a0projects\u00a0you can download my eBook called <a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">&#8220;Home Automation using ESP8266&#8221; here<\/a>.\u00a0<strong>Let\u2019s get started!<\/strong><\/p>\n<h2>Watch the video demonstration below<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/VROj6i3npo0?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Parts List<\/h2>\n<p>Here&#8217;s the hardware that you need to complete\u00a0this tutorial example:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp-01-wi-fi-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x\u00a0ESP8266<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best ESP8266 Wi-Fi Development Boards<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ftdi-programmer-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x\u00a0FTDI programmer<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x Breadboard\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x LED<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x 220 Ohm Resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x 1K Ohm Resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x 2k Ohm Resistor<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/makeradvisor.com\/tools\/esp-01-wi-fi-board\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-4815\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/01\/ESP01v0.jpg?resize=224%2C224&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"224\" height=\"224\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/01\/ESP01v0.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/01\/ESP01v0.jpg?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/01\/ESP01v0.jpg?resize=300%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 224px) 100vw, 224px\" \/><p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p><\/a><\/p>\n<h2>Uploading Your Arduino\u00a0Sketch<\/h2>\n<p>The Arduino sketch for this tutorial\u00a0is very simple.<\/p>\n<p>You begin a serial\u00a0communication in the <em>setup()<\/em> function at a baud rate of 9600. Then in the <em>loop()<\/em> function it prints &#8220;HI!&#8221; continuously every 1 second (that message will be received by\u00a0your ESP later).<\/p>\n<p>Copy the sketch below to your Arduino IDE and upload it to your Arduino\u00a0board.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Rui Santos \n * Complete Project Details http:\/\/randomnerdtutorials.com\n *\/\n\n\/\/ Send Data From Arduino to ESP8266 via Serial @ baud rate 9600\nvoid setup() {\n    Serial.begin(9600);\n}\n\nvoid loop() {\n    Serial.print(&quot;HI!&quot;);\n    delay(1000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266-Serial-Communication-with-Arduino\/Arduino_talks_with_ESP8266.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<p>Now if you open your Arduino serial monitor at a baud rate of 9600, you&#8217;ll see a message appearing in your window saying\u00a0&#8220;HI!&#8221; every 1 second.<\/p>\n<h2>Schematics (3.3V FTDI\u00a0Programmer)<\/h2>\n<p><a href=\"https:\/\/randomnerdtutorials.com\/flashing-nodemcu-firmware-on-the-esp8266-using-windows\/\" target=\"_blank\" rel=\"noopener noreferrer\">Having your ESP8266 flashed with NodeMCU<\/a>, follow the next schematics to\u00a0establish a serial communication between your FTDI programmer and your ESP8266 to upload some code.<a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/03\/ESP-Bitcoin-price_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"size-full wp-image-5193 aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/03\/ESP-Bitcoin-price_bb.png?resize=759%2C279&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-Bitcoin-price_bb\" width=\"759\" height=\"279\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/03\/ESP-Bitcoin-price_bb.png?w=759&amp;quality=100&amp;strip=all&amp;ssl=1 759w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/03\/ESP-Bitcoin-price_bb.png?resize=300%2C110&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/a><\/p>\n<h2>Downloading ESPlorer IDE<\/h2>\n<p>I recommend using the ESPlorer IDE which is a program\u00a0created by 4refr0nt\u00a0to create and save Lua files into your\u00a0ESP8266. Follow these instructions to download and install ESPlorer:<\/p>\n<ol>\n<li><a href=\"http:\/\/esp8266.ru\/esplorer\/#tutorials\" target=\"_blank\" rel=\"noopener noreferrer\">Click here<\/a> to download\u00a0ESPlorer<\/li>\n<li>Unzip that folder<\/li>\n<li>Go to the main\u00a0folder<\/li>\n<li>Run\u00a0ESPlorer.jar<\/li>\n<li>Open the ESPlorer (as shown in the Figure below). <img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-5125 aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/esplorer-start.png?resize=386%2C288&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"esplorer start\" width=\"386\" height=\"288\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/esplorer-start.png?w=802&amp;quality=100&amp;strip=all&amp;ssl=1 802w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/esplorer-start.png?resize=300%2C224&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><\/li>\n<\/ol>\n<h2>Writing Your ESP8266\u00a0Script<\/h2>\n<p>Copy and paste the code below into ESPlorer IDE window.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">-- Rui Santos\n-- Complete project details at http:\/\/randomnerdtutorials.com\n\nledOn = 0\npin=4\ngpio.mode(pin,gpio.OUTPUT)\nuart.on(&quot;data&quot;, 3, \n    function(data)\n        print(&quot;Received from Arduino:&quot;, data)\n        if data==&quot;HI!&quot; then\n            if ledOn==0 then\n                ledOn = 1\n                gpio.write(pin,gpio.HIGH)\n                print(&quot;LED On&quot;)\n            else\n                ledOn = 0\n                gpio.write(pin,gpio.LOW)\n                print(&quot;LED Off&quot;)\n        end        \n    end        \nend, 0)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266-Serial-Communication-with-Arduino\/init.lua\" target=\"_blank\">View raw code<\/a><\/p>\n<p><strong>Summary:<\/strong> The ESP is configured to listen to serial communications. Every time that receives the string &#8220;HI!&#8221; at a baud rate of 9600, it will turn the GPIO 2 on or off.<\/p>\n<h2>Uploading Your Script<\/h2>\n<p>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:<\/p>\n<ol>\n<li>Connect your FTDI programmer to your computer<\/li>\n<li>Set bad raute as 9600<\/li>\n<li>Select your FTDI programmer port (COM3, for example)<\/li>\n<li>Press\u00a0Open\/Close<\/li>\n<li>Select NodeMCU+MicroPtyhon tab<\/li>\n<li>Copy the your Lua\u00a0script into\u00a0ESPlorer<\/li>\n<\/ol>\n<p>Then you simply click the button\u00a0<strong>Save to ESP <\/strong>and save your file with the name &#8220;init.lua&#8221;.\u00a0Everything that you need to worry about or change is highlighted in red box in the following Figure.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11406\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/esplorer.png?resize=737%2C372&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"esplorer\" width=\"737\" height=\"372\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/esplorer.png?w=1252&amp;quality=100&amp;strip=all&amp;ssl=1 1252w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/esplorer.png?resize=300%2C151&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/esplorer.png?resize=1024%2C517&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/p>\n<h2>Final Circuit<\/h2>\n<p>Follow the next schematics to complete this tutorial.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12734\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/10\/esp8266-talks-via-serial-with-arduino-s.png?resize=700%2C588&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"esp8266 talks via serial with arduino s\" width=\"700\" height=\"588\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/10\/esp8266-talks-via-serial-with-arduino-s.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/10\/esp8266-talks-via-serial-with-arduino-s.png?resize=300%2C252&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p><strong>Note:<\/strong> I&#8217;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\u00a0baud rates. <a href=\"https:\/\/randomnerdtutorials.com\/how-to-level-shift-5v-to-3-3v\/\" target=\"_blank\" rel=\"noopener noreferrer\">Read this blog post<\/a> for more information about lowering the voltage of signals.<\/p>\n<h3>Demonstration<\/h3>\n<p>Now your LED should be blinking every one second. This means that your Arduino is sending the string &#8220;HI&#8221; and your ESP is receiving that data.\u00a0<em>Watch<\/em> the video at the beginning of this post\u00a0for a live demonstration.<\/p>\n<p>Now instead of sending a string saying just &#8220;HI!&#8221;, you can attach sensors to your Arduino and send that data to your ESP instead. Later you can <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">build a web server<\/a> that displays that data.<\/p>\n<p><strong>Read Next&#8230;<\/strong><\/p>\n<p>You might also find interesting trying one of these tutorials:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-make-two-esp8266-talk\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Make Two ESP8266 Talk<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-remote-controlled-sockets\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP8266 Remote Controlled Sockets<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/posting-a-tweet-with-the-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">Posting a Tweet with the ESP8266<\/a><\/li>\n<\/ul>\n<p><strong>Do you have any questions?\u00a0Leave a comment down below!<\/strong><\/p>\n<p>Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my <a href=\"https:\/\/randomnerdtutorials.com\/download\" target=\"_blank\" rel=\"noopener noreferrer\">blog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial\u00a0you\u2019re going to learn how to send data from an Arduino to the ESP8266 via serial communication.<\/p>\n","protected":false},"author":1,"featured_media":12734,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,2,267,245,265,214,240,220,264,10],"tags":[],"class_list":["post-12733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-arduino-project","category-arduino-ide","category-esp8266-project","category-esp8266","category-esp8266-projects","category-http-client","category-project","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/10\/esp8266-talks-via-serial-with-arduino-s.png?fit=700%2C588&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/12733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=12733"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/12733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/12734"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=12733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=12733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=12733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}