Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.

Please help with bit banging a CO2 sensor using Arduino
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Please help with bit banging a CO2 sensor using Arduino

by daemach on Fri Nov 26, 2010 7:34 pm

Hello - I recently discovered a self-calibrating CO2 sensor that uses an interface similar to I2C: They sent me a couple of samples and said the price would be ~$58 in quantities of 5-10K. The sensor returns temperature, relative humidity and a CO2 measurement (apparently), and possibly air velocity too, though the documentation seems to be conflicting on that one.

The datasheet for the E2 interface, (their protocol), has code for interfacing with the processor that must be modified to work with specific processors: (see the section that says "// adapt this code for your target processor !!!")

Do you use digitalwrite commands to do the bitbanging or is there a better way? I haven't done any programming for a long time so any sample code would be greatly appreciated.

Posts: 143
Joined: Fri Feb 13, 2009 2:07 pm

Re: Please help with bit banging a CO2 sensor using Arduino

by zener on Fri Nov 26, 2010 10:46 pm

You might be better off in the Arduino forum since you want to use an Arduino. The code they have there is C. I think you can compile that pretty well as Arduino code. The last few lines are where you need to put in your pinMode(), digitalWrite() and digitalRead(). Basically, you can set a pin high or low or read its state. Starting here:

Look in the upper right under Functions, Digital I/O

Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Please help with bit banging a CO2 sensor using Arduino

by chatham on Mon Nov 29, 2010 5:07 am

Sooo, I would recommend you actually say to heck with the Arduino read/write functions and start accessing the I/O ports directly. Mainly since if you're going to get started with bit-banging, depending on how timing-critical the device you're trying to read is, digitalRead() and digitalWrite() may be too slow, and at the least, they can get in the way of timing.
In summary, all the pins on an Arduino are on one of 3 8-bit 'PORT's, PORTB, PORTC, and PORTD. Each pin is represented by a specific bit on a specific port - for example, Analog 4 and 5 are actually PORTC.4 and PORTC.5. How to mess with the pins is as follows:
pinMode() = DDRB, DDRC, and DDRD. Change the appropriate bit for whichever pin, where 0 is input, and 1 is output.
Code: Select all | TOGGLE FULL SIZE
DDRB = 0b00001111;  // This is setting whatever pins on PORTB 0-3 to output, and the rest to input.

digitalWrite() = PORTB, PORTC, and PORTD. Again, each bit in each port changes a different pin
Code: Select all | TOGGLE FULL SIZE
PORTB = 0b00111100; /*Following the previous line of code, this set pins PORTB 0 and 1 to output low, PORTB 2 and 3 to output high, PORTB 4 and 5 to input with the internal pull-up set, and PORTB 6 and 7 to input without an internal pullup.*/             

digitalRead() = PINB, PINC, and PIND. Each of these registers will return what state all the pins on a given port are at, regardless of whether or not they're declared as 'input' pins. So, assuming that PORTB 6 and 7 are being pulled down,
Code: Select all | TOGGLE FULL SIZE
 int x = PINB;  // This will make x == 0b00111100

Look up bitwise operators and bit masking for details on how to take those 8-bit registers and access single bits (pins in this case) at a time. There's good explanation of them on the Arduino reference home. For example, to just set the 5th bit on PORTC to HIGH, we'd do
Code: Select all | TOGGLE FULL SIZE
PORTC |= 1<<5;

Also, have you looked into trying to use the Wire library? It really depends on whether or not the E2 protocol is I2C/TWI compatible, which it sounds like there is a chance that it is? It uses the hardware TWI support that the ATmega328 has, so you might check out the ATmega48/88/168/328 datasheet on that. Otherwise, you might search for Arduino I2C or TWI bitbang master and work from there; I know there are a couple solutions out there, but tweaking them may be fairly in-depth... - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/PS3/USB controllers.

Posts: 144
Joined: Thu Feb 04, 2010 2:30 am

Re: Please help with bit banging a CO2 sensor using Arduino

by stinkbutt on Mon Nov 29, 2010 11:52 am

I don't want to meet your mom, I just want, BITBANG BANG!
Red M&M, Blue M&M: They all wind up the same color

Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Please be positive and constructive with your questions and comments.