0% found this document useful (0 votes)
27 views

Arduino Co2 Sensor Code

This document contains code for an Arduino program that reads CO2 levels from a sensor, outputs the reading to LED indicators, and generates an audio tone based on the PPM level. It initializes pins, takes readings in a loop, determines the PPM range, and controls outputs accordingly.

Uploaded by

aearocas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Arduino Co2 Sensor Code

This document contains code for an Arduino program that reads CO2 levels from a sensor, outputs the reading to LED indicators, and generates an audio tone based on the PPM level. It initializes pins, takes readings in a loop, determines the PPM range, and controls outputs accordingly.

Uploaded by

aearocas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <avr/io.

h>
#include <util/delay.h>
#include <MHZ.h>

#define SPS_CONNECTED_PIN 8 // PB0


#define CO2_IN 2 //PD2 PWM PIN
#define OUTPUT_PIN 3 // PD3
#define PB1_HIGH_PIN 9 // PB1
#define PB2_OUTPUT_PIN 10 // PB2
#define PPM_LOW_PIN A2 // PC4
#define PPM_MEDIUM_PIN A1 // PC2
#define PPM_HIGH_PIN A0 // PC6
#define F_CPU 8000000L

MHZ co2(CO2_IN, MHZ::MHZ19C); // here the range value is set to 5000 by default (RANGE_5K)
volatile uint16_t co2Value = 0;

void setup() {
// Start serial communication with a baud rate of 9600
Serial.begin(9600);
// Configure pins
pinMode(CO2_SENSOR_PIN_INPUT, INPUT);
pinMode(CO2_SENSOR_PIN_OUTPUT, OUTPUT);
pinMode(OUTPUT_PIN, OUTPUT);
pinMode(SPS_CONNECTED_PIN, INPUT);
pinMode(PB1_HIGH_PIN, OUTPUT);
pinMode(PB2_OUTPUT_PIN, OUTPUT);
pinMode(PPM_LOW_PIN, OUTPUT);
pinMode(PPM_MEDIUM_PIN, OUTPUT);
pinMode(PPM_HIGH_PIN, OUTPUT);
co2Value = 4500;
}
void CO2Ausgabe() {
// Determine PPM Range and set corresponding pins
if (co2Value <= 2000) {
digitalWrite(PPM_LOW_PIN, HIGH);
digitalWrite(PPM_MEDIUM_PIN, LOW);
digitalWrite(PPM_HIGH_PIN, LOW);
} else if (co2Value >= 2001 && co2Value <= 4000) {
digitalWrite(PPM_LOW_PIN, LOW);
digitalWrite(PPM_MEDIUM_PIN, HIGH);
digitalWrite(PPM_HIGH_PIN, LOW);
} else if (co2Value >= 4001) {
digitalWrite(PPM_LOW_PIN, LOW);
digitalWrite(PPM_MEDIUM_PIN, LOW);
digitalWrite(PPM_HIGH_PIN, HIGH);
} else {
digitalWrite(PPM_LOW_PIN, LOW);
digitalWrite(PPM_MEDIUM_PIN, LOW);
digitalWrite(PPM_HIGH_PIN, LOW);
}
}

void loop() {
co2Value = co2.readCO2PWM();
// SPS is connected
if (digitalRead(SPS_CONNECTED_PIN) == HIGH) {
// LED Output
CO2Ausgabe();
// Send binary signal at a frequency equal to ppm
tone(OUTPUT_PIN, co2Value);
} else {
// Additional logic based on co2Value
CO2Ausgabe();
if (co2Value >= 2001) {
digitalWrite(PB2_OUTPUT_PIN, HIGH);
} else {
digitalWrite(PB2_OUTPUT_PIN, LOW);
}
}
delay(2); // Delay for stability
}

You might also like