Tech Note 064 - ESP32 How to Connect SPI Devices and Get Them Working

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 83

  • @anandjhaveri7828
    @anandjhaveri7828 2 ปีที่แล้ว +3

    I was struggling to get my new ESP32-C3 board to work with a sensor on the SPI bus for the last few days. I was able to resolve the problem using the SPI.begin(SCK, MISO, MOSI, SS) command to re-assign the MISO pin from the default GPIO9 to another pin on the board. That solved the problem and hours and hours of trial and error!! Thanks a ton!!

  • @PakiNewsNetwork
    @PakiNewsNetwork 4 ปีที่แล้ว +30

    I can understand how much hard work you put into this and give it all to us for free. I'm grateful to you.

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว +3

      tradecycles, thank you

  • @KiteTurbine
    @KiteTurbine 3 ปีที่แล้ว

    3:02 you absolute legend... been ages trying to figure this out

  • @peaps
    @peaps 7 ปีที่แล้ว +9

    Great tip about serial outing the SPI pin aliases, - saved me a lot of time. Thanks.

    • @inkrpen
      @inkrpen 3 ปีที่แล้ว

      Same!

  • @Doile911
    @Doile911 2 ปีที่แล้ว

    Is it possible to communicate between 2 ESP32's via SPI? for some reason i have to use SPI and i cannot use UART (rx, tx).

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว

      You can but it’s not easy to implement. The SPI bus is comprised of a Master and Slaves, the libraries are configured to be the Master so for the other device you’d need to set that up as a Slave, there is provision in the SPI library from Espressif to set up an ESP32 as either master or slave.

    • @Doile911
      @Doile911 2 ปีที่แล้ว

      @@G6EJD Thank you for the quick reply, also it is not possible to make it a duplex SPI right?

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว

      You can have a duplex communication MOSI and MISO are for that, but Von Neumann architectures can only achieve full duplex by switching between processes. Many SPI devices send and receive data. Your challenge is to get the other processor to operate in Slave mode like a sensor would. Not sure why you can’t use I2S or serial as the performance would be close.

  • @vevasam
    @vevasam 3 ปีที่แล้ว +1

    @2:25 SDO is Serial Data Out; SCL is Serial Clock and SDI is Serial Data In. This is irrespective of whether you are using your device as a controller or as a peripheral.
    You could just hook up your controller's SDO to peripheral's SDI and vice-versa.

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      I’m not sure what your saying, I was pointing that MOSI is often called SDO and MISO called SDI

    • @benjaminshields9421
      @benjaminshields9421 ปีที่แล้ว

      @@G6EJD You were saying Slave Data In/Out and Slave Clock when that's not what it is. It's Serial Data In/Out and Serial Clock

    • @G6EJD
      @G6EJD  ปีที่แล้ว

      @@benjaminshields9421 well it’s semantics as the SPI bus has the concept of Master and Slave devices and that is a one-to-many relationship, so there can be many Slaves. When someone is learning it seems best to me to use the term Slave so as not to confuse them with another term.

  • @RTOHIO
    @RTOHIO 4 ปีที่แล้ว

    What library do you use for the test sketch to serial print the parmeters?

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว

      No library, all built in functions.

  • @leythecg
    @leythecg 3 ปีที่แล้ว +2

    Many, many thanks! It was a great help for me to understand the SPI Bus!

  • @henrikostman4474
    @henrikostman4474 5 ปีที่แล้ว

    Is "SS" (chip select) needed if only ONE device is running on the SPI bus? Can't it be hardwired to VCC or GND to be always selected? (trying to save pins)

    • @G6EJD
      @G6EJD  5 ปีที่แล้ว

      Henrik Östman, don’t know, you could try it to find out, most likely if there are issues it’s at startup.

  • @neilcameron4680
    @neilcameron4680 6 ปีที่แล้ว

    An ESP32 DevKitC board works well with an ST7735 TFT screen and the TFT_eSPI library and the board also works well with the VS1053 board and esp_wifi library. However, running SPI and Wifi together results in a problem with SPI as the ST7735 screen whites-out.
    Any suggestions would be very welcome.

    • @G6EJD
      @G6EJD  6 ปีที่แล้ว +1

      There is nothing especially different with the Devkit C variant so I would try using Adafruit's ST7735 library and dropping the TFTeSPI library and is almost certainly what is causing the conflict. Using the in-built (assuming your IDE is current 1.8.10 and 2.4.2 and 1.0) libraries should result in no issues.

  • @FasaniCorp
    @FasaniCorp 6 ปีที่แล้ว +1

    Great David, thanks great explanation! There are also some SPI components like cameras that have additional SDA / SDL pins too, do you know if they also have a constant defined ?

    • @G6EJD
      @G6EJD  6 ปีที่แล้ว

      Martin, no only the pins for the standard I2C bus will be defined, unless a specific camera device is listed in the boards section, but I know of none. It seems like just a need to connect the I2C bus to the camera as well as the SPI connections.

    • @FasaniCorp
      @FasaniCorp 6 ปีที่แล้ว

      Thanks! Here in this sketch : github.com/martinberlin/FS2/tree/board/esp32
      Serial.println(SDA); // Works! But SCL has no constant
      I added the pins like David described. For the Arducam (2MP or 5MP) the SCL is 22 and SDA is 21. If you have any Arducam around try it out. Supported models are: OV2640 / OV5642

  • @ahmetozdemir7173
    @ahmetozdemir7173 2 ปีที่แล้ว

    Hello, the video was good. Is it possible to communicate with esp32, ads1256(SPI), LIS3MDL(I2C) and 20x4 lcd(İ2C) together?
    What kind of connection should I make?

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว +1

      Yes, the ESP32 has an SPI and I2C bus. The I2C devices have separate device addresses so are all wired in parallel and the SPI bus except for Chip Select lines are also wired in parallel and each device has its own CS connection/pin.

    • @ahmetozdemir7173
      @ahmetozdemir7173 2 ปีที่แล้ว

      @@G6EJD David, is it a problem to have peripherals of different resolutions on the i2c line?
      For example, to esp32 microcontroller; Suppose a 24bit ads1256(i2c), 16bit lis3mdl(spi) and 20x4 liquid crystal LCD display are connected. Do you think there is a problem here?

    • @G6EJD
      @G6EJD  2 ปีที่แล้ว +1

      @@ahmetozdemir7173 none whatsoever, the sensor readings are transferred in a scalar way, so if a sensor has a 64-bit reading the request for a reading would ask for 8-bytes (8x8=64) or a 16-but reading just 2-bytes, the libraries take care of all that.

  • @commonmanvoice5055
    @commonmanvoice5055 4 ปีที่แล้ว +1

    Great video!!
    Is there any video for ESP32 and ILI9486/ILI9481?

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว

      You can get a driver from here: www.arduinolibraries.info/libraries/waveshare-ili9486

  • @paulkirchhof9546
    @paulkirchhof9546 3 ปีที่แล้ว

    You are Right SPI is it time consuming a lot. after I connect one TFT 9486 parallel to the ESP32 I want to use the SD-Card reader on that board connected via 5,18,19,23 with a picture demo sketch. Serial.print(SCL) tells me Pin 22 is use instead of 18.
    If I can change it with SPI.begin(18,19,23,5) your video is helpful. SCL=18 have no effect in the Arduino IDE.
    I will choose the names MOSI,MISO,SCL and SS in the Sketch because of the predefines in the most libraries.

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      If you define the pins away from the compiler default the Arduino IDE implements a software emulation of the SPI bus. You can find what’s are used by the compiler by either printing out the key names or by inspecting the hardware folder for the board the file starts win ‘pins’ a parallel SD card drive should work ok, just make sure both the display and SD card reader have separate and unused pins for their Chip Select (CS)

    • @paulkirchhof9546
      @paulkirchhof9546 3 ปีที่แล้ว

      @@G6EJD , thanks for your answer. I have found the issue, in this demo ( read a picture from SD-Card and Transfer to the TFT ) sketch the author forget the SPI.begin() complete.
      After insert the begin statement I can read the SD with Pins 18,19,23,5. I think this SPI is not emulated because of the very fast speed.
      I will compare The same pictures loading from SPIFFS and see how times faster this is.

  • @squidben5780
    @squidben5780 2 ปีที่แล้ว

    Should have seen this video first when I started playing with esp32. Different board different peripheral and library, it's a jungle specially the first time. You think you did something wrong, when it's just the library or something else, or reading documentation for different board.. Great video even in 2022 !!!!

  • @wjn777
    @wjn777 2 ปีที่แล้ว

    Thanks for sharing this, I got hit by this problem a day ago, and this video makes it easy

  • @VideoDetection
    @VideoDetection 7 ปีที่แล้ว

    Interesting video thanks!
    The PIN 5 on the Lolin board also lights the on board led, so every time SS goes low the led will light.
    Might be an interesting effect and useful in debugging, but I cant see any way to disable the led on the board apart from physically disconnecting it.
    Have you found away around this or will you simply use another PIN?

    • @G6EJD
      @G6EJD  7 ปีที่แล้ว +1

      Check out the file in your IDE location, mine is at ArduinoIDE\hardware\espresssif\esp32\variants\lolin32 in there is a file called pins, which you can change so that Slave Select is on another pin, it’s a small design error/bad choice of Wemos to have used pin-5 for the LED unless you like the light show when the SPI devices are being used!

    • @VideoDetection
      @VideoDetection 7 ปีที่แล้ว +1

      Thank you so much!
      I could not find this pin info before when looking thru various files created during compiling. So easy to change when you know how.
      The Wemos Lolin32 is a great little board but its good to know I can change pin 5 so easily. Keep the videos coming I'm learning so much from watching them!

  • @elindifferente4544
    @elindifferente4544 2 ปีที่แล้ว

    Hi David,
    Tried to connect my first SPI device (ePaper display) to an ESP32 board. This one really helped me a lot.
    Thanks!

  • @serverguy2
    @serverguy2 6 ปีที่แล้ว

    Thanks, David. You are the one I turn to these days. I made notes on this! Going to pass them to a friend who has trouble with these pins. Your ePaper WX app is running just fine here, still. Just found out that the ePaper display can do a pretty good grey scale. Never realized it before. Going to update it with those greyscale moon pix with I find them again. Good job!

    • @G6EJD
      @G6EJD  6 ปีที่แล้ว

      Thanks, yes epaper images can be dithered to simulate a grey scale, I must investigate too.

  • @michealmorrow1481
    @michealmorrow1481 3 ปีที่แล้ว

    Thanks for preparing this. This helped me unkink a problem I had but, unfortunately, the ILI9489 still does not work. It was supposed to be in ILI1394 but they sent the wrong display. Have been trying to get it to show for hours. My brain hurts!

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      You need to find a screen driver for the ili9489, but haven’t seen any, the library is your problem

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      I’ve just checked, the ili9489 is an 8-bit parallel interface not an SPI variant so it’s no good

    • @michealmorrow1481
      @michealmorrow1481 3 ปีที่แล้ว +1

      Update: It works. See below for help setting it up.

    • @G6EJD
      @G6EJD  3 ปีที่แล้ว

      @@michealmorrow1481 so did you manage to find an SPI mode in the configuration pins?

    • @michealmorrow1481
      @michealmorrow1481 3 ปีที่แล้ว

      Yes. I am sending you an email now.

  • @hfe1833
    @hfe1833 4 ปีที่แล้ว

    I guess there is hardware spi and software spi in esp32 but not sure I got confused too

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว

      Yes there is a hardware spi bus using specific GPIO pins assigned by espressif but if you want to use other pins then the spi bus gets implemented in software to allow the chosen pins to be used.

  • @jayendrasinhji
    @jayendrasinhji 4 ปีที่แล้ว +1

    it saved my time, thank you david.

  • @markschibilla134
    @markschibilla134 4 ปีที่แล้ว

    Thanks for this! I now have HW SPI working and it's much faster!

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว

      Good to hear, the speed difference over a software implementation is significant. If you use any other pins than the designated hardware ones then Arduino emulates SPI in software, this is why it makes such a difference.

  • @miskimiskiyanto7224
    @miskimiskiyanto7224 5 ปีที่แล้ว

    Thanks, David. But, i have trouble when i want to connect and communicate Wemos LoLin ESP32 with arduino due. I try to connect with I2C and Serial Communication, but this is not work for me. Everyone can help me ? please :)

    • @G6EJD
      @G6EJD  5 ปีที่แล้ว +1

      Miski Lastname, you can’t cross connect the I2C bus but you can with the serial, make sure Tx goes to Rx and Rx to Tx and the baud rate and data format is correct for both devices e.g. both 8N1 meaning 8-bit No parity and 1 stop bit.

    • @miskimiskiyanto7224
      @miskimiskiyanto7224 5 ปีที่แล้ว

      If i use serial communication Wemos lolin esp 32 can receive data but not send data back to arduino due

    • @G6EJD
      @G6EJD  5 ปีที่แล้ว

      So the due is Rx and Tx data ok and the ESP32 is receiving only, in which case I suspect you have the wrong pin for Tx data what pins are you using on the ESP32 for Rx and Tx

    • @miskimiskiyanto7224
      @miskimiskiyanto7224 5 ปีที่แล้ว

      I use the tx pin in wemos connect ro rx pin in arduino due

    • @G6EJD
      @G6EJD  5 ปีที่แล้ว

      Miski Lastname, you cant use the Tx and Rx pins of the wemos they are used for programming the device use software serial to define the pins required

  • @Masudrana-vi7sy
    @Masudrana-vi7sy 2 ปีที่แล้ว

    Great David, thanks great explanation!

  • @AndersWeijnitz
    @AndersWeijnitz 11 หลายเดือนก่อน

    Super useful! Thanks for sharing!

  • @phillipnewman9174
    @phillipnewman9174 3 ปีที่แล้ว

    Thank you for your valuable contribution!

  • @SaulMonteiroYoutube
    @SaulMonteiroYoutube 2 ปีที่แล้ว

    TY

  • @gentikfromct8151
    @gentikfromct8151 4 ปีที่แล้ว

    Big up to you !
    Merci from France

  • @nicholasbennett8664
    @nicholasbennett8664 9 หลายเดือนก่อน

    clears it up, thanks!

  • @weisnerresende
    @weisnerresende 4 ปีที่แล้ว

    From Brazil, Thank you!!

  • @vineetwilson5277
    @vineetwilson5277 4 ปีที่แล้ว +1

    This was very helpful, thanks a lot

  • @juanpablogomezgarcia1835
    @juanpablogomezgarcia1835 3 ปีที่แล้ว

    you saved my live

  • @mrdtw2010
    @mrdtw2010 4 ปีที่แล้ว +1

    Thank you, thank you, thank you.

  • @michealmorrow1481
    @michealmorrow1481 3 ปีที่แล้ว

    The information for the SPI version of the ILI9486 is here: th-cam.com/video/rq5yPJbX_uk/w-d-xo.html

  • @navjotsinghDhanoa
    @navjotsinghDhanoa 5 ปีที่แล้ว

    Thanks, SPI and HSPI take so much time to understand, This video made the process of understanding SPI on esp32 a lil bit simpler ^._.^

    • @G6EJD
      @G6EJD  5 ปีที่แล้ว

      There is no difference between SPI and HSPI both run at the same speed, I think what’s caused this confusion is Espressif’s use of the H and V port designators. Both ports are identical and the SPI specification is what it is, there is no specification for a High speed version.

    • @enricorodella6952
      @enricorodella6952 4 ปีที่แล้ว

      @@G6EJD How is possible to use HSPI for the SdFat library and the VSPI for an ADC (MCP3208) at the same time?
      I can work with both of them independently, but I can't merge them.
      Cheers!

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว

      You can’t merge them and I assume you have two I dependent circuits for each SPI bus which includes a CS for each device. Are you sure you are creating two instances of the SPI I stance spi1.begin and spi2.begin and using the correct identifier when reading each bus? I have not seen any libraries that allow more than one instance to be created!

    • @G6EJD
      @G6EJD  4 ปีที่แล้ว

      You can easily use both the ADC and SD card on the same SPI bus, the CS for each device determines which device is active

    • @enricorodella6952
      @enricorodella6952 4 ปีที่แล้ว

      @@G6EJD Yes, I have two independent SPI connections and two CS pins.
      I tried to follow different topics about two instances, but it doesn't work. Is there some clear example somewhere? I'll give a try with SD.h, but is slower than SdFat.h

  • @natorverinumbe6511
    @natorverinumbe6511 4 ปีที่แล้ว

    Thanks a trillion.
    I really appreciate.

  • @imranaalam
    @imranaalam 5 ปีที่แล้ว +1

    thanks. you saved my day