Raspberry Pi - Arduino - Link the two via the I2C bus
Warning : This article has been automatically translated by Bing Translate
Danger : This article has been automatically translated by Bing Translate
Caveat : This article has been automatically translated by Bing Translate
Some say that it not dogs with sausages but sometimes why not binding. Then Raspberry Pi and the Arduino are not necessarily also opposed, with even points in common but each benefits that can be good to combine.
With Raspberry Pi you can via ports GPIO control external components and of the hack in electronics. But I imagine that some already have mastered their Arduino, they have the equipment, etc. Et puis ça fait un tuto en plus 😀
To bind the two, We will pass by the I2C Protocol. Several reasons for this : do not use the USB port, Raspberry Pi is limited (If you want to do it anyway there is an article in the MagPi #15 and I will make a myself later). And do not use the serial port. Here it's a little more personal, because I have a card Slice Of Pi on a Raspberry for the weather station and it uses the serial port for wireless sensors.
The I2C Protocol, created by Philips with a home orientation, allows several components to interact between them in a bidirectional way but half-duplex only. As usual, more info at Wikipedia.
There is a principle of master and slave. Here so we will use Raspberry Pi as master and the Arduino as a slave.
Primarily to connect two devices need to know what pins to use !
For the raspberry is on the GPIO 2 and 3 (for maps from model B revision 2, those with the mounting holes in the map)
For the Arduino, It happens on the A4 and A5 pins for the Arduino Uno type cards. For other cards, You can find more information on the official Arduino site.
Need to add a connection to the mass between the two. So you need three wires.
Before connecting our two devices, It must be ensured that they will be able to communicate. All the information is on the site of adafruit but wholesale, Here's what to do.
Define the Arduino as a slave
It is relatively simple by including the Wire module in the program. I took the address 0 x 12 but you can choose 0 x 0 x 77 03 (in hex) :
#include <Wire.h> #define SLAVE_ADDRESS 0 x 12 int dataReceived = 0; void setup() { Serial.begin(9600); Wire.begin(SLAVE_ADDRESS); Wire.onReceive(receiveData); Wire.onRequest(sendData); } void loop() { delay(100); } void receiveData(int byteCount){ While(Wire.available()) { dataReceived = Wire.read(); Serial.print("Data received : "); Serial.println(dataReceived); } } void sendData(){ int send = dataReceived + 1; Wire.write(sending); }
You'll understand, the program Initializes the Arduino with address 0 x 12, defines the method of receiving data, the method of sending data and go to send it to the card with the interface.
Enable management I2C on Raspberry Pi
By default, distributions of type Raspbian disable I2C management to save the memory used by the module. To re-enable, It's easy and it is in the file /etc/modprobe.d/raspi-blacklist.conf. Need to be commented out with a # the line :
blacklist i2c-bcm2708
Rest to enable the module each time you start. It is in the file /etc/modules, by adding at the end a new line :
I2C - dev
Then you install the suite of management I2C tools
apt-get install i2c-tools
You will have to restart the Raspberry to appear two files :
Thanks to them you will be able to communicate on the I2C bus !
You can list devices with the command :
i2cdetect y 1
For 256 MB models (as one that I used for my tests) :
i2cdetect y 0
Voilà. Next step : connect the Arduino and talk to him !
Communicate with the Arduino
You can now connect the Arduino to the Raspberry and turn all this small world !
Attention, on the schema, SDA and SCL connection is reversed ! Le rouge en A5 et le balnc va en A4 😉
Once done, you log on to the Raspberry Pi and you restart the command i2cdetect with the proper parameter :
The setting in the sketch to the Arduino, 0x 12, appears in the detected devices. It progresses !
We now need to implement the script on Raspberry Pi who will send/receive with the Arduino. For this, the simplest is to install the support of I2C in Python scripts. This is what will make python-smbus
apt - get install python-smbus
We are now equipped to speak to our Arduino. For this a small program everything simple. Send a digit between 0 and 8 and return us this number plus one :
smbus import time import # Replace 0 by 1 so new bus Raspberry = smbus.SMBus(0) address = 0 x 12 print "The value sent 3" bus.write_byte(address, 3) # Break 1 second to allow time for the treatment to be time.sleep(1) response = bus.read_byte(address) print "The response of the arduino : ", response
The result of the execution :
Let talk your imagination to now lead the Arduino by sending applications a little more complex depending on what you plugged it !
Source and inspiration : a section d’Oscar
Pingback: C’est pour bientôt | La caverne de Zragg()