I am creating this project to provide further instruction to people like me, who are scouring the internet looking for help that they cannot seem to find.
In this project, I am using a Raspberry Pi to control multiple Arduinos using I2C connections.
NOTE: To use the Raspberry Pi in this tutorial, you also need a monitor, keyboard, and mouse.
NOTE: I am still in the learning process for I2C, Arduino, and the Raspberry Pi. Everything provided in this tutorial has worked for me, but if anyone with a greater knowledge on any part of this tutorial wants to comment changes that need to be made to code, schematics, etc., please do not hesitate to comment below.How It Started
I am on the engineering team in my fraternity for homecoming. One of our main jobs is to create and operate all of the moving parts for our homecoming deck. In the past, their solution to this was to use separately programmed Arduinos that were all turned on individually to control motors. Timing is key to this method, as they have to start the devices all at the same time so that the moving parts on the deck run simultaneously. They were also programming the arduinos with seperate code with different time intervals for each device. When I realized this is what they have been doing in the past, it made me wonder if there was a better solution to this problem. I already owned my own Raspberry Pi, so I started doing some research on communication between the Pi and other micro controllers. I read about some projects using the I2C (Inter-integrated Circuit) protocol for both devices, but a lot of the information I was reading was not very "beginner friendly" and/or it lacked indepth information. I was able to figure it out on my own after days of working on the project, but I wanted to make it easier for amateurs, like me, in the future.
Step One: Setting up the Raspberry Pi environmentFirst, we have to setup our Raspberry Pi with Rasbian and a Python IDE. There are plenty of tutorials and videos on how to do that online, so I won't explain that here.
After you have setup Rasbian, we need to enable the SSH client on the Raspberry Pi. This will allow you to access your Raspberry Pi remotely if you are connected to the same WiFi as the Raspberry Pi.
1. Open up the terminal window and type:
sudo raspi-config
2. Go into the "Interfacing Controls" option
3. Go into the "SSH" tab, and click enable SSH
Now that your Raspberry Pi is setup for a remote connection, you can download applications like "PuTTy" on your laptop to access the device. To do this you need to find the IP address of your Raspberry Pi.
1. Go back into the terminal window on the Raspberry Pi and type:
sudo ifconfig
The IP address you need is labeled as "inet addr:123.45.678.901".
When you setup PuTTy on your laptop this is the IP address you will use to access the Raspberry Pi. You will then be prompted with a sign in screen.
The default username and password for every Raspberry Pi:
Username: pi
Password: raspberry
You will use this environment later when you have completed the rest of the tutorial.
Step Two: Setting up the I2C protocolThis step is to setup the Raspberry Pi so it can communicate with the Arduinos via an I2C bus.
1. Open a new terminal window and type in the following code to install SMBus and the Python dev:
sudo apt-get install python-smbus python3-smbus python-dev python3-dev
2. Now run the command to install I2C tools:
sudo apt-get install i2c-tools
3. Now type in the following code to edit the raspi-blacklist to enable I2C and SPI protocol:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
In the file you should see two lines:
#blacklist spi-bcm2708
#blacklist i2c-bcm2708
Delete the "#" in front of both of these lines, hit "control x" to save and exit the file.
4. For recent versions of the Raspberry Pi, you will need to edit the configuration file to enable I2C and SPI protocol
Type in the following code:
sudo nano /boot/config.txt
In the configuration file, add these two lines to the bottom of the file:
dtparam=i2c1=on
dtparam=i2c_arm=on
Save using "control x" and exit the file.
Now your Raspberry Pi is ready to communicate with other devices using I2C.
Step Three: Hardware connections and schematicsHere are pictures of the schematics for the Raspberry Pi, Arduinos, relays, and developmental boards.
NOTE: You will (preferably for more solid and stable connections) need to solder your connections on the board that connects the relays and the Arduino together.
NOTE: For the I2C breadboard connections, you will only need to use the jumper wires for the connections from the Raspberry Pi to the breadboard.
The Arduino boards pictured above are Arduino Mega's. The ones I used were Arduino Uno's and their pins were slightly different. The correct I2C pins on the Arduino Uno are pins A4 and A5 pictured below.
For connecting the relays to each Arduino I used solderable boards to ensure that I would not have any loose wires.
The forward and backward relays for your motor would be connected to pin 10 and pin 11 respectively.
Step Four: The CodeThe provided Python code for the Raspberry Pi is my testing code for my project. This allowed me to test all of my Arduinos and relays connected to them. I tried to make it user friendly so that the other engineers can run the program when I am not around. In this step I will explain, to the best of my ability, the "need to know" parts of the code I provided.
addr = 0x5
addr2 = 0x6
addrN = 0xN
These lines set the addresses for each Arduino thats connected to the device.
bus.write_byte(addr, 0x1)
bus.write_byte(addr, 0x0)
These lines send a byte value of 1 (forward) and 0 (open) to the Arduino at "addr".
bus.write_byte(addr2, 0x2)
bus.write_byte(addr2, 0x0)
These lines send a byte value of 2 (backward) and 0 (open) to the Arduino at "addr2".
Step Five: Operating the programOnce you have uploaded your code and your wiring is secure, log into the Raspberry Pi terminal and type the following code to scan for the Arduino:
i2cdetect -y 1
You should see the address (0x5, 0x6, ...0xN) the Arduino is connected to in the list. This shows us that our connections are secure and the two devices are ready to talk to one another.
Now run the Python program on your Raspberry Pi:
python3 test.py
If everything is working correctly, your relays will close and open depending on which program you run.
Step Six: ConclusionHopefully this helps some people out there who are looking for a more indepth instruction for using I2C connections. You can alter the code I provided to suit your needs in your DIY projects at home. The options are endless.
NOTE: Again, please comment below if there needs to be any changes to code and or schematics. As a sophomore engineering student, there are bound to be people who know more about this than I do. If you are that person, please critique and add comments about the project below!
Comments
Please log in or sign up to comment.