Hallo ich melde mich wieder mal hier bei euch. ich habe meinen Code soweit fertig nur läuft mein Lüfter nicht mit PWM. Lüfter hängt mit seinem PWM Anschluss Am Arduino. Meine Temperaturregelung läuft nur halt der Lüfter läuft ungeregelt.
Ich schaffe es einfach nicht an dem PWM Pin die 25KHz auszugeben. kann mir bitte jemand helfen beim Code das der PWM Läuft. Habe schon mehrere Lüfter getestet keiner Reagiert auf das PWM- Signal
/**Ver.3 ***/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SHT21.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
SHT21 sht; //SHT on OLED entities
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float Temp; //Here we store the temperature and humidity values
//float Humidity;
#include <Fonts/FreeMonoBold12pt7b.h>
//#include "DHT.h" // Include DHT11 library
//#define DATApin 12 // Defines Arduino pin which is connected to the sensor
//DHT
//#define DHTPIN 12 // DHT pin
//#define DHTTYPE DHT22
//dht(DHTPIN, DHTTYPE); // Creates a DHT object
int fan = 9; // fan is connect to Arduino Pin D3 (PWM)
int GreenLed = 3; // Green LED is connect to Arduino Pin
int RedLed = 4; // Red LED is connect to Arduino Pin D9
//int HumidityMax = 95;
//int HumidityMin = 50;
int tempMin = 26; // the temperature to start the fan 0%
int tempMax = 30; // the maximum temperature when fan is at 100%
int fanSpeed = 0;
int fanLCD;
int fanOut = 1;
int fanMin = 0;
unsigned long previousMillis = 0; // speichert den Zeitpunkt an dem zuletzt geschalten wurde
const long intervall = 200; //600000; // Länge der Pause (hier 5 Min.) in Millisekunden 5 * 60 *
const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;
const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency (Frequency in HZ!) (Set currently to 25kHZ)
const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ);
void setup()
{
pinMode(OC1A_PIN, OUTPUT);
// Clear Timer1 control and count registers
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Set Timer1 configuration
// COM1A(1:0) = 0b10 (Output A clear rising/set falling)
// COM1B(1:0) = 0b00 (Output B normal operation)
// WGM(13:10) = 0b1010 (Phase correct PWM)
// ICNC1 = 0b0 (Input capture noise canceler disabled)
// ICES1 = 0b0 (Input capture edge select disabled)
// CS(12:10) = 0b001 (Input clock select = clock/1)
TCCR1A |= (1 << COM1A1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << CS10);
ICR1 = TCNT1_TOP;
//Sets the baud for serial data transmission between Arduino and your computer
Serial.begin(9600);
//dht.begin();
//sht.begin();
//initialize with the I2C addr 0x3C (128x64)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
delay(10);
pinMode(fan, OUTPUT);
pinMode(RedLed, OUTPUT); // initialize digital pin RedLed as an output.
pinMode(GreenLed, OUTPUT); // initialize digital pin GreenLed as an output.
// Print text on display
display.clearDisplay();
//display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(25, 0);
display.println(F("ELECTRODUIN0")); // Print text
/*
display.setCursor(10, 20);
display.println(F("temperature Based"));
display.setCursor(3, 35);
display.println(F("fan Speed Controller"));
display.display();
*/
delay(2000);
}
void loop() {
float temp = sht.getTemperature();
float Humidity = sht.getHumidity();
//float temp = dht.readTemperature();
//float Humidity = dht.readHumidity();
//Print tempareture Value on Serial Monitor Window
Serial.print("temperature = ");
Serial.print(temp, 0); // temperature value in Degree Celsius
Serial.println("°C");
unsigned long currentMillis = millis();//Jetzige Millisekunden
if (currentMillis - previousMillis >= intervall) // Falls mehr als 300000 ms vergangen sind
{
previousMillis = currentMillis; // Zeitpunkt der letzten Schaltung wird festgehalten
if (temp < tempMin) // if temp is lower than tempMin
{
fanSpeed = 100; // fan is not spinning
analogWrite(fan, fanSpeed);
fanLCD = 100;
digitalWrite(fan, 255); // fan aus
digitalWrite(GreenLed, HIGH); // turn off Green LED
}
//if temperature is higher than tempMin and lower than tempMax
if ((temp >= tempMin) && (temp <= tempMax))
//if ((Humidity >= HumidityMin) && ( Humidity <= HumidityMax))
{
fanSpeed = map (temp, tempMin, tempMax, 255,0); // the actual speed of fan//map(temp, tempMin, tempMax, 32, 255);
//fanSpeed = 1.5 * fanSpeed;
fanLCD = map (temp, tempMin, tempMax, 100, 0); // fan speed Show on display
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
digitalWrite(GreenLed, HIGH); // turn on Green LED
} //if temp is higher than tempMax
if (temp > tempMax)
{
digitalWrite(RedLed, HIGH); // turn on Red LED
digitalWrite(GreenLed, LOW); // turn off Green LED
}
else
{
digitalWrite(RedLed, LOW); // turn off Red LED
}
// Wenn der PWM Wert unter den van FanMin fällt, schält der Lüfter ab
if (fanSpeed < fanMin)
{
fanSpeed = 0;
fanOut = 1;
}
// Hysterese
if (fanOut == 1)
{
fanSpeed = 0;
}
if(temp >= 31)
{
if(fanOut == 1)
{
fanOut = 0;
analogWrite(fan, 255);
}
}
// PWM Wert auf 255 begerenzen
if (fanSpeed > 255)
{
fanSpeed = 255;
}
// Lüftergeschwindigkeit über den Seriellen Monitor ausgeben
//Serial.print("Lueftergeschwindigkeit: ");
//Serial.println(fanSpeed);
analogWrite(fan, fanSpeed); // Den Lüfter mit dem PWM Wert ansteuern
delay(500);
}
// Print text on display
display.clearDisplay();
// display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 0);
display.print("H: "); // Print text
display.print(Humidity);
display.print(" %");
display.setTextSize(2);
display.setCursor(0, 20);
// Print temperature
display.print("Temp:");
display.setCursor(60, 20);
display.print(temp, 1); // temperature Value
display.setCursor(95, 20);
//display.print((char)247);
display.setCursor(110, 20);
display.print("C");
// Print fan Speen
display.setCursor(0, 40);
display.print("FAN:: ");
display.setCursor(60, 40);
display.print(fanLCD); // fan Speed Value
display.setCursor(110, 40);
display.print("%");
display.display();
//delay(2000); // wait two seconds
}
