0

How to program an ATtiny45 to use the pins 1, 5, 6, 7 (i.e. PB5, PB0, PB1, PB2) as input or output?

pinMode(5, OUTPUT);
pinMode(0, OUTPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
digitalWrite(5, HIGH);
...

These pins are usually used to program the ATtiny itself, so how to avoid a conflict?

Of course, I could just avoid these 4 pins, but then as there are also pins for VCC and GND, there would be only 2 pins left: PB3, PB4!

Question: how to use the pins as input/output that are usually used for ATtiny programming?

Linked question:

  • Should I connect pins 1, 5, 6, 7 of the ATtiny to the programmer

  • Or should I connect pins 1, 5, 6, 7, as well as 8 and 4 (VCC and GND) of the ATtiny to the programmer?

If the second bullet is correct, when connecting VCC and GND, won't it make the ATtiny run the previously uploaded code?

1
  • Clue: They aren't reserved for programming. They double as the programming pins.
    – Majenko
    Nov 14, 2019 at 17:21

2 Answers 2

1

The ATTiny chips commonly used in Arduino-like boards, such as the Adafruit Trinket or the Digispark USB will usually have a bootloader which runs before your own code. The bootloader checks for a certain state on the USB port of the board, to see if something is waiting to write a new program to the chip.

The bootloader accepts new code from the host computer, if present, and writes it to a certain location in flash memory. If the bootloader does not find anything connected to the USB connector trying to load a new program, then it (the bootloader) starts executing from flash at the location where your program has been stored.

But, what if you want to write a new bootloader, or not have a bootloader? In that case, you can write new software using the MOSI, MISO, and SCK (clock) pins if the RESET pin is pulled to ground.

See: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf#page=151

In order to make use of all 6 non-power pins as I/O for the Trinket or Digispark, a "fuse" is set in the ATTiny chip which disables the RESET pin's normal purpose of holding the chip in reset. This means you cannot pull RESET to ground and load a program the normal way.

If the RESET pin is disabled, and the bootloader is not present, then there is another method for loading software, called "High Voltage Serial Programming". Not to worry, the "high" voltage is is about 12V.

See: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf#page=155

You can find schematics, or purchase kits/products that allow you to program an ATTiny chip in HV mode online. Many use the small 12V "A23" or "A27" batteries commonly found in RF remote control transmitters to get the 12V signal to put the chip in high-voltage programming mode.

4
  • this looks like an answer to OP's other question
    – Juraj
    Nov 14, 2019 at 17:17
  • Both seem to be related... Nov 14, 2019 at 17:18
  • Thank you for your answer!
    – Basj
    Nov 14, 2019 at 18:56
  • @Basj If this answers your question, please mark it. Nov 15, 2019 at 8:35
2

You can just normally use all pins except for the reset pin (pin 0). When you want to program the Attiny, you just connect the programmer. It will reset the Attiny through the reset pin and put it into ISP programming mode. At that point your code hasn't run.

Note: If you want to program the Attiny while it still sits in its circuit, you have to be careful, that the connected circuit does not disturb the ISP communication. The safest way is to disconnect the Attiny from its circuit and then program it.

3
  • Also account for the ISP communication disturbing your connected circuit.
    – Gerben
    Nov 14, 2019 at 16:27
  • Just to be sure: when I use Arduino Uno as ISP programmer for an ATtiny, I usually connect pins 1, 5, 6, 7. Do I also need to connect VCC and GND of the ATtiny to VCC and GND of the Arduino Uno? If so, when connecting VCC and GND, won't it make the ATtiny run the previous code? (I updated the question at the end to include this remark).
    – Basj
    Nov 14, 2019 at 16:32
  • 1
    Yes, you need to also connect GND and VCC. Unless your ATTiny project has it's own power-supply/batteries ( in which case you still need to connect GND). The programmer will pull the reset pin LOW, which will stop your original program from running and enter the ATTiny into "ISP mode".
    – Gerben
    Nov 14, 2019 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.