Arduino-tiny Attiny45 PWM pins not working properly

arduinoattinyavrpwm

I am trying to use an Attiny45 as an Arduino such as here http://www.forkrobotics.com/2012/04/run-arduino-code-on-an-attiny/ using the Arduino-tiny cores. Now according to the diagram on the site pin D0, D1, D3 and D4 should have 8bit PWM capabilities but while testing with an LED only D4 works correctly, D3 goes completely on or completely off and D0 and D1 flicker while not completely on or off. I know the tutorial is for the Attiny85 but as far as I can determine the only differnce between it and the 45 should be RAM and flash storage. Also the 8bit is derived from the retailers website which states that there should in fact be 6 8-bit PWM channels https://za.rs-online.com/web/p/microcontrollers/6962614 Do any of you have any idea what could be causing this?

The code is just a version of Blink modified to switch between 100% and 50% duty cycle rather than on or off:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 3;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  //digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  analogWrite(led, 255);
  delay(1000);               // wait for a second
  //digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  analogWrite(led, 127);
  delay(1000);               // wait for a second
}

Best Answer

The ATTiny25/45/85 have three PWM pins, but AFAIK the HLT group's Arduino core only exposes two. There's an entire thread on this subject over on the Arduino forums: http://forums.adafruit.com/viewtopic.php?f=24&t=23937

The datasheet is your go-to reference for these issues. There's a lot of misinformation posted on vendor sites. Learning how to read the datasheets for these devices is an extremely valuable skill. The datasheet is here: http://www.atmel.com/images/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdf

Note: there is also a bug in earlier revisions of the ATTiny45 (prior to Rev. D) that prevents PWM from working properly on pins 3 and 6 (OC1B, OC0B) if PWM is not also enabled on pin 5 (OC0A).