-1

This is the code:

#define TMST A2 // Thermistor
#define BZR 10  // Buzzer
#define FanP 11 // Fan

void setup() {
  DDRD = B11000000;
  DDRB = B00001111;
  DDRC = B00000000;
  TCCR2B = TCCR2B & B11111000 | B00000101; // pin 3 & 11 -> 245Hz
  T0 = 25 + 273.15;
}

void loop() {
  if (hallState > magnetDistance) {
    PORTB &= ~(1 << 0); // port manipulation -> LOW
    strcpy(WPStatus, "OFF");
    if (Alarm == 1) { // this will be set in setting menu
      if ((millis() - BZRT) > 500) {
        tone(BZR, 2300);
        BZRT = millis();
      }
      if ((millis() - BZRT) > 250) {
        noTone(BZR);
      }
    }
    else {
      noTone(BZR);
    }
  }
  else {
    PORTB |= (1 << 0); // port manipulation -> HIGH
    noTone(BZR);
    strcpy(WPStatus, " ON");
  }

  if ((millis() - fanTimer) > 2000) { // checks every 2 seconds if temperature changed
    Thermistor();
    if (TX <= 32) {
      analogWrite(FanP, 60); // Fan spinning speed
    }
    if (TX > 32 && TX <= 36) {
      analogWrite(FanP, 120);
    }
    if (TX > 36) {
      analogWrite(FanP, 255);
    }
    fanTimer = millis();
  }
}
float Thermistor() { // reading and calculating from thermistor
  VRT = analogRead(TMST);
  VRT = (5.00 / 1023.00) * VRT;
  VR = 5 - VRT;
  RT = VRT / (VR / 10000);
  ln = log(RT / 10000);
  TX = (1 / ((ln / 3977) + (1 / T0)));
  TX = TX - 273.15;
  return TX; // returning the temperature
}

The 5V buzzer connected to a PWM pin with a 100 ohm resistor, thermistor and fan are connected to Arduino like this:

schematic

simulate this circuit – Schematic created using CircuitLab

Why When buzzer is alarming fan stops spinning?

  • I have an idea why, but from that code snippet I cannot be certain. You need to post your whole code, since the part that will tell me if I am right in my surmise or not is the part you have replace with the comment "// defined Variables and stuff". – Majenko Jul 30 at 9:31
  • @Majenko the whole code is too long, but i added the part you mentioned. – Hamed Jul 30 at 9:35
  • Ta. Also, which Arduino are you using for this? – Majenko Jul 30 at 9:37
  • 1
    "Use of the tone() function will interfere with PWM output on pins 3 and 11." arduino.cc/reference/en/language/functions/advanced-io/tone – Juraj Jul 30 at 9:43
  • 1
    the pin for tone() doesn't need to be a pwm pin – Juraj Jul 30 at 10:05
2

The tone() command uses Timer2 to generate the square wave (it triggers an interrupt which toggles an IO pin).

However Timer2 is also used to generate the PWM signal on pins 3 and 11.

Thus you cannot use tone() and PWM on pins 3 or 11 simultaneously.

The simple fix: use PWM from pins 5, 6, 9 or 10 to control your fan.

  • an off topic question; since i can't use that pin anymore and other pins are occupied, is it better to use a series of diodes to reduce the fan speed or a high wattage resistor would work? – Hamed Jul 30 at 9:55
  • 1
    A high wattage resistor would work. That is basically what is used in mains fans with the buttons that change the speed. – Majenko Jul 30 at 10:00
  • 1
    @newbie, you can use pin 10 for fan and pin 11 or any other pin for tone() – Juraj Jul 30 at 10:06
  • 2
    If you don't mind your sketch blocking while playing a tone there is TimerFreeTone – Majenko Jul 30 at 10:10
  • 2
    or you can change the timer used by the tone() library – Juraj Jul 30 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.