How to Use SPI in LabVIEW

Introduction: How to Use SPI in LabVIEW

About: Software Engineer at Digilent Inc.

This Instructable will explain how to communicate with an ambient light sensor Pmod (PmodALS) via SPI using chipKIT WF32, LabVIEW, and LabVIEW MakerHub LINX. This will also go over how to read the data sheet so you can use this as a guide to get other SPI devices working.

This project can be completed with the LabVIEW Physical Computing Kit from Digilent which includes chipKIT WF32 and LabVIEW Home Bundle.

Step 1: Materials

1) LabVIEW

LabVIEW Home Bundle can be used for this project and is sold by Digilent. Otherwise, any LabVIEW version can be used including the free trial.

2) chipKIT WF32

3) LabVIEW MakerHub LINX

Installation is free and in depth instructions and video guides can be found here to learn how to install and use LINX. Just click "Getting Started."

4) USB A to mini B USB cable

5) PmodALS

Step 2: Install Software

If you have installed LabVIEW, chipKIT WF32 drivers, and LabVIEW MakerHub LINX already, you can skip this step.

First, install LabVIEW onto your computer.

Next, install NI VISA here if you did not include it in your LabVIEW installation. Once that is completed, install LabVIEW MakerHub LINX here by clicking on download now from the attached page. A detailed installation instruction video can be found by clicking on "Getting Started."

With all the required software installed, connect chipKIT WF32 to your PC using a USB A to mini B USB cable. Windows will most likely install the device drivers automatically. To make sure, open the device manager from the control panel and expand the Ports section. The COM port is the chipKIT WF32 and if the drivers were not installed correctly, a yellow exclamation mark will appear over the device.

If the drivers were not installed properly, download the drivers for your device which are included as part of MPIDE found at Once the drivers are installed, right click on the COM port for the chipKIT WF32 and click properties then click on port settings and choose advanced. Under the BM options, change the latency timer to 1 ms.

Now, launch LabVIEW and click tools --> MakerHub --> LINX --> LINX Firmware Wizard to deploy the LINX firmware to the chipKIT. Choose Digilent from device family and choose chipKIT WF32 from device type and click next (pictured above). Choose the COM port that the WF32 is connected to. Click next and then choose LINX serial/USB and click next again. The firmware will then be transferred to the WF32 and the on-board lights will flash.

Again, if you're having trouble with these steps, check out the guide on LabVIEW MakerHub for step-by-step video instructions.

Step 3: ChipKIT WF32 and PmodALS Setup

The next step is to connect PmodALS to chipKIT WF32. Connect CS on the PmodALS to channel 10 on chipKIT WF32. Connect SDO to channel 12, connect SCK to channel 13, connect GND to ground, and connect VCC to 3.3V on the WF32 (pictured above). These are the default channels for SPI on chipKIT WF32. Other SPI channels can be found by looking at the reference manual.

Make sure that the jumpers on JP6 and JP7 are placed in the master position (pictured above) so chipKIT WF32 acts as a master to the Pmod.

Step 4: LabVIEW Code

To code for the SPI specifications, the CS channel, SPI channel, serial clock frequency, how many bytes are passed, where the bits of information are placed, MSB or LSB first, and whether it is active high or active low are needed.

From the PmodALS reference sheet, it is placed in normal mode by bringing the CS pin low (active low), the serial clock is between 1 and 4 MHz, bits are placed on the falling edge of the SCLK and valid on the rising edge (Mode 0), and there are 16 SCLK clock cycles which means 16 bits (2 bytes) are transferred.

The CS channel was placed on pin 10 in our case. SPI channel is 0 since this is the default configuration. Different SPI configurations will give a different SPI channel based on the information in the data sheet for the WF32.

Step 5: Information Processing

Although there is no master out slave in connection, 2 bytes must still be transferred so that 2 bytes can be received from the slave. After reading the 2 bytes, each byte is placed into an entry in a 1D array so the two bytes are added together by splicing the two entries together. The array subset block is used to skip the first 4 bits and then the next 8 bits are placed into a new array.

This 8-bit integer can range from 0 to 255 based on the light level. Since there are no units for this value, the value is represented as a percent of 255. A reading of 0 is no light sensed and a reading of 255 is the maximum light the PmodALS can sense.

Step 6: Download and Run

Download the VI below and open it. On the front panel, make sure the CS channel is 10, the serial port you found for the WF32 is selected, and the SPI channel is set to 0.

Run the VI and you're done! Feel free to let me know if you have any questions.

Be the First to Share


    • Crafts For Kids Challenge

      Crafts For Kids Challenge
    • Organization Challenge

      Organization Challenge
    • Make it Glow Contest

      Make it Glow Contest



    7 years ago


    Few questions

    1. In your labview prog, where do you tell LINX which pin (of ChipKit) is data and which pin is clock?

    2. Can you please explain the data bundle inside the loop? why its needed.

    3. What is the function of "SPI channel" - its not the data line, right?

    Sudharsan Sukumar
    Sudharsan Sukumar

    Reply 7 years ago

    1 and 3. So the pins that correspond to the SPI connections (CS, MOSI, MISO, and SCK) are determined by the board. This also has to do with question 3 where the SPI channel refers to the specific SPI configuration that you're using. SPI0 is the default SPI configuration for that board. In the case with the WF32, SPI0 uses channels 10-13 for the SPI data. If we were using one of the other SPI connections on the board say (SPI2), we'd have to look at the reference manual and see what channels are used for SPI2 (though if you look closely we have to use the DSPI library to use SPI2 which the LINX firmware does not use but that doesn't really matter right now). So to answer your question, LINX knows what channels because of your question 3 where you say that you are using SPI0 which corresponds to pins 10-13 on the board based on the its reference manual.

    2. I'm not sure what you mean by data bundle inside the loop? If you mean why does the data get returned in an array, that has to do with the way SPI works. Once we get the data via SPI we then have to convert that data back to useful information. I'd be happy to help better answer your question if you give a little bit more details on what you mean.


    Reply 7 years ago

    That really helpful. Thanks a lot.

    About my 2nd question in previous post, I have uploaded an image.

    Sudharsan Sukumar
    Sudharsan Sukumar

    Reply 7 years ago

    Oh so that is because I was packaging up this code into subVIs after making this Instructable so that other uses can read from this Pmod easily. What that is doing is taking the cluster that includes the CS channel and the active logic level at the very top left of your picture and then overwriting the CS channel with whatever the user selects as their CS channel. This is useful so that others can select the CS channel they are using but they don't need to know if it is active high or active low in order to get a reading.

    For this code, it actually doesn't need to be in the while loop and can be outside but it was necessary to have it in the while loop when packaging it into a subVI.

    If you were making your own code, you would just enter the correct values in the cluster and you wouldn't have to do the CS overwrite that I did.