Attiny84 PWM Not Working Correctly

Hi there, I'm using an Arduino Uno to program an ATtiny84 chip. I'm trying to drive a DC motor, and everything works except that if I set the PWM output to anything lower than 150, it's as if it's set to 0.

Here's my code:

//define the two direction logic pins and the speed / PWM pin
const int DIR_A = 2;
const int DIR_B = 1;
const int PWM = 7;

void setup()
{
//set all pins as output
pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
pinMode(PWM, OUTPUT);
}

void loop()
{
 //drive forward at full speed by pulling DIR_A High
 //and DIR_B low, while writing a full 255 to PWM to 
 //control speed
 digitalWrite(DIR_A, HIGH);
 digitalWrite(DIR_B, LOW);
 analogWrite(PWM, 150);

 //wait 1 second
 delay(1000);

 //Brake the motor by pulling both direction pins to 
 //the same state (in this case LOW). PWM doesn't matter
 //in a brake situation, but set as 0.
 digitalWrite(DIR_A, LOW);
 digitalWrite(DIR_B, LOW);
 analogWrite(PWM, 0);

 //wait 1 second
 delay(2000);

 //change direction to reverse by flipping the states
 //of the direction pins from their forward state
 digitalWrite(DIR_A, LOW);
 digitalWrite(DIR_B, HIGH);
 analogWrite(PWM, 255);

 //wait 1 second
 delay(1000);

 //Brake again
 digitalWrite(DIR_A, LOW);
 digitalWrite(DIR_B, LOW);
 analogWrite(PWM, 0);

 //wait 1 second
 delay(2000);
 }

The above code works, but as I said, if I set any of those analogWrite()s to anything lower than 150, at that point in the code, the motor will brake.

I have my PWM pin attached to physical leg 6 on the ATtiny. Which, according to this, should be pin 7 in the Arduino IDE.

The other side of the PWM wire is connected to pin 1 of this chip

the 2 logic pins go from pins 1 and 2 on the ATtiny to pins 2 and 7 on the H bridge.

I have the chip set to 8mhz. I've also tried it on 4mhz and the PWM set to 50, and then I can hear the motor "buzz" but it doesn't move.

Any ideas why this is happening? TIA!

DC motors can not be controlled easily. The load, torque and friction play an essential role. Use a rotary encoder and PID regulator for controlled speed.

Inappropriate PWM frequency, inadequate motor power supply and/or inadequate driver could result in that behavior.

Motor speed is not linear with PWM fraction, and depends strongly on the motor winding inductance and PWM frequency.

[quote="jremington, post:3, topic:1092255"]
inadequate driver
[/quote] By driver, do you mean my h-bridge or my software used to program the ATtiny?

driver = motor driver. Consider telling us about your setup!

Right, sorry about that. I'm using this H-bridge and this motor

What are you using for a motor power supply? The Arduino 5V output won't work.

Ive tried everything from 3.7v 18650 battery to a 9 volt. They all behave the same. :confused:

Ill make a diagram of my entire circuit when i get off work

Please do post a photo of a hand-drawn wiring diagram, with pins and connections clearly labeled. Don't bother with Fritzing.

As the Sparkfun documentation states, the motor driver will not function at motor voltages below 4.5V, and a 9V smoke alarm battery is totally unsuitable for motors. Do NOT follow any tutorial, including Sparkfun's, that suggests it is OK to use the Arduino 5V output as a motor power supply. You can actually destroy the Arduino doing so.

Try a 4xAA alkaline battery pack, positive connected to Vcc2 of the motor driver (Arduino 5V to the logic supply Vcc1), battery pack negative connected to Arduino GND.

Ah, I was only looking at the specs of the motor, not the driver itself. Rookie mistake lol. Thanks for pointing that out.

I thought you could use the Arduino's voltage as long as you used a diode to protect it?

Anyway, i didnt realize a 9 volt wouldnt work. Im assuming its because of a lack of amperage?

Ill try a better power source, and if that still doesnt fix the issue, ill come back with that circuit diagram. :+1:

Well, looks like that fixed it. Just wasn't getting enough power. Thanks for all the help!

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