An archive of as of Saturday January 26, 2019.

Error with i2c with HTU21D sensor library


I have the following really small snippet...



// Create an I2C instance
i2c <- I2C(0);

// Set speed to 400 kHz

// Set address

local htu21d = HTU21D(i2c, 0x40);

local temperature;
local humidity;

temperature = htu21d.readTemp();
humidity = htu21d.readHumidity();

print("temp=" + temperature + "\n");
print("humid=" + humidity + "\n");

It runs and produces reasonable results the first time. However subsequent attempts to run it result in:

%%Runtime error [unable to open I2C device]

*FUNCTION [main()] sd:/htu21d_demo.nut line [6]

I'm sure I'm just doing something stupid...


Weird, I just noticed if I hit "X" and then run it again it works... Hmmm... I'm obviously missing something here. Do I need to terminate the program or something when it is done?


I've had problems with that error, seems if I start and stop, then start again I get the error but once I start a i2c program if I comment out the I2c line when I run it again it works fine. Like it isn't letting go or unloading from memory or something, don't know.

Usually only a problem when I'm testing and running and stopping then running again after making changes.




I've just gotten in the habit of hitting the X to stop the nut after each run, then it always works next time I run it. It is a little annoying but I'm getting used to it.