Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

General I2C Tutorial

Jaden Bottemiller edited this page Jun 27, 2018 · 9 revisions

Introduction

This tutorial will walk you through the basics of setting up and talking to a device with I2C. The goal is to help unfamiliar users get up to speed with how to use some of the cool features in Scarlet. As such, this tutorial is focused on beginners; if you are familiar with I2C this section probably won't help you much, and we recommend just using the general documentation linked below in bold.

For the I2C general overview/basic information, consult this documentation page.

So you have a device that uses I2C and you want to talk to it using Scarlet. First of all, it's good to know how I2C works. If you are unfamiliar with the platform this could get pretty confusing. Sparkfun has a good written tutorial as well as a video that includes SPI. Please take the time to understand the protocol if you are trying to use it. This Texas Instruments I2C Overview can also be very helpful!

I2C is generally an easy protocol to deal with; this tutorial should do a decent job explaining this to all levels. It goes into a lot to try and help you understand exactly what is happening, but this much information could be overwhelming, so just remember that if you follow the code in this example it will get you started. The more you use I2C, the more familiar you will become with it.


Talking to the device

To be clear, this is not the way you should talk to a device with Scarlet. To make a new device, please refer to the documentation here. (TODO: Link this documentation)


MPU 6050 Notes

If you have taken the time to understand I2C, you know that to use it we need to know the device address in order to communicate. Sometimes that information is hard or tricky to find on a device sometimes it's easier. The MPU-6050 is an I2C or SPI sensor where that information is relatively easy to find. Best way to find it is to look at the documentation, CTRL/CMD+F "address" and click through until you find detailed information on the I2C address because sometimes there is a nuance. In this example, the I2C address information is detailed on page 33. The address is typically b1101000 which is 0x68 in hex (104 decimal) but can be 0x69 (105) depending on if one of the pins on the chip (A0 in this case) is pulled high. This is to allow for two of the same devices on the same I2C bus. If one's A0 pin is pulled high and the other is pulled low, there will be no device collision on the bus and everyone is happy! (Notice that A0 means address bit 0) We will assume for this example that A0 is low and the address is 0x68.

In some devices, you can only talk to the device itself (i.e. you can just write or read to the device address), but on most you can choose from multiple onboard devices with their own address to talk to, these are called registers and we need to know a lot about the registers specific to the device if we plan to use it. What's nice about the MPU 6050 is that it has a whole document dedicated to its registers (this is atypical, but they do it because this device has a LOT of registers, usually devices will have their registers detailed in their main documentation).

How is it possible that a device has a bunch of onboard devices on the same I2C bus? How does that work?

Good question. It is all handled in the backend of Scarlet and by most I2C libraries, so if you are not worried about understanding this throuroughly you can skip through the rest.

Those "onboard devices" are not visible to the main I2C bus they are only onboard, so when we talk to the device we need to tell it what register we are trying to deal with. To handle this, the standard is to begin an I2C communication with a register. We must write, to the device, one byte that tells the system which register to access. Then either write the data right after that transmission, or start a read command with the device. Below is a link, I recommend taking a look at page 7 if you are still confused (as you likely are if this is your first experience with I2C).

Logo

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

Clone this wiki locally