Programming the ATtiny85 From Raspberry Pi
Intro: 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 https://www.instructables.com/id/How-to-program-ATt...
and http://www.raspberrypi.org/phpBB3/viewtopic.php?t=...
and https://www.instructables.com/id/RGB-LED-Mood-Light...
https://www.instructables.com/id/ATTiny4585-LCD-display-control-with-a-shift-regis/step3/Programming-the-ATTiny85-with-a-test-program/
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 https://www.instructables.com/id/How-to-program-ATt...
and http://www.raspberrypi.org/phpBB3/viewtopic.php?t=...
and https://www.instructables.com/id/RGB-LED-Mood-Light...
https://www.instructables.com/id/ATTiny4585-LCD-display-control-with-a-shift-regis/step3/Programming-the-ATTiny85-with-a-test-program/
STEP 1: 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
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
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)
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
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!
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
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
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
32 Comments
bullstreetboy 5 years ago
I sorted the tabs by looking at the screen dump of step 4.
STEMROX 7 years ago
I am lost when it comes to coding... Where should the TABS go? Can someone please post a picture of the correct code?
prb3333 7 years ago
Hi,
I have the code on Github now:
https://github.com/paulbarber/ATtiny85_RaspberryPi...
Cheers!
NeilC41 8 years ago
Thankyou so much! I had purchased 10x ATtiny84a's and had no idea, until I stumbled upon this. Much Appreciated:)
LarsO2 8 years ago
Hi, this helped me to get going, but you should update/change one thing.
After reading this post: http://kevincuzner.com/2013/05/27/raspberry-pi-as-...
I noticed that the 'linuxspi' programmer by default uses the GPIO pin 25 for reset, so your reset wrapper script is not needed. Else, great work ;)
breagan22 8 years ago
Outstanding!!
PavelH2 8 years ago
Thank you.
grimw 8 years ago
This is a fantastic tutorial, and it really helped me get started. I'd like to point out a couple things I learned along the way, however. I don't know about at the time you were doing this project, but the "linuxgpio" configuration for avrdude exists in /etc/avrdude.conf on the RPi (after you install avrdude, of course); you simply uncomment the lines and set the pins to what they should be. Second, because the AVR and the RPi both run at 3.3v, I didn't find any need to use resistors for anything other than the LED itself, and that was only to protect the LED. And finally, if you hook the LED's positive pole to one of the SPI pins, the LED will happily blink for you as the RPi uploads data, which is cool and gives some nice feedback!
grimw 8 years ago
Oh, and I used this with an ATmega88; I'm sure you can use it with any ATmega that supports 3.3v operation, which, for all I know, is all of them.
iugamarian 8 years ago
Works with atmega8. When I poweroff the Raspberry Pi the Reset pin GPIO 22 gets to zero, stopping the program on the microcontroller. I added a pullup resistor of 10 k to 3.3 v to GPIO22 and now it works after Pi poweroff too :) . Thank you. I made it but I don't yet have a camera / photo capable phone so no images...
haneja 8 years ago
jollyviking 8 years ago
Thank you! Thank you! Thank you! My L.E.D.s are flashing, and my stepper motor is spinning. I could not have gotten this far this fast without your VERY helpful Instructable!
AlejandroL2 9 years ago
i dont know nothing about makefile, could you help me, which is the path where the file should be, i have to paste or create a new file ?
alvaro.vaz.18 9 years ago
hi im having this error"28:*** target pattern contains no '%' .Stop
Can anyone help me with this....
bacoms 9 years ago
On the strength of getting blinky to work following this instructable (thanks for that), I thought I'd use what I'd learnt to install Atmel's TWI Bootloader on a ATTiny85 (Atmels Application Note: AVR112).
Unfortunately I fell at the first hurdle running the 'make' with error
'fatal error: ioavr.h: No such file or directory'
Has anyone done this successfully? If so, some pointers on getting passed this would be much appreciated.
bacoms 9 years ago
Problem solved by replacing lines:
#include <ioavr.h>
#include <inavr.h>
with:
#include <avr/io.h>
Running the 'make' again gives error message:
Common_Define.h:164:1: error: unknown type name ‘__no_init’
Line 164 in Common_Define.h is:
__no_init uint8_t pageBuffer[PAGE_SIZE];
Can anyone throw some light on why code from Atmel doesn't seem to work?
david.manciet 9 years ago
I try to upload this code instead of blinky but i got a lot of error and i don't know why if someone can help me
https://www.instructables.com/files/orig/F95/O60L/I... from https://www.instructables.com/id/MASON-JAR-MOOD-LAM...
PS : It works perfectly when i upload blinky
prb3333 9 years ago
Hi,
That code uses the Arduino code libraries ("digitalWrite" etc.). You can program these devices that way but I do not cover it here.
I am using the avr-libc library. By the way this gives you the possibility to have three pwm outputs from the ATtiny85. I have mood light code I have used in the past, it's here:
https://gist.github.com/paulbarber/fd0467d912f3a2d...
I hope you can work out from the comments what the circuit should be like.
Cheers,
Paul.
david.manciet 9 years ago
I gonna try to directly upload the hex generate by arduino and if it doesn't work i gonna use your code to make mine.
Thanks again
vpopov1 9 years 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