DMShropshire
Posts: 15
Joined: Thu Oct 11, 2012 3:29 am

i2c issue - attiny85

Sun Mar 01, 2015 4:58 am

Evening!
I am at the beginning stages of learning I2C communication. So the sample code im working with works great from Rpi to Arduino. But I wanted to work with the attiny85 chips instead of the Arduino.

Im off to a decent start in getting the 85s to work but im running into an issue. Im trying to determine if the code is causing this to happen or if I am missing something in the hardware. So I have all my lines hooked up properly with 4.7k pull ups from the SDA and SCL pins to 5v on the 85.
The code is supposed to send a digit to the tiny85, add 10 to the input and output back to the Rpi the sum. So Input = 1 output = 11. As you can see below when I get to a number higher than 2, the code fails and outputs 0 and will continue to do so until I reset the tiny85. When I perform the i2cdetect command after the code failed, all the possible addresses show up as detected.

Is this the code causing this malfunction or perhaps something in the wiring? Why are all the addresses showing up only after the code malfunctions?
Any input greatly appreciated!

Code: Select all

pi@raspberrypi ~/Desktop $ i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- 28 -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi ~/Desktop $ python raspi2cmaster.py
Enter 1 - 9: 1
Sending input to Attiny85  1
Attiny85 received a digit  11

Enter 1 - 9: 2
Sending input to Attiny85  2
Attiny85 received a digit  12

Enter 1 - 9: 3
Sending input to Attiny85  3
Attiny85 received a digit  0

Enter 1 - 9: ^Z
[6]+  Stopped                 python raspi2cmaster.py


pi@raspberrypi ~/Desktop $ i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70: 70 71 72 73 74 75 76 77


hampi
Posts: 223
Joined: Fri May 31, 2013 11:29 am
Contact: Website

Re: i2c issue - attiny85

Sun Mar 01, 2015 8:14 am

Never used attiny85 but that looks like a bug in the i2c implementation of attiny85. You could test that with a more robust chip first to see that your Python program is working correctly. You might also need a logic analyser to see what is happening on the i2c bus. BTW you are using an old Raspberry Pi since for the new ones you need to use command

Code: Select all

i2cdetect -y 1

DMShropshire
Posts: 15
Joined: Thu Oct 11, 2012 3:29 am

Re: i2c issue - attiny85

Sun Mar 01, 2015 4:32 pm

Thanks for the input Hampi!
I have tested the master code for the pi to communicate with the Arduino Uno. This sample code was actually written for the Arduino, which indeed works flawlessly. Then again the Arduino was built to work with I2c right out of the gate (internal pull ups and such). So still trying to weed out the hardware issue.
Below is the code if anyone is interested. I just like the Attinys because they are so cheap, and i have quite a few laying around. Im going to look around with the scope to see what I can find. But ima newbie when it comes to that thing. Any ideas greatly appreciated.
Thanks!

Master Python code

Code: Select all

import smbus
import time

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(0)

# This is the address we setup in the Arduino Program
address = 0x28

def writeNumber(value):
    bus.write_byte(address, value)
    # bus.write_byte_data(address, 0, value)
    return -1

def readNumber():
    number = bus.read_byte(address)
    # number = bus.read_byte_data(address, 1)
    return number

while True:
    var = input("Enter 1 - 9: ")
    if not var:
        continue

    writeNumber(var)
    print "Sending input to Attiny85 ", var
    # sleep one second
    time.sleep(1)

    number = readNumber()
    print "Attiny85 received a digit ", number
    print
Attiny85 slave code

Code: Select all

/* ATtiny85 as an I2C Slave            BroHogan                           1/12/11
 * Example of ATtiny I2C slave receiving and sending data to an Arduino master.
 * Gets data from master, adds 10 to it and sends it back.
 * SETUP:
 * ATtiny Pin 1 = (RESET) N/U                      ATtiny Pin 2 = (D3) N/U
 * ATtiny Pin 3 = (D4) to LED1                     ATtiny Pin 4 = GND
 * ATtiny Pin 5 = I2C SDA on DS1621  & GPIO        ATtiny Pin 6 = (D1) to LED2
 * ATtiny Pin 7 = I2C SCK on DS1621  & GPIO        ATtiny Pin 8 = VCC (2.7-5.5V)
 * NOTE! - It's very important to use pullups on the SDA & SCL lines!
 * Current Rx & Tx buffers set at 32 bytes - see usiTwiSlave.h
 * Credit and thanks to Don Blake for his usiTwiSlave code. 
 * More on TinyWireS usage - see TinyWireS.h
 */


#include "TinyWireS.h"                  // wrapper class for I2C slave routines

#define I2C_SLAVE_ADDR  0x28            // i2c slave address (38)
#define LED1_PIN         4              // ATtiny Pin 3
#define LED2_PIN         1              // ATtiny Pin 6

void setup(){
  pinMode(LED1_PIN,OUTPUT);             // for general DEBUG use
  pinMode(LED2_PIN,OUTPUT);             // for verification
  Blink(LED1_PIN,2);                    // show it's alive
  TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
}


void loop(){
  byte byteRcvd = 0;
  if (TinyWireS.available()){           // got I2C input!
    byteRcvd = TinyWireS.receive();     // get the byte from master
    Blink(LED1_PIN,byteRcvd);           // master must wait for this to finish before calling Wire.requestFrom
    byteRcvd += 10;                     // add 10 to what's received
    TinyWireS.send(byteRcvd);           // send it back to master

    Blink(LED2_PIN,1);                  // show we transmitted
  }
}


void Blink(byte led, byte times){ // poor man's display
  for (byte i=0; i< times; i++){
    digitalWrite(led,HIGH);
    delay (250);
    digitalWrite(led,LOW);
    delay (175);
  }
}


dgordon42
Posts: 772
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: i2c issue - attiny85

Mon Mar 02, 2015 10:51 am

The Pi comes with 1k8 pullup resistors on the i2c bus 1 pins to the 3v3 power line. You should not have added pullup resistors to the 5v power line. 5v can damage the GPIO pins on the Pi, but perhaps having resistors in there saved your Pi.
Try again without your pullup resistors. You can also try reducing the power to the attiny85, try running it at 3v3.

Hope this helps,
Dave.

astromik
Posts: 2
Joined: Thu Aug 18, 2016 10:19 am

Re: i2c issue - attiny85

Thu Aug 18, 2016 10:35 am

Hello.
I tried example from this thread, but I have some problem.
I recieved "delayed" numbers.
See a picture below.
Is there any buffer or what?

How to correct it?

Image
http://www.astromik.org/forum/attiny85-i2c.gif

astromik
Posts: 2
Joined: Thu Aug 18, 2016 10:19 am

Re: i2c issue - attiny85

Thu Aug 18, 2016 7:22 pm


Return to “General discussion”