Introduction: Tutorial: Communication Between a RaspberryPi and an Attiny85

About: Jack passed away May 20, 2018 after a long battle with cancer. His Instructables site will be kept active and questions will be answered by our son-in-law, Terry Pilling. Most of Jack's instructables are tuto…

This tutorial will show you how to set up a RaspberryPi and an Attiny85 to pass digital signals in both directions.

The Attiny85 is powered by the 3.3 volt line line from the RaspberryPi so no power supply is neccessary.

The RaspberryPi program in this tutorial uses the wiringPi libraries, written by Gordon Henderson, for programming the GPIO in C.

wiringPi must be installed. Instructions for download, installation and use are located at http://wiringpi.com

wiringPi uses it's own pin numbering scheme. All RaspberryPi pin numbers are wiringPi numbers unless otherwise specified.

If you need a programmer for the Attiny85 I recommend this one: https://www.sparkfun.com/products/11801

Follow this link for instructions on how to add the Attiny85 definitions to the Arduino IDE:
https://learn.sparkfun.com/tutorials/tiny-avr-programmer-hookup-guide/

If you want to use a different programmer, or build your own, that is okay too. There many alternatives, just search instructables for "attiny".

You will need a way to run wires from the RaspberryPi to the breadboard. You can use male/female jumper wires but one of the Pi Cobblers listed on this page from Adafruit will make it a lot easier: http://www.adafruit.com/search?q=cobbler

Step 1: Building the Circuit

To build the circuit you will need:

jumper wires

4 10K resistors (Pulldowns)

2 330-560 Ohm resistors (For LEDs)

2 small push button switches

2 LEDs

Attiny85 Microcontroller, available here: https://www.sparkfun.com/products/9378

Here are the Attiny85 Pinouts:

            Reset    1 *      8  VCC 5 Volts
   Analog In 3, PB3  2        7  PB2, Analog In 1, I2C SCL, SPI SCK
   Analog In 2, PB4  3        6  PB1, PWM, SPI MISO
             Ground  4        5  PB0, PWM, I2C SDA, SPI MOSI

Follow the diagram above to build the circuit.

Step 2: The Code

//This is the Code for the RaspberryPi:

/****************************************************************
 * Filename: Attiny_RPi.c (Program for RaspberryPi)
 *
 * Send a GPIO signal in both directions from a RaspberryPi 
 * to/from an Attiny85 microcontroller.
 *
 * When the button switch attached to the Attiny85 is pressed
 * it lights the LED attached to the RaspberryPi, and vice-versa.
 *
 ****************************************************************/

#include <wiringPi.h>

void setup()
{
  wiringPiSetup();
  pinMode(2, INPUT);   // Switch with pull-down resistor.
  pinMode(1, INPUT);   // Receive signal from Attiny, pull-down resistor.

  pinMode(14, OUTPUT); // Send signal to Attiny when button pressed.
  pinMode(11, OUTPUT); // Lights LED when button pressed on Attiny.
}

void loop()
{
  digitalWrite(14, digitalRead(2)); // Send signal to Attiny if button pressed.
  digitalWrite(11, digitalRead(1)); // Light LED if signal received from Attiny.
}

int main()
{
  setup();
  for(;;)
  {
    loop();
  }
  return(0);
}
//And this is the code for the Attiny85:

/****************************************************************
 * Filename: Attiny_RPi.ino (Program for Attiny85)
 *
 * Send a GPIO signal in both directions from a RaspberryPi 
 * to/from an Attiny85 microcontroller.
 *
 * When the button switch attached to the Attiny85 is pressed
 * it lights the LED attached to the RaspberryPi, and vice-versa.
 *
 * The Attiny is powered by 3.3 volts from the RaspberryP.
 *
 ****************************************************************/

void setup()
{
  pinMode(0, INPUT);  // Switch with pull-down reristor.
  pinMode(4, INPUT);  // Receive signal from RPi, pull-down resistor.

  pinMode(1, OUTPUT); // Send signal to RPi when button pressed.
  pinMode(3, OUTPUT); // Lights LED when button pressed on RPi.
}

void loop()
{
  digitalWrite(1, digitalRead(0)); // Send signal to RPi if button pressed.
  digitalWrite(3, digitalRead(4)); // Light LED if signal received from RPi.
}

Step 3: Sending Pulse Width Modulation (PWM) Signals.

You can also send PWM signals. Use the same program on the RaspberryPi and put this program on the Attiny85.

/****************************************************************
* Filename: AttinyPWM_RPi.ino (Program for Attiny85) * * The Attiny sends a PWM signal to the RaspberryPi * to vary the brightness of an LED. * * Pressing the button on the RPi lights the LED on the Attiny. * * The Attiny is powered by 3.3 volts from the RaspberryP. * ****************************************************************/ void setup() { pinMode(1, OUTPUT); // Send PWM signal to RPi, has pull-down resistor. pinMode(3, OUTPUT); // Lights LED when button pressed on RPi. pinMode(4, INPUT); // Receive signal from RPi, pull-down resistor. } void loop() { int x=1; for(int i=0;i>-1;i+=x) { analogWrite(1, i); // Send signal to RPi. if(i==128) x=-1; digitalWrite(3, digitalRead(4)); // Light LED if button pressed on RPi. delay(25); } }