Instructables

Programming the ATtiny85 from Raspberry Pi

Picture of Programming the ATtiny85 from Raspberry Pi
These instructions tell you how to setup and program the ATtiny85 microcontroller from a Raspberry Pi via the SPI interface. Lots of people use the Ardiuno to do this (then you can use the Arduino IDE and simplified C commands), or you can use a USB based programmer. I do not have an Ardiuno and don't want to buy a dedicated programmer. I do have a Pi, so I was pleased to learn I could use it as a way to get into microcontroller programming.

You will need:
Raspiberry Pi
ATtiny85 chip
5 x 1K resistors (or similar)
LED of your choice
A connection to the GPIO of the Pi, and a breadboard and wire.

Based on http://www.instructables.com/id/How-to-program-ATt...
and http://www.raspberrypi.org/phpBB3/viewtopic.php?t=...
and http://www.instructables.com/id/RGB-LED-Mood-Light...
http://www.instructables.com/id/ATTiny4585-LCD-display-control-with-a-shift-regis/step3/Programming-the-ATTiny85-with-a-test-program/




 
Remove these adsRemove these ads by Signing Up

Step 1: Setup the Raspberry Pi

Picture of Setup the Raspberry Pi
At the terminal of the Pi:

Download and build avrdude

sudo apt-get install bison automake autoconf flex git gcc
sudo apt-get install gcc-avr binutils-avr avr-libc
git clone https://github.com/kcuzner/avrdude
cd avrdude/avrdude
./bootstrap && ./configure && sudo make install

Setup SPI on the GPIO

sudo raspi-config
and Enable SPI device in the Advanced Options (see picture)
You can check this at the command line with lsmod, no need to reboot. (Maybe need to sudo modprobe spidev)


Download and build WiringPi for the gpio commands

cd ~
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

Step 2: Electrical Connections

Picture of Electrical Connections
Connect up the ATtiny85 to the Raspberry Pi GPIO (wire colours from the picture are given for reference):

GPIO pin ATtiny pin Comment
15            1                GPIO22 to Reset (through 1K, Blue wire)
17            8                3.3 V (Green wire)
19            5                MOSI (through 1K, Yellow wire)
21            6                MISO (through 1K, Orange wire)
23            7                SCLK (through 1K, Red wire)
25            4               GND (Brown wire)

(I could not find a way to do a nice table in instructables)

Step 3: Test avrdude connection

Picture of Test avrdude connection
Test avrdude connection to the ATtiny85, we are set up with GPIO pin 22 on the ATtiny reset. We must pull this pin low to program the chip. This can be done in other ways, e.g. a switch, but I an using another pin of the GPIO to do this.

sudo gpio -g mode 22 out
sudo gpio -g write 22 0
sudo avrdude -p t85 -P /dev/spidev0.0 -c linuxspi -b 10000
sudo gpio -g write 22 1

This must give success type messages!


Step 4: Program the ATtiny85

Picture of Program the ATtiny85
Program the ATtiny85:

cd ~
mkdir ATtiny85
cd ATtiny85
mkdir blinky
cd blinky

create the blinky.c file and add the following code
nano blinky.c
////////////////////////
#define F_CPU 1000000L
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRB = 0xFF; // PORTB is output, all pins
PORTB = 0x00; // Make pins low to start

for (;;) {
PORTB ^= 0xFF; // invert all the pins
_delay_ms(100); // wait some time
}
return 0;
}
////////////////////////

add this code to a Makefile file
nano Makefile
///////////////////////
MCU=attiny85
AVRDUDEMCU=t85
CC=/usr/bin/avr-gcc
CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=$(MCU)
OBJ2HEX=/usr/bin/avr-objcopy
AVRDUDE=/usr/local/bin/avrdude
TARGET=blinky
all :
$(CC) $(CFLAGS) $(TARGET).c -o $(TARGET)
$(OBJ2HEX) -R .eeprom -O ihex $(TARGET) $(TARGET).hex
rm -f $(TARGET)

install : all
sudo gpio -g mode 22 out
sudo gpio -g write 22 0
sudo $(AVRDUDE) -p $(AVRDUDEMCU) -P /dev/spidev0.0 -c linuxspi -b 10000 -U flash:w:$(TARGET).hex
sudo gpio -g write 22 1
noreset : all
sudo $(AVRDUDE) -p $(AVRDUDEMCU) -P /dev/spidev0.0 -c linuxspi -b 10000 -U flash:w:$(TARGET).hex

