Problems programming an arduino for a CO gas sensor

I have a CO gas sensor module that is a digital sensor. I am trying to program my arduino uno to give 5V to one of the module pins for 60 seconds, but there is something wrong with my code. Can someone please help me? This is my first time programming the Arduino. I'm including some code that came with the module (but it's not in Arduino language), and I am including my code. Could someone look at it?

CO sensor module code: ' ========================================================================= ' ' File...... CO Gas Sensor.bs2 ' Purpose... Runs the CO Gas Sensor Module Heater ' Author.... Parallax, Inc. ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ========================================================================= ' -----[ Program Description ]---------------------------------------------

' This program runs the Gas Sensor Heater through two phases (voltages) as ' recommended by the manufacturer datasheet. The sensor should run for at ' least 10 minutes in clean air before any calibration is done.

' The first phase is the PURGE phase where the heater element is turned on ' at a full 5V. This clears the sensor and no checking for an alarm ' condition is done here. The DEBUG screen will count down the 60 seconds ' of this phase.

' The second phase is the SENSE phase where the heater element is run at ' ~1.4V for 90 seconds. It is during this phase that the sensor can be ' calibrated or that the sensor is checked for alarm conditions.

' -----[ I/O Definitions ]-------------------------------------------------

HSW PIN 0 ' Heater Switch Control ALR PIN 1 ' Alarm Input Sense

' -----[ Variables ]-------------------------------------------------------

index VAR Word ' Counter Variable

' -----[ Program Code ]----------------------------------------------------

Main: DO LOW HSW ' Turn Heater ON FOR index = 59 TO 0 ' Count Down 60 Seconds DEBUG HOME, "PURGE MODE...", DEC2 index, " " PAUSE 1000 ' 1 Second Pause NEXT index = 1710 ' Approximately 90 Seconds DO ' Of Iterations On BS2 DEBUG HOME, "SENSE MODE...", DEC2 index / 19 LOW HSW ' Turn Heater ON PAUSE 15 ' For 15 mS INPUT HSW ' Turn Heater OFF PAUSE 3 ' For 3 mS index = index - 1 ' Decrement Counter IF ALR = 1 THEN ' Check For Alarm Condition DEBUG " ALARM" ' Display Alarm Condition ELSE DEBUG " " ' Clear Alarm Condition ENDIF LOOP UNTIL index = 0 ' End Of Sense Mode Loop LOOP

My Code: int ALR = 8; int HSW = 7; int LEDpin = 13; void setup() { Serial.begin(9600); // sets the serial port to 9600ms pinMode(HSW, INPUT); //sets pin 7 as the input for the HSW pinMode(ALR, OUTPUT); //sets pin 8 as the output for ALR }

void loop() { digitalWrite(HSW, HIGH); //sets HSW high delay(60000); //waits 60s delay(1000); //wait for 1s digitalWrite(HSW,HIGH); //HSW is set high again delay(90000); //waits 90s digitalRead(ALR); //it will read the value at pin 8 if it;s high or low if (ALR = 1) //if ALR is high (CO detected) then pin 13 will be high { digitalWrite(LEDpin, HIGH); } else { digitalWrite(LEDpin, LOW); }


 if (ALR = 1)

Is always true. (and next time through the loop, the "digitalread (ALR)" will read and discard the result of reading a different pin. Which is one of the reasons it is a good idea to use the "const" qualifier on things that ought to be constants.)

I'm worried by the term "heater" - what buffering have you got to drive this "heater"?

 // sets the serial port to 9600ms


ummm....I don't have a buffer for the heater pin (I have read the datasheet, and it does not seem to need a heater, only a 5V supply for 60s). The heater is to warm up my sensor (it comes with the module). This is where the datasheet is located:

When you said that the "if (ALR = 1)" is always true, do you mean that i have set it to always true? Should I code it differently? Any suggestions? (Thanks for replying so quickly)

5V @ 165mA.
Don’t want to use an AVR output for that.
You didn’t, did you?

When you said that the “if (ALR = 1)” is always true, do you mean that i have set it to always true?

No, I meant that in C the result of the assignment of the constant 1 to a variable will always be non-zero, and so be considered to be true.

Do you mean have I hooked up the heater pin directly to the Arduino? That was what I was going to do, but if you are saying that isn't good, then I will use a buffer.

An AVR pin can supply AT MOST 40mA, so yes, you MUST use some form of buffering.

Thanks for letting me know about the buffer.

Should I make the ALR a const? If not ALR, what should I put as const? (sorry I keep asking so many questions, I'm just having trouble with this).


digitalRead(ALR);      //it will read the value at pin 8 if it;s high or low
   if (ALR = 1)

you call digitalRead, but discard the value it returns, then you assign the the value 1 to ALR and test the result. Tryif (digitalRead(ALR) == HIGH) instead.

Thanks for that suggestion. Was that the only problem with my code that you could see?

Those were the most obvious, but I don't have the sensor

Thanks for helping me.

For a buffer, should I use a NPN transistor?

Yes, that would work, but make sure that the IC rating is greater than the expected current for the heater.

This may be a dumb question (but I really am having trouble with this) but how do I connect the transistor to the Arduino and to the sensor? Any idea?

Oops, sorry, the heater has a 2n2907 to drive it - I thought it was bare sensor.

Digital ouptut to HSW pin, digital input from ALR, power and ground.

Groove: Digital ouptut to HSW pin, digital input from ALR, power and ground.

What do you mean by what you wrote here?

Sorry, that should have said "output to HSW"

Sorry, I don't understand the "power and ground" thing.

You need to connect a 5V supply (power) capable of supplying > 160mA, and you need to connect the ground pin on the sensor board to the ground (GND) on your Arduino.

I see. Thanks.