Skip to content

Instantly share code, notes, and snippets.

@owencsmith
Created March 22, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owencsmith/1c2f62cc8c3af54050d81645cbb33d69 to your computer and use it in GitHub Desktop.
Save owencsmith/1c2f62cc8c3af54050d81645cbb33d69 to your computer and use it in GitHub Desktop.
A python file that defines the class used to communicate to an Arduino from a Raspberry Pi over I2C
import smbus
import time
class Communication:
def __init__(self, address=0x00):
# This is the address we setup in the Arduino Program
self._address = address
# for RPI version 1, use “bus = smbus.SMBus(0)”
self._bus = smbus.SMBus(1)
print("Communication Class initialized")
# This is the address we setup in the Arduino Program
def writeNumber(self, value):
self._bus.write_byte(self._address, value)
# bus.write_byte_data(address, 0, value)
print("Value sent")
return -1
def readNumber(self):
number = self._bus.read_byte(self._address)
# number = bus.read_byte_data(address, 1)
return number
if __name__ == "__main__":
ac = Communication()
while True:
var = input("Enter 1 – 9: ")
if not var:
continue
ac.writeNumber(var)
print(f"RasPI: Hi Arduino, I sent you {0}\n", var)
# sleep one second
time.sleep(1)
number = ac.readNumber()
print(f"Arduino: Hey RasPI, I received a digit {0}\n", number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment