Raspberry Pi to Arduino communication using I2C and interrupts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* RPi2c - test i2c communication between an Arduino and a Raspberry Pi. | |
* | |
* Copyright (c) 2013 Carlos Rodrigues <cefrodrigues@gmail.com> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
#include <Wire.h> | |
static const char intPin = 8; | |
static const char ledPin = 9; | |
static const char sensorPin = A0; | |
short ledValue = 0; | |
short sensorCurr = 0; | |
short sensorPrev = -10; | |
void setup() | |
{ | |
pinMode(intPin, OUTPUT); | |
pinMode(ledPin, OUTPUT); | |
Wire.begin(0x03); | |
Wire.onReceive(receiveEvent); | |
Wire.onRequest(requestEvent); | |
} | |
void loop() { | |
sensorCurr = 0; | |
// Take a few samples and average the results to avoid jitter... | |
for (int i = 0; i < 10; i++) { | |
sensorCurr += analogRead(sensorPin); | |
delay(1); | |
} | |
sensorCurr /= 10; | |
// Trigger an interrupt on the RPi when the sensor changes... | |
if (abs(sensorCurr - sensorPrev) > 1) { | |
sensorPrev = sensorCurr; | |
digitalWrite(intPin, HIGH); | |
delay(10); | |
digitalWrite(intPin, LOW); | |
} | |
} | |
void receiveEvent(int bytes) { | |
int operation = Wire.read(); | |
// Change the LED intensity... | |
if (operation == 0x01 && bytes > 1) { | |
ledValue = Wire.read(); | |
analogWrite(ledPin, ledValue); | |
} | |
// Consume any remainder bytes... | |
while (Wire.available()) { | |
Wire.read(); | |
} | |
} | |
void requestEvent() { | |
// Both the Arduino and RPi are little-endian, no conversion needed... | |
Wire.write((uint8_t *)&sensorCurr, sizeof(sensorCurr)); | |
} | |
/* EOF - RPi2c.pde */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# RPi2c - test i2c communication between an Arduino and a Raspberry Pi. | |
# | |
# Copyright (c) 2013 Carlos Rodrigues <cefrodrigues@gmail.com> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# | |
from __future__ import division | |
from __future__ import print_function | |
import RPi.GPIO as GPIO | |
import smbus | |
import time | |
import sys | |
I2C_BUS = 1 | |
I2C_SLAVE = 0x03 | |
INTERRUPT_PIN = 17 | |
def interpolate(value, a1, a2, b1, b2): | |
# Normalize the value into a 0..1 interval... | |
n = float(value - a1) / float(a2 - a1) | |
# Scale the normalized value to the target interval... | |
return b1 + (n * (b2 - b1)) | |
if __name__ == '__main__': | |
# Initialize the interrupt pin... | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(INTERRUPT_PIN, GPIO.IN) | |
# Initialize the RPi I2C bus... | |
i2c = smbus.SMBus(I2C_BUS) | |
while 1: | |
try: | |
# Wait until the Arduino triggers the interrupt... | |
GPIO.wait_for_edge(INTERRUPT_PIN, GPIO.RISING) | |
try: | |
# Get the sensor value from the Arduino (signed 16bit little-endian)... | |
sensor_value = i2c.read_word_data(I2C_SLAVE, 0x00) | |
sys.stdout.write("sensor: %d" % sensor_value) | |
except IOError: | |
sys.stderr.write("*** error: receiving sensor value ***\n") | |
continue | |
sys.stdout.write(" / ") | |
try: | |
# Map the sensor value to a [0, 255] interval... | |
led_value = int(interpolate(sensor_value, 0, 1023, 0, 255)) | |
sys.stdout.write("led: %d\n" % led_value) | |
# Send the PWM value for the LED to the Arduino... | |
i2c.write_byte_data(I2C_SLAVE, 0x01, led_value) | |
except IOError: | |
sys.stderr.write("*** error: sending led value ***\n") | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
# vim: set expandtab ts=4 sw=4: |
What about any protocol? :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi carlosefr,
I have downloaded your code and it works perfectly and is a very good example indeed of connecting RPi and arduino with the I2C bus.
Br, Olav