I ordered few ATtiny microcontrollers from Aliexpress to build some low power door sensors. Programming these using Arduino was not straight forward as these ATtiny came with some issues like wrong fuse bits and lock bits set. Here is how I identified and recovered issues.
Arduino IDE
Arduino UNO
WeMosD1 mini
ATtiny development board
Breadboard, Resistors. NPN Transistor, few wires
Linux
Follow this nice artical to program ATtiny13 Arduino Uno.
https://www.hackster.io/taunoerik/programming-attiny13-with-arduino-uno-07beba
If all works, you are lucky and no need to read further.
In my case Arduino gave error in writing bootloader and so it couldn't upload the sketch too.
Later it turned out that RESET was disabled in fuse bits of ATtiny. Lets find out how to read FUSE bits...
To read/write FUSE avrdude is needed. Arduino IDE installs avrdude on windows by default but this avrdude installation on windows has some limitations and was not able to read fuses. So I took a detour, it was long but worked perfactly and gave advantage of wireless interface to read/write ATtiny.
Grab a wemos or any compatible esp8266 board.
Select Board Type from
tools menu.
Open AVRISP from Examples --> ESP8266AVRISP
Input your
SSID and wifi password in sketch.
By default sketch usage
"reset_pin=5" which is GPIO5 that is D1 pin in wemos.
Connection with ATtiny13:
ATtiny | Wemosd1 R2 mini | Pin Function |
8 (VCC) | 3.3v | VCC |
4 (Ground) | G | Ground |
1 (PB5) | D1 | RESET |
5 (PB0) | D7 | MOSI |
6 (PB1) | D6 | MISO |
7 (PB2) | D5 | SCK |
I used virtual box and created a CentOS virtual machine. You can use any linux machine. We just need the avrdude installed and reachable from our arduino machine.
# yum install avrdude
note down path of avrdude.conf. (replace in avrdude commands if it's different then "/etc/avrdude/avrdude.conf"
# find / -name 'avrdude.conf'
Connect ATtiny and wemos
Power on wemos loaded with AVRISP
Open
serial connection to wemos and note down it's IP address
Ping wemos
IP from linux machine where you installed avrdude, ensure it is
reachable
Give following command in linux to read FUSE. (Replace IP
and Port with your values)
# avrdude -C /etc/avrdude/avrdude.conf -v -pATtiny13 -carduino -P net:192.168.1.39:328 -D -U lfuse:r:-:i -v
If you want to read arduino uno (cpu is atmega328p) fuses, use
following command
# avrdude -C /etc/avrdude/avrdude.conf -v
-patmega328p -carduino -P net:192.168.1.39:328 -b115200 -D -U
lfuse:r:-:i -v
So now you have got the FUSE values, now head on to http://www.engbedded.com/fusecalc/ and decode it.
My ATtiny13v had initially LFuse: 4A, HFuse: F8
It means "RESET
DISABLED". Normal ISP programmers need RESET ENABLED to program ATtiny.
I have no choice other than build a HVSP (High Voltage Serial
Programmer) to enable reset.
Look at this well explained artical with a simple circuit diagram
https://github.com/shariramani/AVR-High-voltage-Serial-Fuse-Reprogrammer
Get HighVoltageProgrammer.ino, upload this to arduino uno and run it as instructed in above link.
Look at Serial Monitor output above, I got same FUSE bits before and
after writing (LFuse: 4A, HFuse: F8, EFuse: FF). Looks like this HVSP is
not working .....
One more surprise awaits here, the lock bits are
preventing HVSP to write anything on this chip. To solve this issue, we
need to erase Flash and Lock bits.
I added chip erase fuction (to reset lock bits and erase flash) to recover my attiny13v chip. Erase function is commented in code. Uncomment it if you encounter same problem as mine.
// Serial.println("Erasing flash and lock
bits...");
//
chip_erase(); // uncomment chip_erase to reset lock bits and erase
flash. Sometimes lock bits are set to prevent read/write flash.
Run HVSP again to erase flash and reset the FUSE and LOCK bits, verify in serial output. Now go back to step-1 "Programming ATtiny13 with Arduino Uno" or Use wemos for uploading program to ATtiny.
In Arduino IDE enable verbose output
File --> Preferences
Look for
setting "Show verbose output during" and enable it for "upload"
Select your ATtiny board from Tools menu
Open your sketch
Set
clock and BOD
Go to Sketch --> Export compiled Binary
Upload
Upload will fail but you will get avrdude command for uploading program
hex file in the lower pane of Arduino IDE (Note the command just for
reference)
Locate the Hex file created by Arduino IDE. The path is in the the avrdude upload command in lower pane or Go to Sketch --> Show Sketch folder
FTP hex file to Linux machine installed with avrdude.
Power on wemos loaded with ESP8266AVRISP. Here is the command for uploading the Hex file to ATtiny.
# avrdude -C /etc/avrdude/avrdude.conf -v -pattiny13 -carduino -P net:192.168.1.39:328 -D -Uflash:w:/home/admin/ATtiny13-transmitter-433Mghz.ino_attiny13a_1200000L.hex:i
Modify above command with your cpu type, IP, port, file name and paths
Ahh, one more error is in the way. Many times I got verification errors
avrdude: verification error, first mismatch at byte 0x0000
0x09 !=
0x00
avrdude: verification error; content mismatch
Solution is to remove -D flag and try command without "-D" flag and with "-D" flag, one after other. D flag is to disable flash memory erase cycle. This did the trick to upload succesfully.
# avrdude -C /etc/avrdude/avrdude.conf -v -pattiny13 -carduino -P net:192.168.1.39:328 -Uflash:w:/home/admin/ATtiny13-transmitter-433Mghz.ino_attiny13a_1200000L.hex:i
All the best!
I am an IOT enthusiast with more than 20 years of experience in the IT sector. Specializing in telecom service's; follow me for some very innovative and best in class IOT products as I unfold my knowledge and passion for the subject.
Just me, myself and I, exploring the universe of uknownment. I have a heart of love and interests in technology, IOT and travel . And I want to share my world with you .