The objective of this post is to explain how to transfer data between the ESP8266 and the SN74HC595 using the SPI interface.
Introduction
The objective of this post is to explain how to transfer data between the ESP8266 and the SN74HC595 using the SPI interface. An explanation on how to control some LEDs using this method is also included.
We assume the use of the ESP8266 libraries for the Arduino IDE. You can check here how to configure the Arduino IDE to support the ESP8266.
Although this tutorial was tested using the SN74HC595 from Texas Instruments (datasheet here), it’s expected to work with versions of the 74HC595 from other manufacturers.
You can check a great article about the SPI interface here.
Hardware
The schematic for controlling the SN74HC595 via SPI is shown in figure 1. It’s equal to the one shown in the previous post about controlling the device using the shiftOut function, except for the fact that now we are using the SPI pins of the ESP8266.
Figure 1 – Connection diagram between the ESP8266 and the SN74HC595, for data transfering using SPI.
You can check the mapping of the SPI pins in the ESP8266 on this article. You can read a more detailed explanation about the connection between the SN74HC595 and the ESP8266 in this previous post.
Setup
In this case, we need to include the SPI library, which provides easy to use functions to interact with devices using this interface. You can check the implementation of this library for the ESP8266 here.
#include <SPI.h>
Since we are using a GPIO pin for as Slave Select (SS), we declare it in a global variable.
const int SSpin = 15;
Finally, in the setup function, we define the SS pin as an output pin and we initialize the SPI bus with the begin method.
void setup() { pinMode(SSpin, OUTPUT); SPI.begin(); }
Note: The SPI clock is initialized with a value of 1 MHz, as can be seen here in the implementation of the begin method. Nevertheless, the frequency can be changed with the setFrequency method.
Main loop
The main loop function is actually pretty simple and the code is similar to the one in the previous post.
First of all, we set the SS pin to LOW, in order to avoid internal transference of data between the shift register and the storage register of the SN74HC595 while we are sending the 8 bits from the microcontroller to the integrated circuit.
digitalWrite(SSpin, LOW);
Then, we use the transfer method of the SPI library to send the actual data to the device.
int valueToSend=1; SPI.transfer(valueToSend);
Finally, we set the SS pin to HIGH, so the 8 bits we transferred to the internal shift register of the SN74HC595 are propagated to the storage register and thus become accessible in pins QA through QH.
digitalWrite(SSpin, HIGH);
Check the final main loop function bellow, where we do a for loop to set all the possible combinations of LEDs on/off.
void loop() { for (int i= 0; i<=7; i++){ digitalWrite(SSpin, LOW); //Disable any internal transference in the SN74HC595 SPI.transfer(i); //Transfer data to the SN74HC595 digitalWrite(SSpin, HIGH); //Start the internal data transference in the SN74HC595 delay(3000); //Wait for next iteration } }
The final result is shown in the video below.
Related Posts
- SN74HC595: Shift Register
- ESP8266: Connection to SN74HC595
- ESP8266: Controlling chained SN74HC595 ICs
Resources
Technical details
- ESP8266 libraries: v2.3.0.
Pingback: ESP8266: Controlling chained SN74HC595 ICs | techtutorialsx
Pingback: ESP8266: Controlling chained SN74HC595 ICs | techtutorialsx
Pingback: ESP8266: Connection to SN74HC595 | techtutorialsx
Pingback: ESP8266: Connection to SN74HC595 | techtutorialsx
Pingback: SN74HC595: Shift Register | techtutorialsx
Pingback: SN74HC595: Shift Register | techtutorialsx
Pingback: ESP8266: Controlling a LED matrix with the 74HC595 ICs | techtutorialsx
Pingback: ESP8266: Controlling a LED matrix with the 74HC595 ICs | techtutorialsx