CO2 Sensor code

.

Perhaps you can get help if you show a schematic or a block diagram of how YOU have the sensor, the Arduino , and power connected together. Also need to know which method you are using to communicate with the sensor. Are you leaving it powered continuously so it can do it's self calibration or are you doing the manual calibration?

Paul

It looks as though the code writes to mySerial and tries to read from it before the call to mySerial.begin. I would expect this line to be earlier in the setup function:

  mySerial.begin(9600);
SoftwareSerial mySerial(A0, A1); // A0 and A1 on Arduino to RX, TX on MH-Z14 respectively

This looks backwards. The software serial descriptor is from the point of view of the Arduino. So A0 is rx of the Arduino and A1 is tx of the Arduino.

These pins need to be connected so that rx Arduino is connected to tx sensor, and tx Arduino is connected to rx sensor.

You need to cross connect rx <->tx and tx <->rx. Transmiters talk to receivers.

Your code has some serious errors.

mySerial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

You need to call .begin() before all the .write() and .read() commands. You do not need to configure the pinModes, as the software serial library will do that.

Work first on getting a reading from the gas sensor and add the lights later.

wildbill:
It looks as though the code writes to mySerial and tries to read from it before the call to mySerial.begin. I would expect this line to be earlier in the setup function:

  mySerial.begin(9600);

Where should it be?

Right after this would do:

  Serial.begin(9600);

Are you convinced that this ever worked?

I understand that my code is for analog signals.
I find this article where is PWM signal code - "MH-Z14A Arduino Code PWM Mode".

Is it possible to replace part of my code with this code?
If yes, can someone combine these two codes and paste it here?

Sorry, I'm not knowledgeable in programming at all :blush:

I understand that my code is for analog signals.

Correct.

mppt = map(analogRead(A1), 0, 1023, 0, 2000 );

What is connected to the Arduino pin A1?

If it is one of the Serial output pins that would be wrong.

There are three modes of output for that sensor (Serial, pwm, analog) but they are all internally related. The analog output is the most simple to read, and if you are not getting valid data, I don't think that using the serial or pwm output will be any better.

cattledog:
What is connected to the Arduino pin A1?

It is connected to sensor - UART(TXD) TTL Level output.

cattledog:
.... I don't think that using the serial or pwm output will be any better.

But I think that there is a mistake in my code and if I can add this PWM or UART code to mine it will work...

Look at the data sheet you referenced.

A1 should be connected to Pad4/Pad21 Analog Output (0.4~2V)

I'm not certain that you need the software serial and command writing at all in your code at this point, so I would try to remove it, and just read the analog output on A1.

Alternatively, you can leave the A0/A1 software serial code in place, and move the analog reading to A2

Can you please give me code only to read analog output on A1. With option to see output in monitor(Serial.print) ?

You clearly do not understand enough of the basics of the Arduino to be jumping into this project.

There are built in basic examples included with the ide under File>examples>01.Basics where the first one you find is AnalogReadSerial.
Here it is with the pin number changed to A1

/*
  AnalogReadSerial

  Reads an analog input on pin A1, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 1:
  int sensorValue = analogRead(A1);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(100); // delay in between reads for stability
}

You said you purchased the code, but have not got it to run. What is the application? You may want to go to Gigs and Collaboration section of the forum and find someone with the hardware you can pay to test and write code for you.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.