Utente:Galessandroni/Rilevatore di anidride carbonica: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
mNessun oggetto della modifica
Etichetta: Editor wikitesto 2017
→‎Codice sorgente: Codice KA30, licenza GNU/GPL
Riga 24:
 
== Codice sorgente ==
 
=== Sensore di precisione KA30 ===
<syntaxhighlight lang="arduino" line="1" start="1">
/*
* Misuratore di CO2 nell'aria
* Compatibile coi sensori K30/K22/K33 via I2C; mostra il valore di CO2
* valore tipico all'aperto 419,28 ppm
* valore inizio ricambio aria 1800 ppm
* valore fine ricambio aria 800 ppm
*/
 
#include <Wire.h>
// We will be using the I2C hardware interface on the Arduino in
// combination with the built-in Wire library to interface.
// Ingresso analogico Arduino A5 - I2C SCL
// Ingresso analogico Arduino A4 - I2C SDA
/*
In this example we will do a basic read of the CO2 value and checksum verification.
For more advanced applications please see the I2C Comm guide.
*/
int co2Addr = 0x68;
// This is the default address of the CO2 sensor, 7bits shifted left.
void setup() {
Serial.begin(9600);
Wire.begin ();
pinMode(13, OUTPUT); // address of the Arduino LED indicator
Serial.println("Application Note AN-102: Interface Arduino to K-30");
}
 
void loop() {
int co2Value = readCO2();
if (co2Value > 0)
{
Serial.print("Valore di CO2: ");
Serial.println(co2Value);
}
else
{
Serial.println("Errore di comunicazione col sensore");
}
delay(2000);
}
</syntaxhighlight>Di seguito la funzione readCO2 che comunica con il sensore e restituisce il valore della <chem>CO2</chem> presente nell'aria.<syntaxhighlight lang="arduino" line="1" start="41">
///////////////////////////////////////////////////////////////////
// Function : int readCO2()
// Returns : CO2 Value upon success, 0 upon checksum failure
// Assumes : - Wire library has been imported successfully.
// - LED is connected to IO pin 13
// - CO2 sensor address is defined in co2_addr
///////////////////////////////////////////////////////////////////
int readCO2()
{
int co2_value = 0; // We will store the CO2 value inside this variable.
 
digitalWrite(13, HIGH); // turn on LED
// On most Arduino platforms this pin is used as an indicator light.
 
//////////////////////////
/* Begin Write Sequence */
//////////////////////////
 
Wire.beginTransmission(co2Addr);
Wire.write(0x22);
Wire.write(0x00);
Wire.write(0x08);
Wire.write(0x2A);
 
Wire.endTransmission();
 
/////////////////////////
/* End Write Sequence. */
/////////////////////////
 
/*
We wait 10ms for the sensor to process our command.
The sensors's primary duties are to accurately
measure CO2 values. Waiting 10ms will ensure the
data is properly written to RAM
 
*/
 
delay(10);
 
/////////////////////////
/* Begin Read Sequence */
/////////////////////////
 
/*
Since we requested 2 bytes from the sensor we must
read in 4 bytes. This includes the payload, checksum,
and command status byte.
 
*/
 
Wire.requestFrom(co2Addr, 4);
 
byte i = 0;
byte buffer[4] = {0, 0, 0, 0};
 
/*
Wire.available() is not nessessary. Implementation is obscure but we leave
it in here for portability and to future proof our code
*/
while (Wire.available())
{
buffer[i] = Wire.read();
i++;
}
 
///////////////////////
/* End Read Sequence */
///////////////////////
 
/*
Using some bitwise manipulation we will shift our buffer
into an integer for general consumption
*/
 
co2_value = 0;
co2_value |= buffer[1] & 0xFF;
co2_value = co2_value << 8;
co2_value |= buffer[2] & 0xFF;
 
 
byte sum = 0; //Checksum Byte
sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow
 
if (sum == buffer[3])
{
// Success!
digitalWrite(13, LOW);
return co2_value;
}
else
{
// Failure!
/*
Checksum failure can be due to a number of factors,
fuzzy electrons, sensor busy, etc.
*/
 
digitalWrite(13, LOW);
return 0;
}
}
</syntaxhighlight>
 
=== Sensore SGP30 ===
 
== Espansioni suggerite ==