ATtiny85: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
The ATtiny85 is an Atmel 8-bit AVR microcontroller. The ATtiny falls under the tinyAVR series of AVR controllers and is more limited in terms of available memory, IO pins, and peripheral sets when compared to the megaAVR series microcontrollers found in many Arduinos. However, the ATtiny trades these limitations for lower power consumption and smaller form factor. | The ATtiny85 is an Atmel 8-bit AVR microcontroller. The ATtiny falls under the tinyAVR series of AVR controllers and is more limited in terms of available memory, IO pins, and peripheral sets when compared to the megaAVR series microcontrollers found in many Arduinos. However, the ATtiny trades these limitations for lower power consumption and smaller form factor. | ||
== Getting started == | ==Getting started== | ||
===Connecting the ATtiny for ISP=== | ===Connecting the ATtiny for ISP=== | ||
| Line 34: | Line 34: | ||
[[File:ATtiny85 ISP connection.svg|alt=ATtiny85 ISP connection|center|thumb|ATtiny85 ISP connection]] | [[File:ATtiny85 ISP connection.svg|alt=ATtiny85 ISP connection|center|thumb|ATtiny85 ISP connection]] | ||
=== Programming the ATtiny with AVRDUDE === | ===Programming the ATtiny with AVRDUDE=== | ||
AVRDude ('''AVR D'''ownloader/'''U'''ploa'''DE'''r) is a utility that can program AVR microcontrollers using ISP. Once the appropriate connections are made, we can use avrdude to read/write to the microcontroller. | AVRDude ('''AVR D'''ownloader/'''U'''ploa'''DE'''r) is a utility that can program AVR microcontrollers using ISP. Once the appropriate connections are made, we can use avrdude to read/write to the microcontroller. | ||
avrdude takes some required options: | avrdude takes some required options: | ||
* <code>-c programmer-id</code> - specify the programmer you are using. When using the Arduino as the ISP programmer, you may use the <code>avrisp</code> or <code>arduino</code> (which appears to be aliases for <code>stk500</code>) as the programmer. | *<code>-c programmer-id</code> - specify the programmer you are using. When using the Arduino as the ISP programmer, you may use the <code>avrisp</code> or <code>arduino</code> (which appears to be aliases for <code>stk500</code>) as the programmer. | ||
* <code>-p part-number</code>- the board you are targeting | *<code>-p part-number</code>- the board you are targeting | ||
* <code>-P port</code> - the device or port that is connected to the ISP programmer. On Windows, it is probably some <code>COM#</code> port. On Linux, it's likely to be <code>/dev/ttyUSB#</code>. | *<code>-P port</code> - the device or port that is connected to the ISP programmer. On Windows, it is probably some <code>COM#</code> port. On Linux, it's likely to be <code>/dev/ttyUSB#</code>. | ||
* <code>-B bit-clock</code> - Used to adjust the SPI speed. Default is 10, for 100KHz. KHz = 1000/(1.5+B). | *<code>-B bit-clock</code> - Used to adjust the SPI speed. Default is 10, for 100KHz. KHz = 1000/(1.5+B). | ||
Some useful commands to be aware of: | Some useful commands to be aware of: | ||
| Line 50: | Line 50: | ||
|- | |- | ||
|Read fuse settings | |Read fuse settings | ||
|<code>avrdude -c avrisp -b 19200 -p attiny85 -P /dev/ttyUSB0 -U lfuse:r:-:i</code> | |<code>avrdude -c avrisp -b 19200 -p attiny85 -P /dev/ttyUSB0 -U lfuse:r:-:i</code> | ||
|- | |- | ||
|Change fuse settings | |Change fuse settings | ||
| Line 61: | Line 61: | ||
|} | |} | ||
== Other notes == | ==Other notes== | ||
=== ATtiny configuration fuses === | ===ATtiny configuration fuses=== | ||
There are some configuration settings that can be set on the ATtiny. These settings can change the behavior of the microcontroller such as changing its clock speed or using an external clock source. | There are some configuration settings that can be set on the ATtiny. These settings can change the behavior of the microcontroller such as changing its clock speed or using an external clock source. | ||
| Line 69: | Line 69: | ||
There is a fuse calculator at <nowiki>https://www.engbedded.com/fusecalc/</nowiki> for a wide array of AVR microcontrollers. | There is a fuse calculator at <nowiki>https://www.engbedded.com/fusecalc/</nowiki> for a wide array of AVR microcontrollers. | ||
=== Comparison with other ATtiny microprocessors === | |||
The distinction between the other ATtiny microprocessors are: | The distinction between the other ATtiny microprocessors are: | ||
{| class="wikitable" | {| class="wikitable" | ||
| Line 126: | Line 98: | ||
|12 | |12 | ||
|} | |} | ||
== Tasks == | |||
===Implementing the crazy clock with an ATtiny85=== | |||
The crazy clock project at https://github.com/nsayer/Crazy-Clock uses an ATtiny85 to change the behavior of clock movements driven by a lavet stepper motor. This is done by disconnecting the microcontroller in these clock movements and connecting the lavet stepper motor directly to the ATtiny85. | |||
This project includes some interesting clock behaviors such that ticks happen irregularly while still maintaining time overall. | |||
To reduce power consumption, an external 32.768KHz clock is used with the ATtiny85. Two pins are used to drive the lavet stepper motor. | |||
==== Quick guide ==== | |||
The bill of materials to get your own crazy clock are: | |||
* 2x Schottky diodes | |||
* 2x 100 Ohm resistors, 1x 10K resistor | |||
* 2x ~15 pF capacitor | |||
* 1x 32.768KHz crystal | |||
* 1x ATtiny85 | |||
* ISP programmer (an Arduino would do) | |||
* A random clock that uses a lavet stepper motor | |||
* breadboard and wires | |||
Then, | |||
# Clone the crazy clock git repo. Install <code>avrdude</code>. | |||
# Wire up the ATtiny to the ISP programmer. Connect the crystal oscillator to XALT1/XALT2 with the 15pF capacitor on both pins tied to ground. Set the reset pin high using the 10K resistor. | |||
# Disconnect the lavet stepper motor from the existing microcontroller in the clock movement. Connect PB1 and PB2 to the lavet stepper motor via 100 Ohm resistors. A flyback diode should be added to ground. | |||
# Upload crazy clock to the ATtiny. This part is a little tricky since you need to change the fuses first, then adjust the programmer to use the slower SPI clock speed. First, set the fuses: | |||
{{Highlight | |||
| code = $ make fuse | |||
| lang = terminal | |||
}} After setting the fuses, make sure your programmer is using a speed of 250. If using ArduinoISP, change the line <code>#define SPI_CLOCK (1000000/6)</code> to <code>#define SPI_CLOCK (1500/6)</code>. For some reason, <code>-B 250</code> to <code>avrdude</code> doesn't seem to do the trick. | |||
# Then, upload the desired clock with <code>make flash</code>: | |||
{{Highlight | |||
| code = $ make flash TYPE=crazy | |||
| lang = terminal | |||
}} This step will take a while. If your clock is connected, the second hand should be going back and forth randomly, which is sort of interesting to look at. When avrdude tries to verify the flash by reading back the contents, it will fail. This seems to be OK. | |||
# Seed the randomness to the EEPROM. | |||
{{Highlight | |||
| code = $ make seed | |||
| lang = terminal | |||
}} | |||
If all goes well, the clock should begin ticking using the chosen clock type. | |||
== Troubleshooting == | |||
=== programmer not responding === | |||
When using <code>avrdude</code> using an Arduino running the ArduinoISP sketch as the ISP programmer, I got: | |||
{{Highlight | |||
| code = avrdude: ser_recv(): programmer is not responding | |||
avrdude: stk500_recv(): programmer is not responding | |||
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00 | |||
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0xe0 | |||
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0xe0 | |||
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0xe0 | |||
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00 | |||
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0xe0 | |||
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0xe0 | |||
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0xe0 | |||
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00 | |||
avrdude: ser_recv(): programmer is not responding | |||
avrdude: stk500_recv(): programmer is not responding | |||
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00 | |||
| lang = text | |||
}} | |||
You need to make sure that <code>avrdude</code> is talking to the Arduino at 19200 baud. When calling <code>avrdude</code>, pass in <code>-b 19200</code>. | |||
=== flash failes due to invalid device signature === | |||
{{Highlight | |||
| code = avrdude -c avrisp -p attiny85 -B 250 -P /dev/ttyUSB0 -b 19200 -U flash:w:crazy.hex | |||
avrdude: AVR device initialized and ready to accept instructions | |||
Reading {{!}} ################################################## {{!}} 100% 0.04s | |||
avrdude: Device signature = 0x000000 (retrying) | |||
Reading {{!}} ################################################## {{!}} 100% 0.05s | |||
avrdude: Device signature = 0x000000 (retrying) | |||
Reading {{!}} ################################################## {{!}} 100% 0.04s | |||
avrdude: Device signature = 0x000000 | |||
avrdude: Yikes! Invalid device signature. | |||
Double check connections and try again, or use -F to override | |||
this check. | |||
| lang = text | |||
}} | |||
This happened after changing the fuses to use an external clock. Change the SPI speed in the ArudinoISP sketch to use 1500/6 if using a 32KHz oscillator with no prescaling. Change the line <code>#define SPI_CLOCK (1000000/6)</code> to <code>#define SPI_CLOCK (1500/6)</code> and reupload the ArduinoISP sketch before trying again. | |||
== See Also == | |||
*https://www.electroschematics.com/attiny85-pwm-primer-tutorial-using-arduino/ | |||
*https://circuitdigest.com/microcontroller-projects/attiny85-ic-programming-through-usb-using-digispark-bootloader | |||
<br /> | |||
Revision as of 22:00, 31 July 2021
The ATtiny85 is an Atmel 8-bit AVR microcontroller. The ATtiny falls under the tinyAVR series of AVR controllers and is more limited in terms of available memory, IO pins, and peripheral sets when compared to the megaAVR series microcontrollers found in many Arduinos. However, the ATtiny trades these limitations for lower power consumption and smaller form factor.
Getting started
Connecting the ATtiny for ISP
In-system programming (ISP) is used for programming the ATtiny. This is also sometimes referred to as ICSP (In-circuit serial programming).
The ATtiny85 in the DIP/SOIC/SOP package has 8 pins. The pinouts are shown on the right. To program the ATtiny, we will be using ISP via pins 5, 6, and 7 (outlined in orange in the diagram). Any ISP programmer will do, but we will be using an Arudino running the ArduinoISP sketch which can be found from the examples menu.
Once the ArduinoISP sketch is programmed on the Arduino, connect the ATtiny to the Arduino based on the table below.
| Arduino Pin | Function | Connect to ATtiny85 Pin |
|---|---|---|
| 10 | Reset | 1 |
| 11 | MOSI | 5 |
| 12 | MISO | 6 |
| 13 | SCK | 7 |
Connect ground and power to the ATtiny via the Arudino. You may use either 5 or 3.3 volts. A 10uF capacitor should also be connected between ground and the Arudino reset pin to prevent the Arduino from resetting during the ISP programming. At the end, it should look something like the following:
Programming the ATtiny with AVRDUDE
AVRDude (AVR Downloader/UploaDEr) is a utility that can program AVR microcontrollers using ISP. Once the appropriate connections are made, we can use avrdude to read/write to the microcontroller.
avrdude takes some required options:
-c programmer-id- specify the programmer you are using. When using the Arduino as the ISP programmer, you may use theavrisporarduino(which appears to be aliases forstk500) as the programmer.-p part-number- the board you are targeting-P port- the device or port that is connected to the ISP programmer. On Windows, it is probably someCOM#port. On Linux, it's likely to be/dev/ttyUSB#.-B bit-clock- Used to adjust the SPI speed. Default is 10, for 100KHz. KHz = 1000/(1.5+B).
Some useful commands to be aware of:
| Task | Command |
|---|---|
| Read fuse settings | avrdude -c avrisp -b 19200 -p attiny85 -P /dev/ttyUSB0 -U lfuse:r:-:i
|
| Change fuse settings | avrdude -c avrisp -b 19200 -p attiny85 -P /dev/ttyUSB0 -U lfuse:w:0xXX:m -U hfuse:w:0xXX:m -U efuse:w:0xXX:m
Be careful when setting the fuses for an external clock. You may need to adjust the SPI speed in your programmer or via the -B option in avrdude. |
| Write flash | avrdude -c avrisp -b 19200 -p attiny85 -P /dev/ttyUSB0 -U flash:w:firmware.hex
|
Other notes
ATtiny configuration fuses
There are some configuration settings that can be set on the ATtiny. These settings can change the behavior of the microcontroller such as changing its clock speed or using an external clock source.
Despite its name, these fuses are really just flash/EEPROM values used as configuration parameters. You may change these values at a later time and are values don't actually blow any fuses.
There is a fuse calculator at https://www.engbedded.com/fusecalc/ for a wide array of AVR microcontrollers.
Comparison with other ATtiny microprocessors
The distinction between the other ATtiny microprocessors are:
| ATTiny25 | ATTiny45 | ATTiny85 | |
|---|---|---|---|
| Programmable Flash Memory (10k write/erase) | 2K | 4K | 8K |
| EEPROM (100k write/erase) | 128 bytes | 256 bytes | 512 bytes |
| SRAM | 128 bytes | 256 bytes | 512 bytes |
| Program Counter bits wide | 10 | 11 | 12 |
Tasks
Implementing the crazy clock with an ATtiny85
The crazy clock project at https://github.com/nsayer/Crazy-Clock uses an ATtiny85 to change the behavior of clock movements driven by a lavet stepper motor. This is done by disconnecting the microcontroller in these clock movements and connecting the lavet stepper motor directly to the ATtiny85.
This project includes some interesting clock behaviors such that ticks happen irregularly while still maintaining time overall.
To reduce power consumption, an external 32.768KHz clock is used with the ATtiny85. Two pins are used to drive the lavet stepper motor.
Quick guide
The bill of materials to get your own crazy clock are:
- 2x Schottky diodes
- 2x 100 Ohm resistors, 1x 10K resistor
- 2x ~15 pF capacitor
- 1x 32.768KHz crystal
- 1x ATtiny85
- ISP programmer (an Arduino would do)
- A random clock that uses a lavet stepper motor
- breadboard and wires
Then,
- Clone the crazy clock git repo. Install
avrdude. - Wire up the ATtiny to the ISP programmer. Connect the crystal oscillator to XALT1/XALT2 with the 15pF capacitor on both pins tied to ground. Set the reset pin high using the 10K resistor.
- Disconnect the lavet stepper motor from the existing microcontroller in the clock movement. Connect PB1 and PB2 to the lavet stepper motor via 100 Ohm resistors. A flyback diode should be added to ground.
- Upload crazy clock to the ATtiny. This part is a little tricky since you need to change the fuses first, then adjust the programmer to use the slower SPI clock speed. First, set the fuses:
$ make fuse
After setting the fuses, make sure your programmer is using a speed of 250. If using ArduinoISP, change the line #define SPI_CLOCK (1000000/6) to #define SPI_CLOCK (1500/6). For some reason, -B 250 to avrdude doesn't seem to do the trick.
- Then, upload the desired clock with
make flash:
$ make flash TYPE=crazy
This step will take a while. If your clock is connected, the second hand should be going back and forth randomly, which is sort of interesting to look at. When avrdude tries to verify the flash by reading back the contents, it will fail. This seems to be OK.
- Seed the randomness to the EEPROM.
$ make seed
If all goes well, the clock should begin ticking using the chosen clock type.
Troubleshooting
programmer not responding
When using avrdude using an Arduino running the ArduinoISP sketch as the ISP programmer, I got:
avrdude: ser_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0xe0
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0xe0
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0xe0
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0xe0
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0xe0
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0xe0
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: ser_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
You need to make sure that avrdude is talking to the Arduino at 19200 baud. When calling avrdude, pass in -b 19200.
flash failes due to invalid device signature
avrdude -c avrisp -p attiny85 -B 250 -P /dev/ttyUSB0 -b 19200 -U flash:w:crazy.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.04s
avrdude: Device signature = 0x000000 (retrying)
Reading | ################################################## | 100% 0.05s
avrdude: Device signature = 0x000000 (retrying)
Reading | ################################################## | 100% 0.04s
avrdude: Device signature = 0x000000
avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.
This happened after changing the fuses to use an external clock. Change the SPI speed in the ArudinoISP sketch to use 1500/6 if using a 32KHz oscillator with no prescaling. Change the line #define SPI_CLOCK (1000000/6) to #define SPI_CLOCK (1500/6) and reupload the ArduinoISP sketch before trying again.
See Also
- https://www.electroschematics.com/attiny85-pwm-primer-tutorial-using-arduino/
- https://circuitdigest.com/microcontroller-projects/attiny85-ic-programming-through-usb-using-digispark-bootloader