PHV2.ino 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <SPI.h>
  2. #include "../MCP3428/MCP3428.h"
  3. // Variables pour stocker les valeurs de calibration
  4. float cal_ph4_voltage = 0;
  5. float cal_ph7_voltage = 0;
  6. float cal_ph10_voltage = 0;
  7. float slope = 0;
  8. float intercept = 0;
  9. float temperature = 25.0; // Température par défaut en °C
  10. void setup() {
  11. Serial.begin(9600);
  12. Serial.println("Calibration du pH-mètre...");
  13. calibrate();
  14. }
  15. void loop() {
  16. Serial.println("Entrez la température de la solution en °C : ");
  17. while (Serial.available() == 0) {}
  18. temperature = Serial.parseFloat();
  19. Serial.print("Température : ");
  20. Serial.println(temperature);
  21. float ph = readPH();
  22. Serial.print("pH de la solution: ");
  23. Serial.println(ph);
  24. delay(1000);
  25. }
  26. void calibrate() {
  27. Serial.println("Plonger la sonde dans la solution de pH 4.01 et appuyer sur Enter");
  28. while (Serial.read() != '\n') {}
  29. cal_ph4_voltage = readVoltage(0);
  30. Serial.print("Voltage à pH 4.01: ");
  31. Serial.println(cal_ph4_voltage);
  32. Serial.println("Plonger la sonde dans la solution de pH 7.00 et appuyer sur Enter");
  33. while (Serial.read() != '\n') {}
  34. cal_ph7_voltage = readVoltage(0);
  35. Serial.print("Voltage à pH 7.00: ");
  36. Serial.println(cal_ph7_voltage);
  37. Serial.println("Plonger la sonde dans la solution de pH 10.01 et appuyer sur Enter");
  38. while (Serial.read() != '\n') {}
  39. cal_ph10_voltage = readVoltage(0);
  40. Serial.print("Voltage à pH 10.01: ");
  41. Serial.println(cal_ph10_voltage);
  42. // Calcul de la pente et de l'ordonnée à l'origine de la courbe de calibration
  43. slope = (10.01 - 4.01) / (cal_ph10_voltage - cal_ph4_voltage);
  44. intercept = 7.0 - slope * cal_ph7_voltage;
  45. Serial.print("Pente: ");
  46. Serial.println(slope);
  47. Serial.print("Ordonnée à l'origine: ");
  48. Serial.println(intercept);
  49. }
  50. float readPH() {
  51. float voltage = readVoltage(0);
  52. float temp_slope = -59.16 * (temperature + 273.15) / 298.15; // Ajustement de la pente de Nernst avec la température
  53. return (voltage - cal_ph7_voltage) * temp_slope / -59.16 + 7.0;
  54. }
  55. float readVoltage(byte channel) {
  56. unsigned int command = 0xD0 | (channel << 4); // Configuration de l'ADC
  57. digitalWrite(CS_PIN, LOW);
  58. SPI.transfer(command);
  59. unsigned int high_byte = SPI.transfer(0x00);
  60. unsigned int low_byte = SPI.transfer(0x00);
  61. digitalWrite(CS_PIN, HIGH);
  62. unsigned int value = (high_byte << 8) | low_byte;
  63. float voltage = (value * 5.0) / 65535.0; // Conversion en tension
  64. return voltage;
  65. }