I2C interface using python over MacOS to Accelerometer

Viewed 392

I plan to use StandardFirmata (already flashed to the Arduino board) protocol and be able to read/write to MMA7660FC 3-Axis Orientation/Motion Detection Sensor, which is already mounted on a Grove Beginner Kit pcb.

Using Arduino code i2c_scanner.ino, I can see the various devices over I2C bus. But I am unable to read/write to the accelerometer (device address is 0x19.

I am using a MacBook and want to work off of that and not Raspberry Pi.

I used the following lines of code:

from smbus2 import SMBus
bus = SMBus(1)

And got this error:

  File "/Users/sth/Documents/Arduino/smbus_test.py", line 63, in <module>
    bus = SMBus(1)
  File "/Users/sth/mypython/lib/python3.7/site-packages/smbus2/smbus2.py", line 279, in __init__
    self.open(bus)
  File "/Users/sth/mypython/lib/python3.7/site-packages/smbus2/smbus2.py", line 308, in open
    self.fd = os.open(filepath, os.O_RDWR)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'
1 Answers

Short Answer: You dont have a device mapping point to your i2c-1. ls -l /dev/i2c-1 will show you the same error from your mac

Long Answer: When you use smbus2, you are using an interface to your machine peripherals. In a Unix-like environment, those are mapped in the /dev folder. So, in order to operate your device using your Mac, you will need to first have to enable the i2c in your kernel. For Linux environments, this usually means to enable it in your device tree. The error you are having is because you are trying to connect to an i2c-1 from your Mac machine. Which does not make too much sense for some reasons:

  • You dont have the i2c interface enabled in your mac that is mapping a /dev/i2c-1. That is what SMBus(1) means. The param 1 means port 1 of an i2c mapping
  • Your device connected to the Raspberry board will interface with Raspberry i2c. Not your mac.

If you want to write code in your Mac to operate with your module, I would suggest buying a USB to i2c device. Like this one from Adafruit:

https://www.adafruit.com/product/4471

I did not test this device. But, it does look like a good option for you to write your code and test the device. But, keep in mind that this device will map some /dev/i2c-whatever. Which might not be the same for your Raspberry PI. So, make it configurable is a MUST

Related