SPI Transactions between ESP8266 and Arduino UNO

As the weekend came by, I started exploring more about the esp8266, especially the SPI, as I am looking forward to configuring and communicating with my Si4455 transceiver chip.

There are loads of esp8266 examples online, that focuses mainly on the use of WiFi library, but very few examples that actually focuses on the use of MCU peripherals, such as the communication protocols like SPI and I2C. And also, I have seen a lot of folks having trouble with using the SPI of the esp8266. In this post, let’s get a hang of the SPI in esp8266 using the Arduino core libraries.

If you haven’t installed the esp8266 package in your Arduino IDE, then please view here, on how to set it up and get started.


Hardware Set Up  and Circuit Diagram

esp8266 spi
Fig 1. NodeMCU(Master) + Arduino UNO(Slave) Hardware Setup and Connection Diagram

The Fig 1. shows the circuit diagram and hardware set up. So make the connections as shown in the above Fig 1.


Precautions

CAUTION:—->  Though the above setup works without any problems, but note that the Arduino UNO works at 5V  logic levels, while the esp8266 works on 3.3 V logic levels. So, In the above circuit, I haven’t used any TTL logic level converters for converting the 5V SPI signals from the Arduino UNO to 3.3V, before passing it to the esp8266. This may blow your esp8266, however, Its a simple fix explained below.

You may be wondering, if this could damage the esp8266, then why did I not take precautions?. The thing is that I did not realize this until I started writing this post, damn!!! , I could have blown my esp8266 o_O. However, from my experience, it may not cause any problems, but still, I’d suggest using a TTL 5V to 3.3V converter to be on the safer side. So, I have warned you and cannot guarantee the safety of your esp8266 :D, you can proceed ahead at your own risk.


Master and Slave Code

The below are the codes for the master and slave which is pretty simple, as far as you have a basic understanding of how SPI works. I have also included comments in the code for clarity.

The master code should be flashed onto the NodeMCU and the slave code should be flashed onto the Arduino UNO.

MASTER Code – NodeMCU:

 

 

SLAVE Code – Arduino UNO:

 

In the above scenario, the master sends a byte with a value ‘2’ every 1 second to the slave. The slave receives and multiplies this byte by ‘2’ and puts the result i.e. 4, back into the SPDR – SPI Data Register. In the next transaction, the master reads this result from the SPDR and displays it on the UART.

You can download the above codes from here as well. The below is a short video that demonstrates the working of the above codes and setup.

This is a very simple example that demonstrates the principle use of SPI on esp8266, however, you can modify it as per your needs. Also, thanks to Nick Gammon, for the detailed and valuable information he has shared in his blog, regarding  Arduino UNO’s SPI  here. I shall keep coming with nice articles just for you, so stay in sync with Deeply Embedded.

4 thoughts on “SPI Transactions between ESP8266 and Arduino UNO”

  1. ISR (SPI_STC_vect) line is showing error
    “expected constructor, destructor, or type conversion before ‘(‘ token”

Leave a Reply

Your email address will not be published. Required fields are marked *