Advertisement
atheos42

ESP32_IR_Meter

Jun 1st, 2023 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | Source Code | 0 0
  1. // Reference:
  2. // https://circuitdigest.com/microcontroller-projects/interfacing-flame-sensor-with-arduino
  3. // https://circuitdigest.com/microcontroller-projects/arduino-frequency-counter-circuit
  4. // https://www.instructables.com/Arduino-Frequency-Counter/
  5. // https://github.com/Bodmer/TFT_eSPI/blob/master/examples/160%20x%20128/TFT_Print_Test/TFT_Print_Test.ino
  6.  
  7. // Uses IR Infrared Flame Sensor Module 4 Pin
  8.  
  9. #include "Arduino.h"
  10. #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
  11. #include <SPI.h>
  12.  
  13. // Pin Assignments
  14. #define IR_Pin 2
  15.  
  16. // Variables
  17. unsigned long H_cycle;
  18. unsigned long L_cycle;
  19. unsigned long Freq;
  20. char str_Freq[32];
  21. TFT_eSPI tft = TFT_eSPI();  // Invoke TFT library
  22.  
  23. void setup(){
  24.     // Initalize GPIO pins
  25.     pinMode(IR_Pin, INPUT);
  26.  
  27.     // Initalize Serial port
  28.     Serial.begin(9600);
  29.    
  30.     // Initalize TFT
  31.     tft.init();
  32.     tft.setRotation(1);
  33.   tft.fillScreen(TFT_BLACK);
  34. }
  35.  
  36. void loop (){
  37.     Freq = 0;
  38.   //tft.fillScreen(TFT_BLACK);
  39.  
  40.     H_cycle = pulseIn(IR_Pin,HIGH); // read signal at high state
  41.   L_cycle = pulseIn(IR_Pin,LOW);    // read signal at low state
  42.     if((H_cycle + L_cycle) != 0){
  43.     Freq = 1000000 / (H_cycle + L_cycle);   // covert H + L cycles in Micro seconds to Frequency
  44.   }
  45.    
  46.     // format output string
  47.     snprintf_P(str_Freq, sizeof(str_Freq), PSTR("Freq: %u Hz"), Freq);
  48.    
  49.     // output string to serial port
  50.     Serial.println (str_Freq);
  51.    
  52.     // output string to tft display
  53.     tft.setCursor(0, 0, 4);
  54.     tft.setTextColor(TFT_GREEN,TFT_BLACK);
  55.     tft.println("IR Meter");
  56.   tft.println("");
  57.     tft.println(str_Freq);
  58.   //delay(3000);
  59. }
  60.  
Tags: ESP32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement