-
Notifications
You must be signed in to change notification settings - Fork 3
Using I2C
Unfamiliar with I2C? We have a whole guide on it!
Scarlet supports I2C usage on the Beaglebone Black and Raspberry Pi, but all I2C busses have the same core methods as required by the Scarlet.IO.II2CBus
interface (shown below).
-
void Write(byte Address, byte[] Data)
- Writes the data supplied to the given device address.
-
void WriteRegister(byte Address, byte Register, byte[] Data)
- Writes the data supplied to the register and the given device address.
-
void WriteRegister16(byte Address, byte Register, ushort Data)
- Writes the given 16-bit ushort to the register and the given device address. This is used to easily communicate with 16-bit registers.
-
byte[] Read(byte Address, int DataLength)
- Reads
DataLength
number of bytes from the given device address.
- Reads
-
byte[] ReadRegister(byte Address, byte Register, int DataLength)
- Reads
DataLength
number of bytes starting at the given register of the device.
- Reads
-
ushort ReadRegister16(byte Address, byte Register)
- Reads 16-bits from the register of the given device. This is used to easily communicate with 16-bit registers.
-
void Dispose()
- Used to dispose of the object and free resources when the bus is no longer needed.
Since we have to deal with the device tree on the BeagleBone, there are a few steps we have to take to initialize an I2C bus.
As described here, we need to initialize the BeagleBone before using any IO.
BeagleBone.Initialize(SystemMode.DEFAULT, true);
for example.
Then we will need to set the pins for the I2C bus, check out this chart for help choosing pins. For example, we will use pins P9_17
and P9_26
, which are on I2C Bus number 1.
It is important that these pins come from the same bus.
BBBPinManager.AddMappingsI2C(BBBPin.P9_17, BBBPin.P9_26);
After adding mappings for all I/O pins you want to use in your program, don't forget to apply the pin settings, see the same IO guide listed above if this is not clear.
Once that is all set up, we can define the bus as such:
II2CBus I2C1 = I2CBBB.I2CBus1
(since the pins we defined are on bus number 1)
Using I2C on the Raspberry Pi is simpler than the BeagleBone because there is not device tree manipulation to worry about, although you do need to make sure your I2C bus is turned on by following this guide. Other than that initialization is as simple as this:
II2CBus I2C = new I2CBusPi(BusNum);
Where BusNum
(an int
) is the I2C bus number for your Raspberry Pi (typically this is 1, but some older Pi version use 0, to find out for sure, you can use i2cdetect
, described in "Tips and Troubleshooting" below).
Be sure to only construct one I2C bus on the Pi otherwise thread safety goes away
Here are some handy I2C commands you can run from the command line:
If you don't have these tools, try installing them with: sudo apt-get install i2c-tools
-
i2cdetect
- This allows you to detect devices on an I2C bus. This is helpful in finding the Pi's internal I2C bus number. For example:
sudo i2cdetect 1
will try to detect devices on bus 1. If it works, then that's your bus!
- This allows you to detect devices on an I2C bus. This is helpful in finding the Pi's internal I2C bus number. For example:
-
i2cdump
- This allows you to read registers on an I2C device. For example:
i2cdump -r 0x00-0x10 1 0x48 b
gets registers 0x00 to 0x10 from device 0x48 on bus 1 in 8bit mode (replace b with w for 16b mode)
- This allows you to read registers on an I2C device. For example:
-
i2cset
- This can be used to set registers on an I2C device.
Quick Links:
NuGet
Pin Diagrams: RPi | BBB
Developers: CaiB, Baldstrom
General Info:
Home
Common Issues
Getting Started
Supported Devices
Sections:
Logging
DataLog
Filters
Hardware I/O:
- BeagleBone Black
- Raspberry Pi
- Pin Diagrams: RPi | BBB
- GPIO: Using | For Beginners
- PWM: Using | For Beginners
- ADC: Using | For Beginners
- I2C: Using | For Beginners
- SPI: Using | For Beginners
- UART: Using | For Beginners
- CAN: Using | For Beginners
Networking
Sensors
StateStore
Other: Interesting Case Studies