How To Set The Clock Speed Fuses On An ATtiny85 When Using An Arduino As A Programmer?

11 min read Sep 26, 2024
How To Set The Clock Speed Fuses On An ATtiny85 When Using An Arduino As A Programmer?

The ATtiny85 is a popular microcontroller for small projects due to its compact size, low power consumption, and versatility. However, to fully utilize its capabilities, understanding how to set the clock speed is crucial. This involves using fuses, which are specific settings stored in the microcontroller's memory, to determine the operating frequency. This article will delve into the process of setting the clock speed fuses on an ATtiny85 using an Arduino as a programmer.

Understanding Clock Speed and Fuses

The clock speed of a microcontroller determines how fast it can execute instructions. The ATtiny85 can operate at various clock speeds, ranging from a few kilohertz (kHz) to several megahertz (MHz), depending on the specific settings chosen. The fuses are essentially configuration bits stored in the microcontroller's non-volatile memory, affecting its operation in various ways, including the clock speed. These settings persist even after power is removed, ensuring that the desired operating frequency remains in place.

Setting Up Your Arduino and ATtiny85

Before starting the fuse configuration process, you'll need to have the following set up:

  • An Arduino board: Any Arduino board can be used as a programmer for the ATtiny85.
  • An ATtiny85 microcontroller: Ensure you have the ATtiny85 chip itself.
  • Connecting wires: You'll need a few jumper wires to establish communication between the Arduino and the ATtiny85.

Wiring the Arduino and ATtiny85

  1. Power: Connect the VCC pin of the ATtiny85 to the 5V pin on the Arduino.
  2. Ground: Connect the GND pin of the ATtiny85 to the GND pin on the Arduino.
  3. Reset: Connect the RESET pin of the ATtiny85 to the digital pin 10 on the Arduino.
  4. Data: Connect the MOSI pin of the ATtiny85 to the digital pin 11 on the Arduino.
  5. Clock: Connect the SCK pin of the ATtiny85 to the digital pin 13 on the Arduino.

Installing the avrdude Library

The avrdude library is essential for communicating with the ATtiny85 and programming its fuses. It is typically included with the Arduino IDE. If it's not already installed, you can install it from the Arduino IDE by going to Sketch -> Include Library -> Manage Libraries... and searching for "avrdude".

Setting the Clock Speed Fuses with avrdude

Once you have the necessary hardware and software set up, you can start programming the fuses. This is typically done using the command line interface or through an Arduino IDE sketch.

Using the Command Line Interface

  1. Open a terminal or command prompt: You can access the terminal through your operating system's applications.

  2. Navigate to the Arduino IDE's tools directory: This is typically located in the Arduino IDE's installation folder under "hardware\tools\avr\bin".

  3. Run the avrdude command: You'll need to provide the following arguments:

    avrdude -c arduino -p attiny85 -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m -U efuse:w:0xFF:m
    
    • -c arduino: Specifies the programmer type as Arduino.
    • -p attiny85: Indicates the microcontroller type as ATtiny85.
    • -U: This flag is used to program a specific fuse.
    • lfuse:w:0xFF:m: Programs the low fuse with a value of 0xFF.
    • hfuse:w:0xD9:m: Programs the high fuse with a value of 0xD9.
    • efuse:w:0xFF:m: Programs the extended fuse with a value of 0xFF.

    Note: These fuse values correspond to the internal 8 MHz oscillator being selected as the clock source. You can find specific fuse values for different clock speeds in the ATtiny85 datasheet.

Using an Arduino Sketch

Alternatively, you can program the fuses using an Arduino sketch. This offers a more user-friendly approach, especially for beginners. Here's a simple sketch that sets the fuses for the internal 8 MHz oscillator:

#include 

const char* programmer = "arduino"; // Programmer type
const char* device = "attiny85";    // Microcontroller type

void setup() {
  // Set the fuses
  avrdude_program_fuses(programmer, device, 0xFF, 0xD9, 0xFF);
}

void loop() {
  // Do nothing in the loop
}

This sketch utilizes the avrdude library to program the fuses. The avrdude_program_fuses() function takes the programmer type, microcontroller type, and fuse values as arguments. After uploading this sketch to your Arduino board, the ATtiny85's fuses will be programmed with the desired values.

Understanding the Fuse Values

Each fuse has a specific function and can be programmed with a hexadecimal value.

Low Fuse (LFUSE)

The low fuse controls various settings, including:

  • CKDIV8: This bit determines the clock prescaler. Setting it to 1 divides the clock frequency by 8, providing slower clock speeds.
  • SPIEN: Enables the SPI communication interface when set to 1.
  • WDTON: Enables the watchdog timer when set to 1.
  • EESAVE: Allows the EEPROM to be preserved when set to 1, even during power failure.

High Fuse (HFUSE)

The high fuse handles settings like:

  • BODLEVEL: This determines the Brown-out Detection (BOD) voltage.
  • SUT: Sets the startup time for the device, affecting the time it takes to boot after power-up.
  • CKSEL: This setting is crucial for selecting the clock source. For internal 8 MHz, you'll typically set it to 0xD9.

Extended Fuse (EFUSE)

The extended fuse provides additional configuration options, such as:

  • BOOTRST: Enables the bootloader when set to 1, allowing you to upload new code without a physical programmer.
  • RSTDISBL: Disables the reset pin when set to 1.

Verification and Troubleshooting

After programming the fuses, it's essential to verify that the desired clock speed has been set correctly. You can do this by:

  • Using a logic analyzer: This tool can capture the clock signal from the ATtiny85 and display its frequency.
  • Writing a simple sketch: Create a program that blinks an LED at a specific frequency. If the blink rate matches the expected value based on the chosen clock speed, it indicates that the fuses are set correctly.

Troubleshooting:

  • Verify the wiring: Double-check that the connections between the Arduino and ATtiny85 are secure and correct.
  • Check the fuse values: Ensure you've entered the correct fuse values for your desired clock speed.
  • Try different programmers: If you're using a command line approach, attempt using a different programmer type, like the "usbasp" programmer.

Conclusion

Programming the clock speed fuses on the ATtiny85 using an Arduino as a programmer is a crucial step for optimizing your project. Understanding the different fuse settings and their impact on the microcontroller's behavior is essential for achieving the desired performance. By carefully following the steps outlined in this article, you can confidently set the clock speed of your ATtiny85 and unlock its full potential.