Current Lua downloadable firmware will be posted here

User avatar
By kyote00
#25798 Hi all
Having some trouble with a sparkfun i2c accelerometer....https://www.sparkfun.com/products/12756

My setup is as follows:
- ESP12 board, good quality 3.3v regulator.
- running standard nodemcu firmware from (http://frightanic.com/nodemcu-custom-build/)
- all other functions (wifi etc) work fine....

When I run an i2c scanner code (lua code), It appears to find the i2c accelerometer as I see
"device found at address 0x1D on IO Index 6 and 7" (this is the way its connected)

The problem is that when I try to get the init() function, I do not get the valid device id back ... always 255


Lua Library:
Code: Select alllocal moduleName = ...
local M = {}
_G[moduleName] = M

-- i2c interface ID
local id = 0

local ADDR = 0x1C --i2c address


-- MMA registers
local MMA_STATUS = 0x00
local MMA_WHOAMI = 0x0D
local MMA_SYSMOD = 0x0B
local MMA_CTRL_R1 = 0x2a
local MMA_CTRL_R2 = 0x2b

-- Wrapping I2C functions to retain original calls
local Wire = {}
function Wire.beginTransmission(ADDR)
    i2c.start(id)
    i2c.address(id, ADDR, i2c.TRANSMITTER)
end

function Wire.write(commands)
    i2c.write(id, commands)
end

function Wire.endTransmission()
    i2c.stop(id)
end

function Wire.requestFrom(ADDR, length)
    i2c.start(id)
    i2c.address(id, ADDR,i2c.RECEIVER)
   tmr.delay(20000)
    c = i2c.read(id, length)
  -- i2c.stop(id)
    return string.byte(c)
end

local function readRegister(deviceAddress, address)
     Wire.beginTransmission(deviceAddress)
     Wire.write(address)                -- register to read
     Wire.endTransmission()
     value = Wire.requestFrom(deviceAddress, 1) -- read a byte
     return value
end

local function writeRegister(deviceAddress, address, val)
     Wire.beginTransmission(deviceAddress)  -- start transmission to device
     Wire.write(address)                    -- send register address
     Wire.write(val)                        -- send value to write
     Wire.endTransmission()                 -- end transmission
end

function setScale()
end
function setODR()
end
function setPL()
end
function setTap()
end


function standby()
local standbyMask =  0x01
   retv = readRegister(ADDR,MMA_CTRL_R1)
   writeRegister(ADDR,MMA_CTRL_R1,bit.band(retv,bit.bnot(standbyMask)))
end

function active()
local activeMask =  0x01
   retv = readRegister(ADDR,MMA_CTRL_R1)
   writeRegister(ADDR,MMA_CTRL_R1,bit.bor(retv,activeMask))
end

function M.init(sda, scl)
   i2c.setup(id, sda, scl, i2c.SLOW)
   retval = readRegister(ADDR,MMA_WHOAMI);
   print("WHOAMI:" .. retval)
   
  standby()
  setScale()
  setODR()
  setPL()
  setTap()
  active()
  print("init done, moving to active mode")
end

function M.available()
retval = readRegister(ADDR,MMA_STATUS)
print("available:"..retval)
return retval
end

return M


Test Script:
Code: Select allSDA_PIN = 6
SCL_PIN = 7

mmaLib = require("mmalib")
mmaLib.init(SDA_PIN, SCL_PIN)

mmaLib.available()

mmaLib = nil
package.loaded["mmalib"]=nil
User avatar
By kyote00
#25966 Ok, fixed. it this works - for MMA8452 accelerometer...

Only difference is the i2c.stop is commented out....

I will post the full library code when I done

Code: Select alli2c.setup(0,6,7,i2c.SLOW)
i2c.start(id)
i2c.address(id, ADDR, i2c.TRANSMITTER)
i2c.write(id, 0x0D)
--i2c.stop(id)
i2c.start(id)
i2c.address(id, ADDR,i2c.RECEIVER)
c = i2c.read(id, 1)
i2c.stop(id)
print("WHOAMI:"..string.byte(c))