Hi,
I am trying to drive a motor via PWM, but when I want to turn it off using analogWrite(pin, 0), I can stil hear hum from the motor. If I measure voltage on the PWM pin (nothing attached on it i.e. only uC pin) it reads 0.104V. It makes no change if I set the pin as output or not. How to solve it?
If this problem cannot be overcome, how to exchange analogWrite and digitalWrite? The following does not work:
pinMode(pin, OUTPUT); //=in Setup. Not needed for analogWrite
if (Speed == 0) {
analogWrite(pin, 0);
digitalWrite(pin, 0);
} else {
//digitalWrite(pin, 1); ?!?!?!? what to do here?
analogWrite(pin, Speed);
}
The pin is PCI28/OC1B/(Digital 12)/PD4/pin18 on ATMEL ATMEGA644P/SANGUINO.
So: pin=12.
I checked wiring.h and wiring_analog.c for sanguino and everything is OK. Translates to:
sbi(TCCR1A, COM1B1); // connect pwm to pin on timer 1, channel B
OCR1B = val; // set pwm duty
I use the connection in attachment but without variac.
Other thing I noticed is if I use analogWrite(pin, 255) the motor is not fully ON (compared to speed achieved by linking gate of IRFZ via resistor in picture directly to +5V)?!
I call MowerSpeed(255); or with 0.
const int Mower = 12;
void MowerSpeed (unsigned char MowerSpeed) {
//0-255 0=off 255=full on
if ((SoftStart == (SoftBOTH - 1)) || (MowerSpeed > 127)) {
for (unsigned char a = 0; a < MowerSpeed; a++) {
analogWrite(Mower, a);
delay(10);
}
SoftStart = 0;
}
analogWrite(Mower, MowerSpeed);
}
for (unsigned char a = 0; a < MowerSpeed; a++) {
analogWrite(Mower, a);
Where did you get the idea that analogWrite() takes an unsigned char as the second argument? It takes an int. Yes, I know that is not reasonable, given the valid range of values that it accepts, but, it is what it is. At least, use byte for the loop index, not unsigned char.