As the title says... Chat on...

User avatar
By fominraos
#59360 I do the wireless device of measurement for a magnetic field based on the magneto-resistive sensor HMC5983 and ESP8266(NodeMCU ESP-12e module).

The sensor is connected to ESP8266 on the I2C interface. ESP8266 polls the sensor and sends this on data collector (RaspberryPi).

It is extremely important to me to achieve the greatest possible number of computation in a second as quality of the obtained data for later processing depends on it.

HMC5983 supports the I2C interface in the Standart, Fast and High-speed mode. But NodeMCU I2C Module support only i2c.SLOW speed

common I²C bus speeds are the 100 kbit/s standard mode and the 10 kbit/s low-speed mode https://en.wikipedia.org/wiki/I%C2%B2C


Then I connected HMC5983 directly to RaspberryPi via I2C I could achieve about 500 measurement per second (by monitoring DRDY interput pin) in single-measurement mode and 200 measurement per second in continuos-measurement mode (with Data Output Rate at 220 Hz - all rigth). The programm was written on Python, her its code:
Code: Select all#!/usr/bin/python
import smbus #for i2c use
import time
import os

bus = smbus.SMBus(1) #use i2c port 1 on Rasspberry Pi
addr = 0x1e #address HMC5983 0x1E

bus.write_byte_data(addr,0x00,0b00011100) #Write to CRA Speed 220Hz
bus.write_byte_data(addr,0x01,0b00100000) #Write to CRB Gain 660 +-2.5Ga 1.52mG/Lsb

print "Start measuring.....
while True: #if we need infinity cycle
    bus.write_byte_data(addr,0x02,0b00000001) #Write to Mode single-measurement mode
    while bus.read_byte_data(addr,0x09) == 0b11: #Wait RDY in Status Register
       ()
    #DATA READY   
    data = bus.read_i2c_block_data(addr,0x03,6)#Take data from data registers
    #convert three 16-bit 2`s compliment hex value to dec values and assign x,y,z
    x = data[0]*256+data[1]
    if x > 32767 :
        x -= 65536
    y = data[2]*256+data[3]
    if y > 32767 :
        y -= 65536
    z = data[4]*256+data[5]
    if z > 32767 :
        z -= 65536 

    print "X=",x, "\tY=",y, "\tZ=",z

When I connected HMC5983 to ESP8266, I could achieve only about 140 computation a second in the mode of single computation...
Code: Select all----------THIS IS FOR SINGLE-MEASUREMENT MODE-------------
--init i2c
function H_init(sda,scl)
    i2c.setup(id, sda, scl, i2c.SLOW)
    print("I2C started...")
end

-- reads 6byte from the sensor
function read_axis()
    i2c.start(id)
    i2c.address(id, dev_addr, i2c.RECEIVER)
    data = i2c.read(id, 6)
    i2c.stop(id)
    return data
end
--set register
function set_reg(reg_addr,val)
    i2c.start(id)
    i2c.address(id, dev_addr, i2c.TRANSMITTER)
    i2c.write(id,reg_addr)
    i2c.write(id,val)
    i2c.stop(id)
end

--------GPIO INITILIZATION-------
drdyn_pin=3
gpio.mode(drdyn_pin, gpio.INPUT)

-------I2C INITILIZATION-------
id = 0
i2c = i2c
local i=0
dev_addr = 0x1e

H_init(1,2)

set_reg(0x00,0x1c) --set speed 220Hz
set_reg(0x01,0x20) --set gain

print("Start measurement...")
while true do
    set_reg(0x02,0x01) --single-measurement mode
    while(gpio.read(drdyn_pin) == 1) do
    end
    data = read_axis()
    tmr.wdclr()
end


After that I configured the sensor in the continuos-measurement mode and received the same 200 measurement per second.

Whether operation of the I2C interface in NodeMCU at high speeds is possible? Somebody can offer how to try to accelerate inquiry of the sensor?
User avatar
By marcelstoer
#83420 There's a new I2C driver currently available on the dev branch thanks to https://github.com/nodemcu/nodemcu-firmware/pull/2465. It will soon land on master but can't assess if it would make any difference for your case.