NCIR, i2c_bus and unit Updated micropython code



  • Hello!
    Is there any way to share the updated ncir unit micropython code? I really need it, the one in github is not updated.
    Thank you!
    Tiago



  • Also the unit code and updated i2c_bus would be highly appreciated!
    I am making a PoC using micropython 1.12 and some external libs, but I can't proceed if I don't have the m5stack libraries that I need!
    Please support us so that in our turn we can support you!



  • @tialm said in NCIR, i2c_bus and unit Updated micropython code:

    Also the unit code and updated i2c_bus would be highly appreciated!
    I am making a PoC using micropython 1.12 and some external libs, but I can't proceed if I don't have the m5stack libraries that I need!
    Please support us so that in our turn we can support you!

    I managed to use the NCIR unit (M5 product) connected to an ESP8266 developing board using micropython 1.12. Sorry for the belated response, hopefully still useful. The NCIR unit uses mlx90614 semiconductor chip, and I provide below the python library for it. The main program which calls the library for testing also provided below. Good luck.



  • @tialm
    Here is the NCIR library:

    import ustruct
    
    _REGISTER_TA = const(0x06)     # ambient
    _REGISTER_TOBJ1 = const(0x07)  # object
    _REGISTER_TOBJ2 = const(0x08)  # object2
    
    class MLX90614:
    	def __init__(self, i2c, address=0x5a):
    		self.i2c = i2c
    		self.address = address
    		_config1 = i2c.readfrom_mem(address, 0x25, 2)
    		_dz = ustruct.unpack('<H', _config1)[0] & (1<<6)
    		self.dual_zone = True if _dz else False
    
    	def read16(self, register):
    		data = self.i2c.readfrom_mem(self.address, register, 2)
    		return ustruct.unpack('<H', data)[0]
    
    	def read_temp(self, register):
    		temp = self.read16(register);
    		# apply measurement resolution (0.02 degrees per LSB)
    		temp *= .02;
    		# Kelvin to Celcius
    		temp -= 273.15;
    		return temp;
    
    	def read_ambient_temp(self):
    		return self.read_temp(_REGISTER_TA)
    
    	def read_object_temp(self):
    		return self.read_temp(_REGISTER_TOBJ1)
    
    	def read_object2_temp(self):
    		if self.dual_zone:
    			return self.read_temp(_REGISTER_TOBJ2)
    		else:
    			raise RuntimeError("Device only has one thermopile")
    
    	@property
    	def ambient_temp(self):
    		return self.read_ambient_temp()
    
    	@property
    	def object_temp(self):
    		return self.read_object_temp()
    
    	@property
    	def object2_temp(self):
    		return self.read_object2_temp()
    


  • @tialm Here is the main program which calls the unit:

    import time
    import mlx90614
    from machine import I2C, Pin
    
    i2c = I2C(scl=Pin(5), sda=Pin(4))
    sensor = mlx90614.MLX90614(i2c)
    
    print("Ambient (C)", sensor.read_ambient_temp())
    print("Object  (C)", sensor.read_object_temp())
    
    if sensor.dual_zone:
        print("Dual zone")
        print(sensor.object2_temp)
    else:
        print("Single zone")
    print("Continuous sampling")
    while True:
        print(sensor.read_ambient_temp(), sensor.read_object_temp())
        time.sleep_ms(500)
    


  • @tialm I do not know your development board but you should check the pins assignment for correct operation. Please change the scl and sda pins properly:

    i2c = I2C(scl=Pin(5), sda=Pin(4))
    

    I have to say that I quoted many parts of the codes from other resources but I could not remember the resources anymore. Sorry about that. This is not all my creation.



  • Thank you for your help @liemph ! I ended up doing something similar. I have only now seen your answer. But for sure that will be helpful to someone!