2

I'm creating a controller for a small greenhouse, I have a DHT22 for measuring and two relay module that controls heating and cooling systems, and I will use a piezo to produce humidity when it goes below X amount. This piezo works at 1.7 MHz and I know that Arduino can provide 1.779Mhz, I think It's close enough. After that I'll place a MOSFET at the output and then connect the piezo.

I'm quite new so I'm trying copy-paste some codes and this is what I've done:

I don't know how to turn on and off the frequency generator, I would like to know the code for that

           #include <DHT.h>
            #include <DHT_U.h>
            
            #define DHTPIN 6 
            #define DHTTYPE DHT11 //Define sensor as DHT11
            
            #define RELAY1  4  // Relay heating
            #define RELAY2  5  // Relay cooling
            
            int red = 8; // red LED heating
            int blue = 9; // blue LED cooling
            
            DHT dht(DHTPIN, DHTTYPE);
            
            float temperature;
            float humidity;
            
            // Frequency Generator code
            const uint8_t OUTPUT_PIN = 3;  // = OC2B pin inamovible
            const uint8_t PERIOD = 9;      // 9 CPU cycles ~ 1.778 MHz
            
            void setup()
            {
             
              pinMode(red, OUTPUT);
              pinMode(blue, OUTPUT);
              pinMode(RELAY1, OUTPUT);
              pinMode(RELAY2, OUTPUT);
            
              // Freq generator code
                pinMode(OUTPUT_PIN, OUTPUT);
                TCCR2B = 0;           // stop timer
                TCNT2  = 0;           // reset timer
                TCCR2A = _BV(COM2B1)  // non-inverting PWM on OC2B
                       | _BV(WGM20)   // fast PWM mode, TOP = OCR2A
                       | _BV(WGM21);  // ...ditto
                TCCR2B = _BV(WGM22);  // ...ditto
                OCR2A = PERIOD - 1;
                OCR2B = PERIOD/2 - 1;
            }
            
            void soundBuzzer() {
                TCCR2B |= _BV(CS20);  // F_CPU / 1
            }
            
            void silenceBuzzer() {
                TCCR2B &= ~_BV(CS20);
                TCNT2 = 0;
            }
              //End of freq generator code
            void loop()
            {
              delay(1000); //delay time between measurements. 1 sec for DHT11
            
              temperature = dht.readTemperature(); //Reads temperature
              humidity = dht.readHumidity(); //Reads humidity
            
             if(temperature < 25)
                {
            digitalWrite(red,HIGH);
            digitalWrite(blue,LOW);
             
             
            digitalWrite(RELAY1,0); 
            digitalWrite(RELAY2,1);   
                 }
             else if(temperature > 25)
                {
            digitalWrite(RELAY1,1);
            digitalWrite(RELAY2,0); 
            digitalWrite(red,LOW);
            digitalWrite(blue,HIGH);
            
            }
        
        if(humidity < 30)
         {
   TCCR2A |= _BV(COM2B1);
    }
        else if(humidity > 35)
                {
          TCCR2A &= ~_BV(COM2B1);
            
            }
        }

I don't even know if i'm doing it right, please let me know and thanks

Edgard
  • 21
  • 3
  • 2
    Your code doesn't compile: `humidity` should be `humedad`, and `turn on frequency` does not mean anything in C++. Other than that, it looks fine. You should test it! – Edgar Bonet Sep 11 '20 at 10:59
  • 1
    You should consider replacing your MOSFET with a suitable H-bridge driver and feed an RF transformer to drive the transducer with matched impedance. Just a simple MOSFET will give very poor results. At the absolute minimum you need a resistance across the transducer to bleed the charge, otherwise it won't do much. Medium, drive it with an H-bridge for a full swing signal. Best is to add a transformer as well. Alternatively a half-bridge and center-tapped transformer will reduce component count at the cost of a more complex transformer. – Majenko Sep 12 '20 at 21:39
  • 1
    Code was fixed, So if you are telling me that mosfet isn't the best for this task I should consider simply turning ON another relay and adding a different circuit for the piezo then I was planning to simply put a MP2565 on the output of the Arduino As I told you before i'm pretty new at this so I don't know if it's good or not – Edgard Sep 14 '20 at 14:30

0 Answers0