Advertisement
pleasedontcode

"Hydrogen Sensor" rev_01

Jun 20th, 2024
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Hydrogen Sensor"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-20 14:50:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create an Arduino project utilizing the */
  21.     /* MQUnifiedsensor library to read and process data */
  22.     /* from an MQ-8 hydrogen sensor. Implement functions */
  23.     /* to convert raw analog data from pin A0 to physical */
  24.     /* values and ensure real-time updates in the loop. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <MQUnifiedsensor.h> // https://github.com/miguel5612/MQSensorsLib
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateInputs();
  34. void convertInputsFromRawToPhyData();
  35. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup);
  36. float map_f(float x, float in_min, float in_max, float out_min, float out_max);
  37.  
  38. /***** DEFINITION OF ANALOG INPUT PINS *****/
  39. const uint8_t H2Sensor_MQ8_AOUT_PIN_A0 = A0;
  40.  
  41. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  42. const uint8_t SEGMENT_POINTS_voltage__PIN_A0 = 1;
  43. const float voltage__PIN_A0_lookup[2][SEGMENT_POINTS_voltage__PIN_A0] =
  44. {
  45.   {0.0}, // Voltage [V]
  46.   {0.0}  // []
  47. };
  48.  
  49. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. unsigned int H2Sensor_MQ8_AOUT_PIN_A0_rawData = 0; // Analog Input
  52.  
  53. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float H2Sensor_MQ8_AOUT_PIN_A0_phyData = 0.0; // []
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. // Definitions for MQ8 sensor
  59. #define placa "Arduino UNO"
  60. #define Voltage_Resolution 5
  61. #define pin A0
  62. #define type "MQ-8"
  63. #define ADC_Bit_Resolution 10
  64. #define RatioMQ8CleanAir 70
  65.  
  66. // Declare Sensor
  67. MQUnifiedsensor MQ8(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
  68.  
  69. void setup(void)
  70. {
  71.   // put your setup code here, to run once:
  72.   pinMode(H2Sensor_MQ8_AOUT_PIN_A0, INPUT);
  73.  
  74.   Serial.begin(9600); // Init serial port
  75.  
  76.   // Set math model and constants for H2 concentration
  77.   MQ8.setRegressionMethod(1);
  78.   MQ8.setA(976.97);
  79.   MQ8.setB(-0.688);
  80.  
  81.   MQ8.init(); // Initialize sensor
  82.  
  83.   // Calibration
  84.   Serial.print("Calibrating please wait.");
  85.   float calcR0 = 0;
  86.   for (int i = 1; i <= 10; i++) {
  87.     MQ8.update();
  88.     calcR0 += MQ8.calibrate(RatioMQ8CleanAir);
  89.     Serial.print(".");
  90.   }
  91.   MQ8.setR0(calcR0 / 10);
  92.   Serial.println("  done!");
  93.  
  94.   if (isinf(calcR0) || calcR0 == 0) {
  95.     Serial.println("Warning: Connection issue detected, please check your wiring and supply");
  96.     while (1);
  97.   }
  98.  
  99.   MQ8.serialDebug(true);
  100. }
  101.  
  102. void loop(void)
  103. {
  104.   // put your main code here, to run repeatedly:
  105.   updateInputs(); // Refresh input data
  106.   convertInputsFromRawToPhyData(); // Transform raw data to physical data according to characteristic curve
  107.  
  108.   MQ8.update();
  109.   MQ8.readSensor();
  110.   MQ8.serialDebug();
  111.   delay(500);
  112. }
  113.  
  114. void updateInputs()
  115. {
  116.   H2Sensor_MQ8_AOUT_PIN_A0_rawData = analogRead(H2Sensor_MQ8_AOUT_PIN_A0);
  117. }
  118.  
  119. /* BLOCK lookup_phyData_from_voltage */
  120. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  121. {
  122.     // Search table for appropriate value.
  123.     uint8_t index = 0;
  124.  
  125.     const float *voltagePointer = &voltage_phyData_lookup[0];
  126.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  127.  
  128.     // Perform minimum and maximum voltage saturation based on characteristic curve
  129.     voltage = min(voltage, voltagePointer[segment_points-1]);
  130.     voltage = max(voltage, voltagePointer[0]);
  131.  
  132.     while( voltagePointer[index] <= voltage && index < segment_points )
  133.         index++;
  134.  
  135.     // If index is zero, physical value is smaller than our table range
  136.     if( index==0 )
  137.     {
  138.         return map_f( voltage,
  139.             voltagePointer[0],   // X1
  140.             voltagePointer[1],   // X2
  141.             phyDataPointer[0],   // Y1
  142.             phyDataPointer[1] ); // Y2
  143.     }
  144.     // If index is maxed out, phyisical value is larger than our range.
  145.     else if( index==segment_points )
  146.     {
  147.         return map_f( voltage,
  148.             voltagePointer[segment_points-2],   // X1
  149.             voltagePointer[segment_points-1],   // X2
  150.             phyDataPointer[segment_points-2],   // Y1
  151.             phyDataPointer[segment_points-1] ); // Y2
  152.     }
  153.     // index is between 0 and max, just right
  154.     else
  155.     {
  156.         return map_f( voltage,
  157.             voltagePointer[index-1], // X1
  158.             voltagePointer[index],    // X2
  159.             phyDataPointer[index-1], // Y1
  160.             phyDataPointer[index] );  // Y2
  161.     }
  162. }
  163. /* END BLOCK lookup_phyData_from_voltage */
  164.  
  165. /* BLOCK map_f */
  166. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  167. {
  168.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  169. }
  170. /* END BLOCK map_f */
  171.  
  172. /* BLOCK convertInputsFromRawToPhyData */
  173. void convertInputsFromRawToPhyData()
  174. {
  175.     float voltage = 0.0;
  176.  
  177.     voltage = H2Sensor_MQ8_AOUT_PIN_A0_rawData * (5.0 / 1023.0);
  178.     H2Sensor_MQ8_AOUT_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage__PIN_A0, &(voltage__PIN_A0_lookup[0][0]));
  179.  
  180. }
  181. /* END BLOCK convertInputsFromRawToPhyData */
  182.  
  183. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement