I have an application similar to this to drive a 12V, 10Hz PWM Mercedes Benz radiator cooling fan.
I was able to get this to work with a 10Hz PWM output with the Yourdruino RoboRed, which uses the UNO drivers and is equivalent to the Uno except it handles a little more voltage and current.
I used a special PWM.h library which allows frequencies under 31 Hz.
But I am now having problems with the Nano 33 BLE. Apparently when I try to compile with the NANO 33 BLE I get a message stating it cannot compile for this board. I believe the problem is that UNO libraries are not compatible with the NANO because they use a different chip set. So I am looking for alternatives to the PWM.h library. But would prefer to use code to will work with all boards.
Over the next several days or so I will be trying the codes presented in post 4, 10, and 23. I will also try a modified version of the Blink Example code very similar to post 4.
But I have several issues. With the exception of the Blink code I have no understanding of what these codes are doing.
From post #4 @chrisknightley presents this:
I really have no idea what this YxYY constant type formatting means. Also in post 4 and in the blink code, the delay statement is used. I understand why the delay statement is being used but when I tried to use the delay to pause programing to read the serial output all of my variables all the variables started to report nonsense. Hopefully that will go away if I don't use the PWM.h library. Notice all the delay statements had to be commented out in my attached code.
@GolamMostafa code in post 10 and modified as in post 23 uses the same YxYY format constants and also uses what appears to me as variables COM1A1, WGM11, WGM12, WGM13, CS10 and CS11. These variables don't appear to be initialized or assigned any where.
My level of expertise on programing is Fortran, BASIC and VBA
#include <PWM.h>
// Note for low frequency < 31Hz,the high resolution pwmWriteHR() must be used with 16 bit PWM Value
//For the RoboRed only pins 9 and 10 can be used with 16 bit resolution
int RadPin = A0;
int CondPin = A1;
int AuxPin = A2;
int RadVals[6] = {0, 0, 0, 0, 0, 0};
int CondVals[6] = {0, 0, 0, 0, 0, 0};
int AuxVals[6] = {0, 0, 0, 0, 0, 0};
// 10 bit temperature equivolents for radiator sensor
int Rad20 = 444; //85C
int Rad30 = 484; //90C
int Rad40 = 524; //95C
int Rad50 = 562; //100C
int Rad60 = 599; //105C
int Rad70 = 635; //110C
int Rad80 = 668; //115C
int Rad90 = 699; //120C
// 10 bit temperature equivolents for condenser sensor
int Cnd20 = 391; //40C
int Cnd30 = 440; //45C
int Cnd40 = 489; //50C
int Cnd50 = 537; //55C
int Cnd60 = 583; //60C
int Cnd70 = 627; //65C
int Cnd80 = 668; //70C
int Cnd90 = 706; //75C
int Aux20 = 0;
int Aux30 = 0;
int Aux40 = 0;
int Aux50 = 0;
int Aux60 = 0;
int Aux70 = 0;
int Aux80 = 0;
int Aux90 = 0;
int RadSpeed = 10;
int CondSpeed = 10;
int AuxSpeed = 10;
int FanSpeed = 10; // Set fan to 10% PWM = off
int FanPin = 9; // Set Fan pin
int16_t PWMVal;
int32_t frequency = 10;
int t1;
void setup() {
// put your setup code here, to run once:
pinMode(RadPin, INPUT);
Serial.begin(9600);
InitTimersSafe()
//sets the frequency for the specified pin
; bool success = SetPinFrequencySafe(FanPin, frequency);
//if the pin frequency was set successfully, pin 13 turn on. Pin 13 can be used to light an LED etc.
if (success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop() {
// put your main code here, to run repeatedly:
//this code prints sensor value to the console
Serial.print("Rad pin input = "); Serial.println(RadVals[0]);
Serial.print("Cond pin input = "); Serial.println(CondVals[0]);
Serial.print("Aux pin input = "); Serial.println(AuxVals[0]);
Serial.println("Yo1");
Serial.println();
// delay(1000);
// t1=millis();
// do {
// } while (millis()-t1 <1000);
// initialize values
RadVals[5] = 0;
CondVals[5] = 0;
AuxVals[5] = 0;
//read sensor value and set upper limit cap
//for (int i = 1; i <= 5; i++) {
int i = 0;
do {
// Find 5 data points
RadVals[i] = analogRead(RadPin);
CondVals[i] = analogRead(CondPin);
AuxVals[i] = analogRead(AuxPin);
Serial.print("Rad pin input stored = "); Serial.println(RadVals[i]);
Serial.print("Cond pin input stored = "); Serial.println(CondVals[i]);
Serial.print("Aux pin input stored = "); Serial.println(AuxVals[i]);
Serial.println(i);
Serial.println("Yo2");
Serial.println();
// delay(1000);
//Serial.print("Rad pin input before sum = "); Serial.println(RadVals[5]);
//Serial.println("Yo25");
//Serial.println();
// sum all 5 data points
RadVals[5] = RadVals[5] + RadVals[i];
CondVals[5] = CondVals[5] + CondVals[i];
AuxVals[5] = AuxVals[5] + AuxVals[i];
Serial.print("Rad pin input sum = "); Serial.println(RadVals[5]);
Serial.print("Cond pin input sum = "); Serial.println(CondVals[5]);
Serial.print("Aux pin input sum = "); Serial.println(AuxVals[5]);
Serial.println(i);
Serial.println("Yo3");
Serial.println();
// delay(1000);
i = i + 1;
} while (i < 5);
// Find Average of 5 data points
RadVals[5] = RadVals[5] / 5;
CondVals[5] = CondVals[5] / 5;
AuxVals[5] = AuxVals[5] / 5;
Serial.print("Rad pin input Average = "); Serial.println(RadVals[5]);
Serial.print("Cond pin input Average = "); Serial.println(CondVals[5]);
Serial.print("Aux pin input Average = "); Serial.println(AuxVals[5]);
Serial.println(i);
Serial.println("Yo4");
Serial.println();
// delay(1000);
if (RadVals[5] < Rad20) RadSpeed = 10;
if (RadVals[5] < Rad30 and RadVals[5] >= Rad20) RadSpeed = 20;
if (RadVals[5] < Rad40 and RadVals[5] >= Rad30) RadSpeed = 30;
if (RadVals[5] < Rad50 and RadVals[5] >= Rad40) RadSpeed = 40;
if (RadVals[5] < Rad60 and RadVals[5] >= Rad50) RadSpeed = 50;
if (RadVals[5] < Rad70 and RadVals[5] >= Rad60) RadSpeed = 60;
if (RadVals[5] < Rad80 and RadVals[5] >= Rad70) RadSpeed = 70;
if (RadVals[5] < Rad90 and RadVals[5] >= Rad80) RadSpeed = 80;
if (RadVals[5] >= Rad90) RadSpeed = 90;
if (CondVals[5] < Cnd20) CondSpeed = 10;
if (CondVals[5] < Cnd30 and CondVals[5] >= Cnd20) CondSpeed = 20;
if (CondVals[5] < Cnd40 and CondVals[5] >= Cnd30) CondSpeed = 30;
if (CondVals[5] < Cnd50 and CondVals[5] >= Cnd40) CondSpeed = 40;
if (CondVals[5] < Cnd60 and CondVals[5] >= Cnd50) CondSpeed = 50;
if (CondVals[5] < Cnd70 and CondVals[5] >= Cnd60) CondSpeed = 60;
if (CondVals[5] < Cnd80 and CondVals[5] >= Cnd70) CondSpeed = 70;
if (CondVals[5] < Cnd90 and CondVals[5] >= Cnd80) CondSpeed = 80;
if (CondVals[5] >= Cnd90) CondSpeed = 90;
if (AuxVals[5] < Aux20) AuxSpeed = 10;
if (AuxVals[5] < Aux30 and AuxVals[5] >= Aux20) AuxSpeed = 20;
if (AuxVals[5] < Aux40 and AuxVals[5] >= Aux30) AuxSpeed = 30;
if (AuxVals[5] < Aux50 and AuxVals[5] >= Aux40) AuxSpeed = 40;
if (AuxVals[5] < Aux60 and AuxVals[5] >= Aux50) AuxSpeed = 50;
if (AuxVals[5] < Aux70 and AuxVals[5] >= Aux60) AuxSpeed = 60;
if (AuxVals[5] < Aux80 and AuxVals[5] >= Aux70) AuxSpeed = 70;
if (AuxVals[5] < Aux90 and AuxVals[5] >= Aux80) AuxSpeed = 80;
if (AuxVals[5] >= Aux90) AuxSpeed = 90;
FanSpeed = max(RadSpeed, CondSpeed);
//map and assign pwm values to the fan output 0 to 65535 corresponds to 0 to 100%
PWMVal = map(FanSpeed, 0, 100, 0, 65535);
Serial.print("Rad pin input Average = "); Serial.println(RadVals[5]);
Serial.print("Cond pin input Average = "); Serial.println(CondVals[5]);
Serial.print("Aux pin input Average = "); Serial.println(AuxVals[5]);
Serial.print("Fan Speed = "); Serial.println(FanSpeed);
Serial.print("PMWval = "); Serial.println(PWMVal);
Serial.println("Yo5");
Serial.println();
// delay(1000);
//write the PWM value to the pwm output pin
pwmWriteHR(FanPin, PWMVal);
}