since I had no luck with current sensor and ADC, I bought MCP3201.
So here is config:
Vref = 3.3V
IN+ = ADC reading (currently gnd)
IN- = GND
Vss = GND
Vdd = 3.3VAnd here is sample code:
local MISO = 6 --> GPIO12
local CLK = 5 --> GPIO14
local CS = 8 --> GPIO15
local duration = 3000 --> 3 seconds
local result = 0
gpio.mode(CS, gpio.OUTPUT)
gpio.mode(CLK, gpio.OUTPUT)
gpio.mode(MISO, gpio.INPUT)
-- Function to read SPI
function readSPI()
spi.setup(0, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 100)
result = 0
firstbyte = spi.recv(0, 8)
secondbyte = spi.recv(0, 8)
print (string.byte(firstbyte))
print (string.byte(secondbyte))
end
readSPI()But I always get 255 (firstbyte and secondbyte).
Any suggestion?
Thank you in advance.
PS I know I2C is way easier to use, but there is no ADC I2C chip available in my country right now.