I2C registers - working with the K33 CO2 Sensor library

I need to porting the Library for the K33 CO2 Sensor written by KinaSmith to the Electron.

This library works great with our Arduino based dataloggers

I think I’m close, but my problem is rewriting a function in Co2Meter_K33.cpp that includes a function called "wakeSensor().

I need some advice on how to repeat this I2C maneuver in the Electron. I did some research on this but I could use some pointers.

Thanks in advance.

///////////////////////////////////////////////////////////////////
// Function : void wakeSensor()
// Executes : Sends wakeup commands to K33 sensors.
// Note : THIS COMMAND MUST BE MODIFIED FOR THE SPECIFIC AVR YOU ARE USING
// THE REGISTERS ARE HARD‐CODED
/////////////////////////////////////////////////////////////////
void Co2Meter_K33::wakeSensor() {
	// This command serves as a wakeup to the CO2 sensor, for K33‐ELG/BLG Sensors Only
	// You'll have the look up the registers for your specific device, but the idea here is simple:
	// 1. Disabled the I2C engine on the AVR
	// 2. Set the Data Direction register to output on the SDA line
	// 3. Toggle the line low for ~1ms to wake the micro up. Enable I2C Engine
	// 4. Wake a millisecond.


	TWCR &= ~(1 << 2); // Disable I2C Engine
	DDRC |= (1 << 4); // Set pin to output mode
	PORTC &= ~(1 << 4); // Pull pin low
	delay(1);
	PORTC |= (1 << 4); // Pull pin high again
	TWCR |= (1 << 2); // I2C is now enabled
	delay(1);
}

@micromet, take a look at Wire.end() and Wire.begin() in the Particle docs. Without testing, the arduino replamement code would look like this:

  Wire.end();  // Turn off the I2C bus so GPIO (pins D0 and D1) can be used
  pinMode(D0, OUTPUT);  // Set SDA (D0) to output
  digitalWrite(D0, LOW);    // Toggle the pin low for 1ms then HIGH again
  delay(1);
  digitalWrite(D0, HIGH);
  Wire.begin(devAddr);  // restart the I2C bus with the device's address

:smile:

2 Likes

Thanks for the pointer - I’ll study and give this a shot - report back.

1 Like

It worked, thanks.
Jjust had to change

Wire.begin(devAddr);

to

Wire.begin();

I also removed the 10K pull up resistors on D0 and D1 that I had tied to 3V

2 Likes

That’s a nice expensive sensor :smiley: How are you putting it to use if you don’t mind me asking?

We are using it at a research site where the soil and groundwater have been contaminated with hydrocarbons (old gas refinery). We cover the soil surface with plastic or a chamber and monitor the buildup of CO2 in the headspace as a measure of underground microbial respiration - a proxy for the hydrocarbon degradation rate.

Interesting!

That’s a very good reason for such an expensive sensor :smiley:

Share your project if you’re willing, it sounds interesting.