When I turn off the PWM timer, the output goes High

Increase in output from turning off PWM timer

My code is as follows. I couldn't figure out a reason. When the timer is turned off, the output driving the high side of the MOSFET always remains high, and this is serious.
I want the PWM outputs to stay low when the fault loop runs.
I would be very grateful if anyone could help with this.

bool fadeCompleted = false;

void setup() {
  Serial.begin(9600); // Initialize serial port
  pinMode(9, OUTPUT); // Output A
  pinMode(10, OUTPUT); // Output B
  pinMode(5, OUTPUT); // mainon - output pin
  pinMode(6, OUTPUT); // ntcbypass - output pin
  // Removed parts related to A0

  // Set initially 5th, 6th, and 2nd pins to "LOW"
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(2, LOW);

  // Configure Timer1 for PWM
  TCCR1A = 0; // Clear Timer registers
  TCCR1B = 0;
  TCNT1 = 0;

  TCCR1B |= _BV(CS10); // No prescaler
  ICR1 = 64; // 80 counts for PWM mode

  OCR1A = 37; // Match on Pin 9
  TCCR1A |= _BV(COM1A1) | _BV(COM1A0); // Set output A rising/falling edge

  OCR1B = 27; // Match on Pin 10
  TCCR1A |= _BV(COM1B1); // Set output B falling/rising edge
}

void loop() {
  Serial.println("Loop Cycle");

  int a0Value = analogRead(A0); // Read from A0 pin
  int a2Value = analogRead(A2);
  int a4Value = analogRead(A4);
  int a5Value = analogRead(A5);

  // Convert analog input values to voltage
  float voltCurr = (a2Value / 1023.0) * 5.0;
  float voltTemp = (a4Value / 1023.0) * 5.0;
  float voltPTC = (a5Value / 1023.0) * 5.0;
  float voltA0 = (a0Value / 1023.0) * 5.0;

  if (voltA0 >= 2.4 && voltA0 <= 2.6 && a0Value > 800 && a0Value < 1000 &&
      a2Value >= 0 && a2Value <= 410 &&
      a4Value >= 255 && a4Value <= 1023 &&
      a5Value >= 0 && a5Value <= 512) {
    Serial.println("Vcheck Cycle");
    Vcheck();
  } else {
    Serial.println("Fault Cycle");
    Fault();
  }

  // Print other values to the serial port
  Serial.print("Current Voltage: ");
  Serial.print(voltCurr);
  Serial.print("\tTemperature Voltage: ");
  Serial.print(voltTemp);
  Serial.print("\tPTC Voltage: ");
  Serial.print(voltPTC);
  Serial.print("\tA0 Voltage: ");
  Serial.println(voltA0);

  delay(1000); // Wait for 1 second
}

void Vcheck() {
  Serial.println("Vcheck Cycle");
  digitalWrite(5, HIGH); // mainon high
  delay(1000);
  int a3Value = analogRead(A3);
  float voltDC = (a3Value / 1023.0) * 5.0;

  if (a3Value >= 530 && a3Value <= 870) {
    if (!fadeCompleted) {
      Serial.println("PWM Cycle");
      PWM();
    }
  } else {
    Serial.println("Fault Cycle");
    Fault();
    fadeCompleted = false; // Reset fadeCompleted flag
  }
  Serial.print("\tDC Voltage: ");
  Serial.print(voltDC);
}

void PWM() {
  Serial.println("PWM Cycle");
  digitalWrite(6, HIGH); // ntcbypass high
  digitalWrite(2, HIGH); // pgood high
  TCCR1B |= _BV(WGM13); // PWM mode with ICR1 Mode 10
  TCCR1A |= _BV(WGM11); // Set WGM13:WGM10 to 1010

  // Fade effect
  for (int i = 10; i <= 42; i++) {
    OCR1B = (i * ICR1) / 100;
    delay(5);
  }

  fadeCompleted = true;
}

void Fault() {
  Serial.println("Fault Cycle");
  // Reset Timer1 registers for normal operation
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;

  TCCR1B |= _BV(CS10); // No prescaler
  ICR1 = 64; // 80 counts for PWM mode

  OCR1A = 37; // Match on Pin 9
  TCCR1A |= _BV(COM1A1) | _BV(COM1A0); // Set output A rising/falling edge

  OCR1B = 27; // Match on Pin 10
  TCCR1A |= _BV(COM1B1); // Set output B falling/rising edge

  digitalWrite(5, LOW); // mainon low
  digitalWrite(6, LOW); // ntcbypass low
  digitalWrite(2, LOW); // pgood low
}

Which line does do that?

I get this error from pb1 pin 9

Please show the code that turns off the timer.

Normally I would just close it with this code. But after the error was fixed, it did not start producing PWM again. When you close it this way, the pin remains at 9 high.

TCCR1A = 0;
   TCCR1B = 0;

Finally, I use this code. With this code, PWM starts again, but it remains at pin 9 high.
There is a short oscilloscope video but I don't know how to upload it.

void Fault() {
   Serial.println("Fault Cycle");
   //Reset Timer1 registers for normal operation
   TCCR1A = 0;
   TCCR1B = 0;
   TCNT1 = 0;

   TCCR1B |= _BV(CS10); //No prescaler
   ICR1 = 64; // 80 counts for PWM mode

   OCR1A = 37; // Match on Pin 9
   TCCR1A |= _BV(COM1A1) | _BV(COM1A0); // Set output A rising/falling edge

   OCR1B = 27; // Match on Pin 10
   TCCR1A |= _BV(COM1B1); // Set output B falling/rising edge

   digitalWrite(5, LOW); // mainon low
   digitalWrite(6, LOW); // ntcbypass low
   digitalWrite(2, LOW); // pgood low
}

This disconnects the output pins from the timer. You are responsible for setting the desired pin state.

So do I need to use it like this?
digitalWrite(9, LOW);
Sorry, I'm new to Arduino.

I would suggest when you turn the PWM off, use pinMode and set it as an output then use digitalWrite(LOW) to shut it down. I do not think that will work until the PWM is shut down but that is just a guess.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.