A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By Rahul K
#42878 I have been looking for an example on SPI based on LUA to interface my MAX6675 thermocouple. But found some examples based on arduino :(. So I decided to write it on my own :geek: . Since nodemcu's SPI library uses GPIO15 as Chip Select, it would be real pain to control the Chip Select by yourself and it completely defeats the automation purpose.
So here's the code. I hope someone may find this useful.

Code: Select all--
Config
local MISO = 5            --> GPIO14
local CLK = 6             --> GPIO12
local CS = 7              --> GPIO13
local duration = 3000     --> 3 seconds
local i = 0
local result = 0

-- Pin Initialization
gpio.mode(CS, gpio.OUTPUT)
gpio.mode(CLK, gpio.OUTPUT)
gpio.mode(MISO, gpio.INPUT)
gpio.write(CS,gpio.HIGH)

-- Function to read SPI
function readSPI()
    gpio.write(CS, gpio.LOW)      -->Activate the chip
    tmr.delay(1)                  -->1us Delay

    gpio.write(CLK, gpio.HIGH)    -->First bit is dummy, ignore it(refer MAX6675 datasheet)
    tmr.delay(2)
    gpio.write(CLK, gpio.LOW)
    tmr.delay(2)

    result = 0
   
   for i=15,1,-1
    do
      result = bit.lshift(result, 1)
      result = bit.bor(result ,(bit.band(gpio.read(MISO),0x01)))
      gpio.write(CLK, gpio.HIGH)   
      tmr.delay(2)
      gpio.write(CLK, gpio.LOW)
      tmr.delay(2)
    end
    if(bit.isset(result,2)) then                      -- refer MAX6675 Datasheet
        print("Sensor not connected")
        gpio.write(CS, gpio.HIGH)
        do return end
    end
       
    gpio.write(CS, gpio.HIGH)
    print(bit.rshift(result,3)*0.25)

end

-- Create an interval
tmr.alarm(0, duration, 1, readSPI)