fuse :
sudo gpio -g mode 22 out
sudo gpio -g write 22 0
sudo $(AVRDUDE) -p $(AVRDUDEMCU) -P /dev/spidev0.0 -c linuxspi -b 10000 -U lfuse:w:0x62:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m
sudo gpio -g write 22 1

clean :
rm -f *.hex *.obj *.o
///////////////////////
(Sorry, the instructables text editor has destroyed all the tabs and spacing in the code above, the Makefile will not work without tabs in the correct place)

To compile type:
make
To compile and upload code to the ATtiny:
make install
To optionally send fuses:
make fuse

This program oscillates ALL 5 pins as outputs so the LED should flash.
We are programming the ATtiny directly using avr-libc.
To change the setup of the ATtiny get fuses from : https://github.com/kcuzner/avrdude
spirail made it!21 days ago

Works good! thankyou!

I made it work with my custom ATmega32u4 board. I had to supply it with an external 3.3v supply.

Later I plan to supply with wiht USB power (5V) but im still waiting for my level converters to come in the mail.

20140702_120454.jpg
vpopov11 month ago

Hello and Thank you for your Tutorial.

But i have a few problems.

When i run make install it flashes ok, but i receive failure:

"avrdude verification error content mismatch"

and "avrdude safe mode lfuse changed"

What i doing wrong ?

I am trying to programm attiny45

vpopov11 month ago

Hello and thank you for tutorial.

But i have a problem. I would like to program Attiny45 and when i run make install ist says :

avrdude verification error content mismatch

avrdude safe mode louse changed, was 62 and now is 0

and when i set y to change fuse back then it freeze ...

Could you please help me

mva111 made it!1 month ago

Very straightforward tutorial. Used it with the ATtiny25 and worked well. Thank you!

IMAG2126.jpg
fubar12 months ago

Many thanks! Your suggestion was spot on!!!!

fubar12 months ago

Hello, I've followed your tutorial and get as far as 'Makefile'. When I run the 'make' command I get .....

'Makefile:9: *** missing separator. Stop'

Any help would be appreciated.

prb3333 (author)  fubar12 months ago

Probably a lack of tabs. You need a hard tab, or a ';' before each command. Where there is a gap at the beginning of the line in the picture of the code, make sure this is a hard tab. See also:

http://stackoverflow.com/questions/14109724/makefi...

Hope this helps.

prb3333 (author)  fubar12 months ago

Probably a lack of tabs. You need a hard tab, or a ';' before each command. Where there is a gap at the beginning of the line in the picture of the code, make sure this is a hard tab. See also:

http://stackoverflow.com/questions/14109724/makefi...

Hope this helps.

ajohnson134 made it!2 months ago

Great tutorial! All of the steps were very clear. The wiring diagrams were a big help. Took me no time to set this up. Everything worked on my first try (well... almost everything. I accidentally mixed up two ports).

IMG_0035.jpg

And technically I'm using ATtiny25, but switching processors was actually fairly trivial. :)

harnon6 months ago

I wasn't fully done before...I just got it all working now. Great !
It is flashing.

Great job man.

one more 'enter' for separate "cd ~ mkdir ATtiny85" and "cd blinky" for easy line copying newbies like me ;o)

You did the most understable tuto I got for this. Hope you will have success.

prb3333 (author)  harnon5 months ago

Thanks, clarified that more. As with all instructables:

The more we use it, the more we improve it.

Cheers,

harnon6 months ago

Hello,

Really nice step by step instructable. I searched hours for it.Thanks !

2 suggestions for perfect cut & paste tutorial :

"sudo raspi-config" instead of " sudo raspi-coinfig"

"cd ~ && git clone git://git.drogon.net/wiringPi" instead of "cd ~ git clone git://git.drogon.net/wiringPi"

This allowed me to continue the tuto.

Have a nice day.

prb3333 (author)  harnon6 months ago

Hi,

Thanks for the comments, glad it was of help. Corrections made.

Cheers,

dan30086 months ago

is it ironic that I want to do this, so I can make an USB AVR programer? (http://www.instructables.com/id/VUSBTiny-AVR-SPI-Programmer/?&sort=ACTIVE&limit=40&offset=40#DISCUSS) lol

prb3333 (author)  dan30086 months ago

Yes, I for one did not think it was possible to interface to the ATtiny via USB to make an AVR programmer. That's awesome!

dan3008 prb33336 months ago

Yeh it is, and alot cheaper than a standard usb AVR programmer. Its limited on what chips it works with, but should save alot of time and effort in the future :D

Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!