Pi2uno

Introduction: Pi2uno

Raspberry Pi to Arduino Mega communication using Inter-Integrated circuit or I2C Protocol and blinking an inbuild LED using Raspberry Pi as Master and Arduino as Slave

Supplies:

  1. Raspberry Pi (any version) -1
  2. Arduino Mega or UNO (here Arduino Mega) -3
  3. Bread Board -1
  4. Female to Male Jumpers Cable - 3
  5. Male to Female Jumpers Cable - 9

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Step 1: Wiring

We start this project by wiring the connection,

  1. Take a breadboard,
  2. Set the connection as follows,

Raspberry Pi SDA (Sky blue color in the picture) –> Shared SDA on breadboard

Raspberry Pi SCL (Sky blue color in the picture)–> Shared SCL on breadboard

Raspberry Pi GND (Black color in the picture)–> Shared GND on breadboard

All Arduino SDA(Gray color in the picture) –> Shared SDA on breadboard

All Arduino SCL (Gray color in the picture)–> Shared SCL on breadboard

All Arduino GND(Black color in the picture) –> Shared GND on breadboard

Step 2: Step 2: Under Standing the Connection

Here the SCL means Serial Clock sends the clock signal for the Slave devices from the Master devices.

SDA means Serial Data sends the Data or Information either from the Master to the Slave or also from the slave to the master.

GND means ground this is the common ground signal for all the microcontroller that is present in this Protocol.

Step 3: Step 3: Arduino Code

This has the Code that needs to be uploaded to the Arduino

If you use more that one arduino Don't forget to comment the slave address because the slave address should be unique.

//Arduino code to receive I2C communication from Raspberry Pi
#include <Wire.h> // Define the slave address of this device. #define SLAVE_ADDRESS 0x04 //Arduino 1 // #define SLAVE_ADDRESS 0x05 //Arduino 2 // #define SLAVE_ADDRESS 0x06 //Arduino 3 // string to store what the RPi sends String str_recieved_from_RPi = ""; void setup() { // setup the LED pinMode(LED_BUILTIN, OUTPUT); // begin running as an I2C slave on the specified address Wire.begin(SLAVE_ADDRESS); // create event for receiving data Wire.onReceive(receiveData); } void loop() { // nothing needed } void receiveData(int byteCount) { while ( Wire.available()) { str_recieved_from_RPi += (char)Wire.read(); } // turn on or off the LED if (str_recieved_from_RPi == "on") { digitalWrite(LED_BUILTIN, HIGH); } if (str_recieved_from_RPi == "off") { digitalWrite(LED_BUILTIN, LOW); } str_recieved_from_RPi = ""; }

Step 4: Step 4: Raspberry Pi Code

Make your you have latest Debian version installed in your Pi,

Else download it from here.

# Raspberry Pi to Arduino I2C Communication 
# Python Code import smbus # Slave Addresses for Arduinos ARDUINO_1_ADDRESS = 0x04 # I2C Address of Arduino 1 ARDUINO_2_ADDRESS = 0x05 # I2C Address of Arduino 2 ARDUINO_3_ADDRESS = 0x06 # I2C Address of Arduino 3 # Create the I2C bus I2Cbus = smbus.SMBus(1) aSelect = input("Which Arduino (1-3): ") bSelect = input("On or Off (on/off): ") if aSelect == 1: SlaveAddress = ARDUINO_1_ADDRESS elif aSelect == 2: SlaveAddress = ARDUINO_2_ADDRESS elif aSelect == 3: SlaveAddress = ARDUINO_3_ADDRESS else: # quit if you messed up quit() # also quit if you messed up if bSelect != "on" or bSelect != "off": quit() BytesToSend = ConvertStringsToBytes(bSelect) I2Cbus.write_i2c_block_data(SlaveAddress, 0x00, BytesToSend) print("Sent " + SlaveAddress + " the " + bSelect + " command.") # This function converts a string to an array of bytes. def ConvertStringToBytes(src): converted = [] for b in src: converted.append(ord(b))

return converted

Step 5: Step 5: Play

Power Up the Devices

Enter the Arduino address in the Raspberry Pi and watch that Arduino's LED glows.

If you any trouble feel free to comment below.

Raspberry Pi Contest 2020

Participated in the
Raspberry Pi Contest 2020

5 People Made This Project!

Recommendations

  • LED Strip Speed Challenge

    LED Strip Speed Challenge
  • Sculpting Challenge

    Sculpting Challenge
  • Clocks Contest

    Clocks Contest

15 Discussions

0
balaji.v.2015.ece
balaji.v.2015.ece

Question 8 weeks ago on Step 4

What OS are u using. Where did u mention that??

0
balaji.v.2015.ece
balaji.v.2015.ece

Answer 8 weeks ago

Debian OS.
It is mentioned in Step 4

0
xrl04006
xrl04006

8 weeks ago

Where is my code send soon

0
balaji49
balaji49

Reply 8 weeks ago

You can find the code in Step 3 and Step 4
Thanks

1
Awesome_Stuff_Is_Awesome
Awesome_Stuff_Is_Awesome

Reply 8 weeks ago

All the code needed is in step 3 and 4. Just copy and paste

0
xrl04006
xrl04006

Tip 8 weeks ago

Make UNO and MEGA communication

0
balaji49
balaji49

Reply 8 weeks ago

Sure! Nice idea Pal

0
zxyq123
zxyq123

Question 8 weeks ago on Step 1

Can u answer my message please

0
balaji49
balaji49

Reply 8 weeks ago

Ya, I have replyed you in message.
Thanks

1
Penolopy Bulnick
Penolopy Bulnick

8 weeks ago

Nice job on your first Instructable :)

0
balaji49
balaji49

Reply 8 weeks ago

Thanks a lot

0
balaji49
balaji49

Reply 8 weeks ago

Thanks a lot Bulnick!
I also participated in Raspberry Pi Contest, Please Vote mine project

0
xrl04006
xrl04006

Reply 8 weeks ago

send me the code