element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci-Pi Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Sci-Pi Design Challenge
  • More
  • Cancel
Sci-Pi Design Challenge
Blog Blog #5 I2C Attiny 85 Communication
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Sci-Pi Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: GustavoMorales
  • Date Created: 26 Jun 2023 7:44 PM Date Created
  • Views 1233 views
  • Likes 6 likes
  • Comments 1 comment
  • python
  • raspberry pi
Related
Recommended

Blog #5 I2C Attiny 85 Communication

GustavoMorales
GustavoMorales
26 Jun 2023

In this blog, I would like to discuss the limited resources available when working with a basic microcontroller like the ATtiny85. I was surprised to find that while communication is straightforward with other boards, it is not as simple when it comes to a Raspberry Pi, especially the Pi 4.

The process of establishing communication was incredibly frustrating. It took several weeks of trying numerous libraries, communication methods, and protocols. Ultimately, I settled on using the I2C protocol due to its straightforward wiring. However, I am fully aware that I chose this protocol because, as a future personal project, I plan to integrate a small OLED display for the GUI, and I would need to communicate using this protocol. It was a headache because losing communication between the Raspberry Pi and the ATtiny was a complex issue to resolve. However, after reading about transmission speeds and synchronization processes, I gained a better understanding. Looking back, I can say that I achieved most of the goals I set, except for creating the PCB, which was the most exciting part for me. I must express my gratitude for the criticisms received in the first blog because, without them, I may have damaged the Raspberry Pi during the initial testing phase.

Now let's talk about the electronic part. The pin connections were straightforward, following the ATtiny85 pinout. However, the ATtiny85 operates at 5V, powered by the circuit regulator, while the Raspberry Pi requires a voltage of 3.3V. This presented a dilemma: whether to use a voltage divider or a level shifter. Using a voltage divider was not an option because an incorrect voltage level could be interpreted as a communication failure, leading to misinterpretation by the ATtiny85 and potentially causing serious issues. Therefore, a level shifter was the solution.

One side handles the "high" voltages, while the other side handles the "low" voltages, ensuring safety and eliminating any potential danger. Once everything was wired up, I embarked on the journey of establishing communication and understanding their interactions. I tried various I2C library codes, including TinyWire.h, TinyWire.S, and WireAT.h, but I couldn't achieve successful data transmission. The Raspberry Pi recognized the ATtiny as a component, but I struggled to find a way to make the ATtiny act as a slave in communication. It only seemed capable of sending data, which was promising for future steps involving OLED displays, but not for the immediate project. However, I knew I was on the right track when I at least managed to establish proper addressing.

image

Here are some codes that work for the Attiny and maybe to communicate to other devices but no with the rasp

Maybe this blog should be named HOW NOT TO ESTABLISH AN I2C COMMUNICATION

#include <TinyWireM.h>

#define SLAVE_ADDRESS 0x10

void setup() {
  TinyWireM.begin();
}

void loop() {
  // Envía un byte de datos desde el ATtiny85
  TinyWireM.beginTransmission(SLAVE_ADDRESS);
  TinyWireM.write(0x42); // Envía el valor 0x42
  TinyWireM.endTransmission();

  delay(1000);
}

I tied sending characters, numbers strings with no success. When I was almost giving up I found a library that comes with a "Core" board which was created in 2006 I thought it might not work and is old enough to keep on failing with my project. But what a surprise:

image

I was sending the string Ready as you can see in the terminal I got it then I started testing with a basic GUI to keep tracking the behavior of the i2c communication.

import smbus

# Dirección del esclavo I2C (ATtiny85)
SLAVE_ADDRESS = 8

# Inicializar el bus I2C
bus = smbus.SMBus(1)

# Leer los datos enviados por el ATtiny85
def read_data():
    try:
        # Leer hasta 6 bytes de datos desde el esclavo
        data = bus.read_i2c_block_data(SLAVE_ADDRESS, 0, 6)
        # Convertir los bytes en una cadena
        message = ''.join(chr(byte) for byte in data)
        return message
    except IOError:
        print("Error de comunicación I2C")
        return None

# Leer y mostrar los datos enviados por el ATtiny85
received_data = read_data()
if received_data is not None:
    print("Datos recibidos:", received_data)

And later with a GUI because the connection was broken after a few minutes and I had to see it to see what was happening.

image

import smbus
import tkinter as tk

SLAVE_ADDRESS = 8
I2C_BUS = 1
BAUD_RATE = 9600

bus = smbus.SMBus(I2C_BUS)

def read_data():
    try:
        data = bus.read_i2c_block_data(SLAVE_ADDRESS, 0, 7)
        message = ''.join(chr(byte) for byte in data)
        return message
    except IOError:
        print("Error de comunicación I2C")
        return None

def update_message_and_duration():
    received_data = read_data()
    if received_data is not None:
        message_label.config(text="Received message: " + received_data)

    root.after(1000, update_message_and_duration)

root = tk.Tk()
root.title("I2C Communication GUI")

message_label = tk.Label(root, text="Received message: ")
message_label.pack(pady=10)

update_message_and_duration()

close_button = tk.Button(root, text="Close", command=root.quit)
close_button.pack(pady=10)

root.mainloop()

Later I understand that speed was not appropriate and that the connection was falling because of a mismatch in synchronization. And at this point, everything is almost done. I'm very happy because the established objectives were done. In the next Blog, I'll share the GUI it will be a little one but I had to share this with you how not to establish an i2c communication. 

  • Sign in to reply
Parents
  • misaz
    misaz over 2 years ago

    Congrats. For saving time buy logic analyzer.There are some expensive one, but since I2C is very slow cheepest chinese are also suitable. The most cost efective way is Aliexpress (around 9 USD nowadays): https://www.aliexpress.com/item/1005003375736481.html. I bought it for 13 USD several years ago and it saved be thousands of hours of debugging (not only low level codes).

    image

    It will show you what is happending on the I2C bus (and other signals). Mentioned samples at 24 MHz and is suitable for analysing buses up to 12 MHz which 0.4MHz I2C fully satisfy.

    If you cant find working library there is no better way than write your own. While you was trying several third-party software which did not work right for you, it was possibly more cost efective learn how do the I2C and ATtiny86 work and write your own software from scratch. ATtiny86 is old microcontroller and have no I2C peripheral. There is USI which can be used for implementing partialy hardware accelerated I2C slave. Library you find most probably control it in tricky way or implement I2C slave using bitband method (directly reading GPIOs and driving them). The second mentioned is simple to implement yourself.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • misaz
    misaz over 2 years ago

    Congrats. For saving time buy logic analyzer.There are some expensive one, but since I2C is very slow cheepest chinese are also suitable. The most cost efective way is Aliexpress (around 9 USD nowadays): https://www.aliexpress.com/item/1005003375736481.html. I bought it for 13 USD several years ago and it saved be thousands of hours of debugging (not only low level codes).

    image

    It will show you what is happending on the I2C bus (and other signals). Mentioned samples at 24 MHz and is suitable for analysing buses up to 12 MHz which 0.4MHz I2C fully satisfy.

    If you cant find working library there is no better way than write your own. While you was trying several third-party software which did not work right for you, it was possibly more cost efective learn how do the I2C and ATtiny86 work and write your own software from scratch. ATtiny86 is old microcontroller and have no I2C peripheral. There is USI which can be used for implementing partialy hardware accelerated I2C slave. Library you find most probably control it in tricky way or implement I2C slave using bitband method (directly reading GPIOs and driving them). The second mentioned is simple to implement yourself.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube