Tuning the ATtiny oscillator for serial communication

Submitted by oliver on Wed, 07/13/2016 - 10:30

The internal oscillator of the ATtiny may be inaccurate (variations in the manufacturing process, supply voltage or temperature play a role) which can cause communication with Software Serial to fail. The oscillator can be tuned to be within 1% accuracy by setting the OSCCAL register. I used the following method, described by http://ernstc.dk/arduino/tinytuner.html

If you load this sketch to your ATtiny, it will output the OSCCAL value to the serial monitor. A potentiometer connected to PB3 will vary the OSCCAL value, and you will see the output going from garbled to readable and garbled again. Then you pick a value in the middle of the readable text.

/**
 * Find OSCCAL value for tuning the internal ATtiny oscillator
 */
 
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX (ic pins 5, 6)

void setup() {
  mySerial.begin(9600);
}

void loop() {
  int val = analogRead(3); // potentiometer on PB3 (ic pin 2)
  OSCCAL = val/4;
  mySerial.print("OSCCAL = ");
  mySerial.println(OSCCAL, HEX);
  delay(200);
}

There is an excellent article by Mark Osborne with some in-depth explanations and other methods.