Post your best Lua script examples here

User avatar
By roccomuso
#30611 I'm doing some tests with a MPU6050 accelerometer but the accelerometer and temperature registers return 0 (I have not tested the gyroscope registers). When this happens I still can read the correct values out of the Power Management 1 register (it gives me value 64) and the Who Am I register (I have not tried others - it gives me value 104).

Do I need some king of inizialitazione function?


On the initialization function I'm resetting the MPU6050 by setting the appropriate bit on the Power Management 1 register and waiting for it to go back to 0 then I'm writing 0x00 to this same register in order to clear the SLEEP bit.

This is the second time I'm trying to work with this accelerometer and both time I faced this problem. The problem shouldn't be the accelerometer itself because I have a friend running the same code on another accelerometer with the same results.

Moreover with arduino and this code: http://playground.arduino.cc/Main/MPU-6050
The mpu6050 works great, but with NodeMCU devkit v1.0 and Lua firmware it doesn't.

I'm out of ideas. :|

Here's the code I'm using to read the temperature registers according to the MPU6050 registers map ( https://www.olimex.com/Products/Modules ... _rev_4.pdf ) :

Code: Select all-- Reading MPU6050 temperature (65 & 66 registers) value through I2C

id=0 -- bus ID
sda=5 -- D5
scl=6 -- D6

mpu6050_addr=104

i2c.setup(id,sda,scl,i2c.SLOW)

-- I'm not using write_reg
function write_reg(dev_addr, reg_addr, reg_val)
    i2c.start(id)
    i2c.address(id, dev_addr, i2c.TRANSMITTER)
    i2c.write(id, reg_addr)
    i2c.write(id, reg_val)
    i2c.stop(id)
end

--write_reg(mpu6050_addr, 107, 0)

 
function read_reg(dev_addr, reg_addr)
  -- WRITING
  i2c.start(id)
  i2c.address(id, dev_addr, i2c.TRANSMITTER)
  i2c.write(id, reg_addr) -- writing reg_addr in dev_addr
  i2c.stop(id)
  -- READING
  i2c.start(id)
  i2c.address(id, dev_addr, i2c.RECEIVER)
  c=i2c.read(id, 1) -- reading dev_addr (1 byte)
  i2c.stop(id)
  return c
end

x=string.byte(read_reg(mpu6050_addr, 65))
y=string.byte(read_reg(mpu6050_addr, 66))
sleep_reg=string.byte(read_reg(mpu6050_addr, 107)) -- gives me 64 = 1000000
print(sleep_reg .." ".." Temperature: " .. x .. y)