{"id":89983,"date":"2019-10-19T14:45:37","date_gmt":"2019-10-19T14:45:37","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=89983"},"modified":"2019-10-19T14:53:18","modified_gmt":"2019-10-19T14:53:18","slug":"ttgo-lora32-sx1276-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/ttgo-lora32-sx1276-arduino-ide\/","title":{"rendered":"TTGO LoRa32 SX1276 OLED Board: Getting Started with Arduino IDE"},"content":{"rendered":"\n<p>The <strong>TTGO LoRa32 SX1276 OLED<\/strong> is an ESP32 development board with a built-in LoRa chip and an SSD1306 0.96 inch OLED display. In this guide, we&#8217;ll show you how to: send and receive LoRa packets (point to point communication) and use the OLED display with Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Arduino-IDE-Project.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TTGO LoRa32 OLED SX1276 Board: Getting Started with Arduino IDE\" class=\"wp-image-90249\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Arduino-IDE-Project.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Arduino-IDE-Project.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Arduino-IDE-Project.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Arduino-IDE-Project.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\">For an introduction to LoRa communication, read: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-lora-rfm95-transceiver-arduino-ide\/\">ESP32 with LoRa using Arduino IDE<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TTGO LoRa32 SX1276 OLED Overview<\/h2>\n\n\n\n<p>The TTGO LoRa32 SX1276 OLED is a development board with an ESP32, a built-in LoRa chip and an SSD1306 OLED display. This is the OLED model display we use in most of our electronics projects (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">Guide for OLED display with ESP32<\/a>).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\" TTGO LoRa32 OLED SX1276 Overview\" class=\"wp-image-90250\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The board also features several GPIOs to connect peripherals, PRG (BOOT) and RST buttons, and a lithium battery connector. For a more in-depth overview of this board, read: <a rel=\"noreferrer noopener\" aria-label=\"TTGO LoRa32 OLED Overview (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-sx1276-lora-ssd1306-oled\/\" target=\"_blank\">TTGO LoRa32 SX1276 OLED Review<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where to buy?<\/h3>\n\n\n\n<p>You can go to the <a href=\"https:\/\/makeradvisor.com\/tools\/ttgo-lora32-sx1276-esp32-oled\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"TTGO LoRa32 SX1276 OLED page on Maker Advisor (opens in a new tab)\">TTGO LoRa32 SX1276 OLED page on Maker Advisor<\/a> to find the best price at different stores. To complete this tutorial, you&#8217;ll need <strong>two <\/strong><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ttgo-lora32-sx1276-esp32-oled\/\" target=\"_blank\">TTGO LoRa32 boards<\/a>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/makeradvisor.com\/tools\/ttgo-lora32-sx1276-esp32-oled\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"500\" height=\"328\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa32-OLED-SX1276-p.jpg?resize=500%2C328&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TTGO LoRa32 OLED SX1276\" class=\"wp-image-90262\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa32-OLED-SX1276-p.jpg?w=500&amp;quality=100&amp;strip=all&amp;ssl=1 500w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa32-OLED-SX1276-p.jpg?resize=300%2C197&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"> TTGO LoRa32 SX1276 OLED<\/h2>\n\n\n\n<p>The following figure shows the TTGO LoRa32 OLED board pinout.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"455\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-Board-Pinout.jpg?resize=936%2C455&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\" TTGO LoRa32 OLED SX1276 Pinout Diagram\" class=\"wp-image-89985\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-Board-Pinout.jpg?w=936&amp;quality=100&amp;strip=all&amp;ssl=1 936w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-Board-Pinout.jpg?resize=300%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-Board-Pinout.jpg?resize=768%2C373&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><\/figure><\/div>\n\n\n\n<p>The OLED displays communicates using <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\">I2C communication protocol<\/a>. It is internally connected to the ESP32 on the following pins:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>OLED<\/strong> (built-in)<\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td><strong>SDA<\/strong><\/td><td><span class=\"rnthl rntclblue\">GPIO 4<\/span><\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td><span class=\"rnthl rntclgreen\">GPIO 15<\/span><\/td><\/tr><tr><td><strong>RST<\/strong><\/td><td><span class=\"rnthl rntcyellow\">GPIO 16<\/span><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The SX1276 LoRa chip communicates via SPI communication protocol, and it is internally connected to the ESP32 on the following GPIOs:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>SX1276 LoRa<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td>MISO<\/td><td><span class=\"rnthl rntcgreen\">GPIO 19<\/span><\/td><\/tr><tr><td>MOSI<\/td><td><span class=\"rnthl rntcgreen\">GPIO 27<\/span><\/td><\/tr><tr><td>SCK<\/td><td><span class=\"rnthl rntcgreen\">GPIO 5<\/span><\/td><\/tr><tr><td>CS<\/td><td><span class=\"rnthl rntcgreen\">GPIO 18<\/span><\/td><\/tr><tr><td>IRQ<\/td><td><span class=\"rnthl rntcorange\">GPIO 26<\/span><\/td><\/tr><tr><td>RST<\/td><td><span class=\"rnthl rntcbrown\">GPIO 14<\/span><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install ESP32 Boards on Arduino IDE<\/h2>\n\n\n\n<p>To program the TTGO LoRa32 board, we&#8217;ll use Arduino IDE. So, you must have Arduino IDE installed as well as the ESP32 add-on. Follow the next guide to install the ESP32 package on Arduino IDE, if you haven&#8217;t already: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Installing OLED Libraries<\/h2>\n\n\n\n<p>There are several libraries available to control the OLED display with the ESP32. In this tutorial we\u2019ll use two Adafruit libraries: <a rel=\"noreferrer noopener\" aria-label=\"Adafruit_SSD1306 library (opens in a new tab)\" href=\"https:\/\/github.com\/adafruit\/Adafruit_SSD1306\" target=\"_blank\">Adafruit_SSD1306 library<\/a> and <a href=\"https:\/\/github.com\/adafruit\/Adafruit-GFX-Library\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Adafruit_GFX library (opens in a new tab)\">Adafruit_GFX library<\/a>.<\/p>\n\n\n\n<p>Follow the next steps to install those libraries.<\/p>\n\n\n\n<p>1. Open your Arduino IDE and go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>2. Type \u201c<strong>SSD1306<\/strong>\u201d in the search box and install the SSD1306 library from Adafruit.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"788\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=788%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing the Adafruit SSD1306 library for OLED display Arduino IDE\" class=\"wp-image-85156\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?w=788&amp;quality=100&amp;strip=all&amp;ssl=1 788w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/figure><\/div>\n\n\n\n<p>3. After installing the SSD1306 library from Adafruit, type \u201c<strong>GFX<\/strong>\u201d in the search box and install the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing GFX Library for ESP32 Arduino IDE\" class=\"wp-image-85157\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Installing LoRa Library<\/h2>\n\n\n\n<p>There are several libraries available to easily send and receive LoRa packets with the ESP32. In this example we\u2019ll be using the <a href=\"https:\/\/github.com\/sandeepmistry\/arduino-LoRa\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"arduino-LoRa library by sandeep mistry (opens in a new tab)\">arduino-LoRa library by sandeep mistry<\/a>.<\/p>\n\n\n\n<p>Open your Arduino IDE, and go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong> and search for \u201c<strong>LoRa<\/strong>\u201c. Select the LoRa library highlighted in the figure below, and install it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/install-LoRa-library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing LoRa Library for ESP32 Arduino IDE\" class=\"wp-image-89988\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/install-LoRa-library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/install-LoRa-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/install-LoRa-library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LoRa Sender Sketch<\/h2>\n\n\n\n<p>Copy the following code to your Arduino IDE. This code sends a &#8220;hello&#8221; message followed by a counter via LoRa every 10 seconds. It also displays the counter on the OLED display.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/ttgo-lora32-sx1276-arduino-ide\/\n*********\/\n\n\/\/Libraries for LoRa\n#include &lt;SPI.h&gt;\n#include &lt;LoRa.h&gt;\n\n\/\/Libraries for OLED Display\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_GFX.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;\n\n\/\/define the pins used by the LoRa transceiver module\n#define SCK 5\n#define MISO 19\n#define MOSI 27\n#define SS 18\n#define RST 14\n#define DIO0 26\n\n\/\/433E6 for Asia\n\/\/866E6 for Europe\n\/\/915E6 for North America\n#define BAND 866E6\n\n\/\/OLED pins\n#define OLED_SDA 4\n#define OLED_SCL 15 \n#define OLED_RST 16\n#define SCREEN_WIDTH 128 \/\/ OLED display width, in pixels\n#define SCREEN_HEIGHT 64 \/\/ OLED display height, in pixels\n\n\/\/packet counter\nint counter = 0;\n\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, OLED_RST);\n\nvoid setup() {\n  \/\/initialize Serial Monitor\n  Serial.begin(115200);\n\n  \/\/reset OLED display via software\n  pinMode(OLED_RST, OUTPUT);\n  digitalWrite(OLED_RST, LOW);\n  delay(20);\n  digitalWrite(OLED_RST, HIGH);\n\n  \/\/initialize OLED\n  Wire.begin(OLED_SDA, OLED_SCL);\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { \/\/ Address 0x3C for 128x32\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\n    for(;;); \/\/ Don't proceed, loop forever\n  }\n  \n  display.clearDisplay();\n  display.setTextColor(WHITE);\n  display.setTextSize(1);\n  display.setCursor(0,0);\n  display.print(&quot;LORA SENDER &quot;);\n  display.display();\n  \n  Serial.println(&quot;LoRa Sender Test&quot;);\n\n  \/\/SPI LoRa pins\n  SPI.begin(SCK, MISO, MOSI, SS);\n  \/\/setup LoRa transceiver module\n  LoRa.setPins(SS, RST, DIO0);\n  \n  if (!LoRa.begin(BAND)) {\n    Serial.println(&quot;Starting LoRa failed!&quot;);\n    while (1);\n  }\n  Serial.println(&quot;LoRa Initializing OK!&quot;);\n  display.setCursor(0,10);\n  display.print(&quot;LoRa Initializing OK!&quot;);\n  display.display();\n  delay(2000);\n}\n\nvoid loop() {\n   \n  Serial.print(&quot;Sending packet: &quot;);\n  Serial.println(counter);\n\n  \/\/Send LoRa packet to receiver\n  LoRa.beginPacket();\n  LoRa.print(&quot;hello &quot;);\n  LoRa.print(counter);\n  LoRa.endPacket();\n  \n  display.clearDisplay();\n  display.setCursor(0,0);\n  display.println(&quot;LORA SENDER&quot;);\n  display.setCursor(0,20);\n  display.setTextSize(1);\n  display.print(&quot;LoRa packet sent.&quot;);\n  display.setCursor(0,30);\n  display.print(&quot;Counter:&quot;);\n  display.setCursor(50,30);\n  display.print(counter);      \n  display.display();\n\n  counter++;\n  \n  delay(10000);\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\/ESP32\/ESP32_TTGO_LoRa_Sender.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>Start by including the libraries to interact with the LoRa chip.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;SPI.h>\n#include &lt;LoRa.h><\/code><\/pre>\n\n\n\n<p>Then, include the libraries to interface with the <a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">I2C OLED display<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h>\n#include &lt;Adafruit_GFX.h>\n#include &lt;Adafruit_SSD1306.h><\/code><\/pre>\n\n\n\n<p>Define the pins used by the LoRa transceiver module:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCK 5\n#define MISO 19\n#define MOSI 27\n#define SS 18\n#define RST 14\n#define DIO0 26<\/code><\/pre>\n\n\n\n<p>Select the LoRa frequency:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define BAND 866E6<\/code><\/pre>\n\n\n\n<p>Define the OLED pins.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define OLED_SDA 4\n#define OLED_SCL 15 \n#define OLED_RST 16<\/code><\/pre>\n\n\n\n<p>Define the OLED size.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 128 \/\/ OLED display width, in pixels\n#define SCREEN_HEIGHT 64 \/\/ OLED display height, in pixels<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">counter<\/span> variable to keep track of the number of LoRa packets sent.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int counter = 0;<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> object called <span class=\"rnthl rntliteral\">display<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, OLED_RST);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, to start using the OLED you need to do a manual reset via software using the RST pin. To do this reset, you need to declare the RST pin as an output, set it to <span class=\"rnthl rntliteral\">LOW<\/span> for a few milliseconds and then, set it to <span class=\"rnthl rntliteral\">HIGH<\/span> again.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(OLED_RST, OUTPUT);\ndigitalWrite(OLED_RST, LOW);\ndelay(20);\ndigitalWrite(OLED_RST, HIGH);<\/code><\/pre>\n\n\n\n<p>Start an I2C communication using the defined <span class=\"rnthl rntliteral\">OLED_SDA<\/span> and <span class=\"rnthl rntliteral\">OLED_SCL<\/span> pins using <span class=\"rnthl rntliteral\">Wire.begin()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Wire.begin(OLED_SDA, OLED_SCL);<\/code><\/pre>\n\n\n\n<p>After that, initialize the display with the following parameters. The parameters set as <span class=\"rnthl rntliteral\">false<\/span> ensure that the library doesn\u2019t use the default I2C pins and use the pins defined in the code (<span class=\"rnthl rntclblue\">GPIO 4<\/span> and <span class=\"rnthl rntclgreen\">GPIO 15<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { \/\/ Address 0x3C for 128x32\n  Serial.println(F(\"SSD1306 allocation failed\"));\n  for(;;); \/\/ Don't proceed, loop forever\n}<\/code><\/pre>\n\n\n\n<p>Then, you can use the methods from the Adafruit library to interact with the OLED display. To learn more you can read our <a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">tutorial about the I2C OLED display with the ESP32<\/a>.<\/p>\n\n\n\n<p>Write the message &#8220;LORA SENDER&#8221; to the display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.clearDisplay();\ndisplay.setTextColor(WHITE);\ndisplay.setTextSize(1);\ndisplay.setCursor(0,0);\ndisplay.print(\"LORA SENDER \");\ndisplay.display();<\/code><\/pre>\n\n\n\n<p>Initialize the serial monitor for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);\nSerial.println(\"LoRa Sender Test\");<\/code><\/pre>\n\n\n\n<p>Define the SPI pins used by the LoRa chip. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SPI.begin(SCK, MISO, MOSI, SS);<\/code><\/pre>\n\n\n\n<p>And set up the LoRa transceiver module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>LoRa.setPins(SS, RST, DIO0);<\/code><\/pre>\n\n\n\n<p>Finally, initialize the LoRa transceiver module using the <span class=\"rnthl rntliteral\">begin()<\/span> method on the <span class=\"rnthl rntliteral\">LoRa<\/span> object and pass the frequency as argument.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!LoRa.begin(BAND)) {\n  Serial.println(\"Starting LoRa failed!\");\n  while (1);\n}<\/code><\/pre>\n\n\n\n<p>If we succeed in initializing the display, we write a success message on the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setCursor(0,10);\ndisplay.print(\"LoRa Initializing OK!\");\ndisplay.display();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> is where we&#8217;ll send the packets. You initialize a packet with the&nbsp;<span class=\"rnthl rntliteral\">beginPacket()<\/span>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>LoRa.beginPacket();<\/code><\/pre>\n\n\n\n<p>You write data into the packet using the <span class=\"rnthl rntliteral\">print()<\/span> method. As you can see in the following two lines, we\u2019re sending a hello message followed by the counter.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>LoRa.print(\"hello \");\nLoRa.print(counter);<\/code><\/pre>\n\n\n\n<p>Then, close the packet with the <span class=\"rnthl rntliteral\">endPacket()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>LoRa.endPacket();<\/code><\/pre>\n\n\n\n<p>Next, write the <span class=\"rnthl rntliteral\">counter<\/span> on the OLED display<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.clearDisplay();\ndisplay.setCursor(0,0);\ndisplay.println(\"LORA SENDER\");\ndisplay.setCursor(0,20);\ndisplay.setTextSize(1);\ndisplay.print(\"LoRa packet sent.\");\ndisplay.setCursor(0,30);\ndisplay.print(\"Counter:\");\ndisplay.setCursor(50,30);\ndisplay.print(counter);\ndisplay.display();<\/code><\/pre>\n\n\n\n<p>After this, the <span class=\"rnthl rntliteral\">counter<\/span> message is incremented by one in every loop, which happens every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>counter++;\ndelay(10000);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the LoRa Sender<\/h2>\n\n\n\n<p>Upload the code to your board. You need to select the right board and COM port you&#8217;re using.<\/p>\n\n\n\n<p>To select the board, in the Arduino IDE, go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select the <strong>TTGO LoRa32-OLED V1<\/strong> board. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"452\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/Select-TTGO-LoRa-Board-Arduino-IDE.png?resize=750%2C452&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Select TTGO LoRa32 OLED V1 in Arduino IDE\" class=\"wp-image-90251\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/Select-TTGO-LoRa-Board-Arduino-IDE.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/Select-TTGO-LoRa-Board-Arduino-IDE.png?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After uploading the code to your board, it should start sending LoRa packets.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Sender.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TTGO LoRa ESP32 Dev Board Sender\" class=\"wp-image-90252\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Sender.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Sender.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">LoRa Receiver Sketch<\/h2>\n\n\n\n<p>Now, upload the receiver sketch to another TTGO LoRa32 OLED board. This sketch listens for LoRa packets within its range and prints the content of the packets on the OLED, as well as the RSSI (relative received signal strength).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/ttgo-lora32-sx1276-arduino-ide\/\n*********\/\n\n\/\/Libraries for LoRa\n#include &lt;SPI.h&gt;\n#include &lt;LoRa.h&gt;\n\n\/\/Libraries for OLED Display\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_GFX.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;\n\n\/\/define the pins used by the LoRa transceiver module\n#define SCK 5\n#define MISO 19\n#define MOSI 27\n#define SS 18\n#define RST 14\n#define DIO0 26\n\n\/\/433E6 for Asia\n\/\/866E6 for Europe\n\/\/915E6 for North America\n#define BAND 866E6\n\n\/\/OLED pins\n#define OLED_SDA 4\n#define OLED_SCL 15 \n#define OLED_RST 16\n#define SCREEN_WIDTH 128 \/\/ OLED display width, in pixels\n#define SCREEN_HEIGHT 64 \/\/ OLED display height, in pixels\n\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, OLED_RST);\n\nString LoRaData;\n\nvoid setup() { \n  \/\/initialize Serial Monitor\n  Serial.begin(115200);\n  \n  \/\/reset OLED display via software\n  pinMode(OLED_RST, OUTPUT);\n  digitalWrite(OLED_RST, LOW);\n  delay(20);\n  digitalWrite(OLED_RST, HIGH);\n  \n  \/\/initialize OLED\n  Wire.begin(OLED_SDA, OLED_SCL);\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { \/\/ Address 0x3C for 128x32\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\n    for(;;); \/\/ Don't proceed, loop forever\n  }\n\n  display.clearDisplay();\n  display.setTextColor(WHITE);\n  display.setTextSize(1);\n  display.setCursor(0,0);\n  display.print(&quot;LORA RECEIVER &quot;);\n  display.display();\n\n  Serial.println(&quot;LoRa Receiver Test&quot;);\n  \n  \/\/SPI LoRa pins\n  SPI.begin(SCK, MISO, MOSI, SS);\n  \/\/setup LoRa transceiver module\n  LoRa.setPins(SS, RST, DIO0);\n\n  if (!LoRa.begin(BAND)) {\n    Serial.println(&quot;Starting LoRa failed!&quot;);\n    while (1);\n  }\n  Serial.println(&quot;LoRa Initializing OK!&quot;);\n  display.setCursor(0,10);\n  display.println(&quot;LoRa Initializing OK!&quot;);\n  display.display();  \n}\n\nvoid loop() {\n\n  \/\/try to parse packet\n  int packetSize = LoRa.parsePacket();\n  if (packetSize) {\n    \/\/received a packet\n    Serial.print(&quot;Received packet &quot;);\n\n    \/\/read packet\n    while (LoRa.available()) {\n      LoRaData = LoRa.readString();\n      Serial.print(LoRaData);\n    }\n\n    \/\/print RSSI of packet\n    int rssi = LoRa.packetRssi();\n    Serial.print(&quot; with RSSI &quot;);    \n    Serial.println(rssi);\n\n   \/\/ Dsiplay information\n   display.clearDisplay();\n   display.setCursor(0,0);\n   display.print(&quot;LORA RECEIVER&quot;);\n   display.setCursor(0,20);\n   display.print(&quot;Received packet:&quot;);\n   display.setCursor(0,30);\n   display.print(LoRaData);\n   display.setCursor(0,40);\n   display.print(&quot;RSSI:&quot;);\n   display.setCursor(30,40);\n   display.print(rssi);\n   display.display();   \n  }\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\/ESP32\/ESP32_TTGO_LoRa_Receiver.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This sketch is very similar with the previous one. We just need to modify some lines to receive LoRa packets instead of sending.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we check if there are new packets to receive using the <span class=\"rnthl rntliteral\">parsePacket()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int packetSize = LoRa.parsePacket();<\/code><\/pre>\n\n\n\n<p>If there&#8217;s a new packet, we&#8217;ll read its content. To read the incoming data, use the <span class=\"rnthl rntliteral\">readString()<\/span> method. The data received is saved on the <span class=\"rnthl rntliteral\">LoRaData<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (packetSize) {\n  \/\/received a packet\n  Serial.print(\"Received packet \");\n\n  \/\/read packet\n  while (LoRa.available()) {\n    LoRaData = LoRa.readString();\n    Serial.print(LoRaData);\n  }<\/code><\/pre>\n\n\n\n<p>We also get the RSSI of the received packet by using the <span class=\"rnthl rntliteral\">packetRSSI()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int rssi = LoRa.packetRssi();<\/code><\/pre>\n\n\n\n<p>Finally, display the received message, as well as the RSSI.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.clearDisplay();\ndisplay.setCursor(0,0);\ndisplay.print(\"LORA RECEIVER\");\ndisplay.setCursor(0,20);\ndisplay.print(\"Received packet:\");\ndisplay.setCursor(0,30);\ndisplay.print(LoRaData);\ndisplay.setCursor(0,40);\ndisplay.print(\"RSSI:\");\ndisplay.setCursor(30,40);\ndisplay.print(rssi);\ndisplay.display();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the LoRa Receiver<\/h2>\n\n\n\n<p>Upload the code to your board. Don&#8217;t forget you need to select the <strong>TTGO LoRa32-OLED V1<\/strong> in the Boards menu.<\/p>\n\n\n\n<p>After uploading the code, it should start receiving the LoRa packets from the other board.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Receiver.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TTGO LoRa ESP32 Dev Board Receiver\" class=\"wp-image-90254\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Receiver.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Receiver.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This article was a quick getting started guide for the <a href=\"https:\/\/makeradvisor.com\/esp32-sx1276-lora-ssd1306-oled\/\">TTGO LoRa32 board<\/a> how to: send LoRa packets in point to point communication and use the OLED display.<\/p>\n\n\n\n<p>Now, the idea is to combine what you&#8217;ve learned here to build IoT projects. LoRa can be specially useful if you want to receive sensor readings that are not covered by your wi-fi network and are several meters apart. Additionally, you can also connect your board to the TTN (The Things Network).<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a> (eBook + Video Course)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a> (eBook)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-lora-rfm95-transceiver-arduino-ide\/\">ESP32 with LoRa RFM95 Chip using Arduino IDE \u2013 Getting Started<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\">More ESP32 projects&#8230; <\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The TTGO LoRa32 SX1276 OLED is an ESP32 development board with a built-in LoRa chip and an SSD1306 0.96 inch OLED display. In this guide, we&#8217;ll show you how to: &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"TTGO LoRa32 SX1276 OLED Board: Getting Started with Arduino IDE\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/ttgo-lora32-sx1276-arduino-ide\/#more-89983\" aria-label=\"Read more about TTGO LoRa32 SX1276 OLED Board: Getting Started with Arduino IDE\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":90249,"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":[281,276,277,299,264],"tags":[],"class_list":["post-89983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/10\/TTGO-LoRa-ESP32-Dev-Board-Arduino-IDE-Project.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/89983","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=89983"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/89983\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/90249"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=89983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=89983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=89983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}