I am running DC fans using PWM outputs D11 and D12 on a MEGA. I use the following command to change the frequency and it all works fine.
/* Code for Available PWM frequency for MEGA pins D11, D12 */
TCCR1B = TCCR1B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
I have a manual mode using a potentiometer to vary the fan speed from 50 - 255. All is fine.
Here's the problem I just ran into. I added a relay board to control power to small heaters and assigned them to D8 and D9. PinMode sets them to OUTPUT and I turn the relays on by setting the output HIGH.
Now, when I manually adjust the potentiometer, the relay will start chattering at certain positions. I didn't have my scope with me, so I couldn't check it, but my guess is that the output suddenly turned into a PWM signal. Is this possible? I tried other digital outputs and had the same problem.
Does messing with the Arduino frequency cause issues like this?
Tested on mega2560 and it changes only the pwm frequency to 31KHz, output 8 stays at the level set ( tccr1a = 1 and tcrr1b = 1 ).
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.print("tccr1a: ");
Serial.println(TCCR1A);
Serial.print("tccr1b: ");
Serial.println(TCCR1B);
TCCR1B = TCCR1B & B11111000 | B00000001;
Serial.print("tccr1b: ");
Serial.println(TCCR1B);
pinMode(11, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(8,1);
}
int i = 0;
void loop() {
// put your main code here, to run repeatedly:
analogWrite(11,i);
//digitalWrite(8,(i>127));
Serial.println(i);
i++; i=i%256;
delay(10);
}
No. You've made some mistake in your code, but you haven't posted it so nobody can help you find it. Please post the code that caused the issue and a wiring diagram and I'm sure someone can spot what you've done wrong. My first guess would be insufficient power supply but that's just a guess.
I tend to agree with you. My code consists of multiple files and hundreds of lines, so I'm not posting all of it. I will go through it carefully and post parts of it. I wondered about the power also. I run the boards on a 12V 50A DC power supply and connect its ground to the Arduino ground. All the peripheral boards use that supply. I feed 12V to the Mega through the barrel jack. The other boards are fed 12V directly from the supply. I need to take a look at the voltage rail when the chattering happens, but the relays work fine until the potentiometer is at certain values, meaning certain PWM duty cycles.
Sorry for the delayed response. It's harvest time on the farm and my creation is an automated hops dryer controller. It uses 12 sensors to monitor the hops as they dry and writes data to a SD card for later analysis. Once the harvest is finished, I can put in the time to try to recreate the problem. I'll build a model of it that just has the necessary parts to reproduce the problem, and the code to simulate it. You can see what my controller looks like in the attached picture. I've never made a wiring diagram for it, but I know that I should. My customer (son #1), isn't too demanding about product documentation.
In automatic mode, the fan speed moves up and down by steps of 20 or 10, depending on the average temperature of 10 sensors. It makes the adjustment once every 3 minutes. It may or may not hit the certain pwm duty cycles to cause the chatter, but I'd have to watch it for hours to test that theory. Using the pot in manual mode, there are certain points where the chatter happens. Turning the pot slightly stops the chatter. It behaves like the coil isn't getting enough current to hold the relay open, but the weird thing is that it happens only at certain pwm duty cycles. When the harvest is finished, I will bring my oscilloscope to the farm to monitor the data line when varying the pot and I will also monitor the current powering the board, which is a tap from the 50A 12V supply.