Top
Home


I2C Communication Between Raspberry Pi & Attiny88 Microcontroller

Introduction:

This is the experimental work to check the communication protocol of I2C (Inter-IC Connection) between Raspberry Pi and the Attiny88 microcontroller. Raspberry Pi is a single-board computer with 40 GPIO (General Purpose Input Output) pin. And Attiny88 is an 8-bit AVR microcontroller with 28 GPIO pins. Both the Raspberry Pi and the Attiny88 support serial communication like I2C or SPI. But here we are trying to figure out the working protocol of the I2C Serial Communication. 

 

Theory Behind the I2C:

I2C is a very famous communication protocol. You can find the important theory from https://i2c.info/, https://www.i2c-bus.org/ websites. Here I point out some important points about I2C.

  • I2C or Inter-IC Connection is a Serial communication protocol that follows the Master-Slave communication concept.
  • I2C supports multiple numbers of master and the multiple numbers of slaves in a single communication system.
  • This system basically working on the two wires called SDA (Serial Data) and SCL (Serial Clock). 
  • This SDA is used to send and receive the data and the command bits.
  • SCL wire is used for the clocking for the communication.
  • For proper communication, both SDA and SCL wires need to pull up. For pulling up generally we use 4.7Kohms resister for 5 volts as VCC.
  • I2C communication can take place in four different speed.
    • Normal Mode: 100KHz
    • Fast Mode: 400KHz
    • High-Speed Mode: 3.4MHz
    • Ultra Fast Mode: 5MHz
  • In this communication, every slave has a unique address. With this address, the master can communicate with the slaves.
  • I2C supports two modes of address. 1st one is 7-bit addressing mode and the 2nd one is the 10-bit addressing mode.
  • In 7-bit addressing mode, maximum slaves possible is 2^7=128. But maximum 112 slaves are allowed and the rest of the addresses are reserves. For 10-bit addressing mode maximum, 1024 is possible but only 1008 is allowed. 
  • For data send and receive this protocol uses only the SDA line. So we can say that it is a half-duplex system.

 

Hardware Setup:

As hardware, we need a Raspberry Pi. Here we are using the Raspberry Pi 3 model with the pre-install rasbian operating system. The second important hardware is the Attiny88 microcontroller IC. Here we are using the DIP package. So we can use the breadboard to make the circuit. To check the output we are using 8 LEDs. Some additional things like a breadboard, Jumper wires, the power supply is needed for the experiment.

Before starting the experiment we need to go through the datasheet of the Attiny88 microcontroller. For this experiment, we need the hardware pin number for the I2C bus and the output port pin number for the LEDs.

This is the pin diagram for the Attiny88 microcontroller. Here hardware pin 28 and 27 are used as SCL and SDA pin. And for output we are using port B. So hardware pin number 14, 15, 16, 17, 18, 19, 9, 10 are used as output. And pin number 7 and pin number 8 is for the power supply. 5 volts DC is perfect for this IC. Pin number 16, 17, 18, 19 are used to program the IC. Here we are going to program the microcontroller by one Arduino. The procedure to the program by the Arduino is described in my other experimental work https://www.arghyabiswas.com/attiny2313-microcontroller-programming-with-arduino. Although in that experiment we used Attiny2313 IC, the basic concept to program those ICs are totally the same.

 

Another important component is the Raspberry Pi. For Raspberry Pi, we also need a little bit of knowledge about the pin configuration and the voltage level. Raspberry Pi also has two GPIO cum I2C  hardware pin.

If you did any project on Raspberry Pi GPIO pin then you definitely know that Raspberry Pi supports two types of pin declaration. One is Board mode and another one is BCM mode. In every mode the numbering of the pin is different. But in this experiment, we are going to use some library for communication. So no need to think about the pin number in programming. But for hardware connection, we need to know the pins which are responsible for this communication. Here the physical pin 03 and 04 (GPIO02, GPIO03) is used for the I2C communication. 

 

The connection for this protocol is very easy. Both the SCL pins will be connected together and both SDA pins will be connected together. And finally, we have to make a common ground connection. This common ground is a very important step, don't ignore it. 

This is the total circuit for this experiment. Here DIY breadboard power supply is used to power the Attiny88 IC. Here perfect 5 volt DC is used that is produced by the L7805 voltage regulator IC.

 

Software Setup:

Like software, we are going to use the Arduino coding for the Attiny88 microcontroller. And the Python language for the Raspberry Pi. In Arduino IDE we write the code for the I2C communication. Here Attiny88 works as slave, so it has an address. In code, we define the address of the attiny88 as 8. From the Raspberry Pi, this address will be used to communicate. In Arduino, we need a library called wire.h and we include this library in the code. You can download the Arduino code from Here.

The code looks like this. This will call a receiveEvent function when the system senses some data in the I2C bus. The PORTB is used to connect the LEDs. So it is declared as output. After successfully receiving the data of 1 byte it will write that data to the PORTB. So it will be shown on the LEDs. The code is then saved and complied. Then the IC is connected to an Arduino as described in https://www.arghyabiswas.com/attiny2313-microcontroller-programming-with-arduino. And then upload the code to the microcontroller via that Arduino. Finally, it placed in the breadboard where we already made the circuit for this experiment.

 

In the Raspberry Pi first, we need to check that the I2C bus is working or not. For this, we need to open the terminal and write i2cdetect -y 1 and press enter. It will show a chart of the all available I2C slaves connected with the Pi. In our case, only 1 slave is connected. So it should show the address of the slave that we provide in the Attiny88 is 8.

It looks like this. But if you not found any device then double check the connection and then re-upload the code in the slave microcontroller. Now we have ta make a python file to write the code to send the data over this I2C bus. To use the I2C bus using python we need a library called SMBus. This library is also come by default with Raspberian OS. So no need to install the library. Only import that and enjoy the code. Here is the python code.

 

Here we import the SMBus and select the channel 1 (Raspberry Pi support 2 channel I2C communication), and the address of the slave as 8. Then we take a random 8-bit data that will be sent to the Attiny88 over this I2C bus. Here we simply use the library function  write_byte and as an argument, we pass the address of the slave and the data.

 

Run the Software:

The Attiny88 starts to execute the uploaded code just after getting power. But in Raspberry Pi, we have to run the software manually. You can run the code via terminal or direct from the IDE which we use to edit the code. Here we directly run the code from the IDE by pressing the F5 button in keyboard. 

  

This is the screenshot of the executed code. The code print some lines according to the data sent over the bus. In the moment of the run, the code the LEDs are turned on as the data pattern is defined in the Raspberry Pi. It means the data is successfully sent to the Attiny88 over this I2C bus.

 

 Conclusion:

This I2C communication between Raspberry Pi and Attiny88 is an experimental work. This experiment helps us to build the concept of Serial communication between two devices. This works can be useful for an important part of a big project where need so many times communicate between these kinds of devices.

 

 

If this small effort can help in your any project then my effort will be successful. Please put feedback On our contact page.

If you faced any problem please send an email to admin@arghyabiswas.com. Thanks for being with us.




Thank You

Leave a Comment







View Previous Comment

Adlof 20th Aug, 2019

Nice and smart project work. Keep it up.

Show 1 Reply

Ashraf maniyar 06th May, 2019

Good job bro

Show 1 Reply