Skip to content
Permalink
Browse files
added an introduction
  • Loading branch information
aa7401 committed Aug 1, 2019
1 parent 304bcdc commit 3d3ae9274f1dda213f38376e54cdf92c5d26343d
Showing 1 changed file with 28 additions and 16 deletions.
@@ -1,8 +1,21 @@
# Using the I2C Protocol

In this tutorial we will configure the I2C interface so that you can attach sensors that make use of it. By the end of this section you should have a screen that displays the computer host name and ip address whenever you power it on. We will also explore how to capture data from an I2C enabled sensor. The materials assume you have already built a base server by following the instructions in the **Getting Started** tutorial. If you have not completed this do it now.
The I2C protocol protocol allows for multiple (slave) devices to be controlled by one or more master devices using only two wires to transmit the data.

## 1 Enabling the I2C Interface
## 1 The I2C Protocol Explained

I2C is a serial communication protocol which means the data is transferred bit by bit which means the speed of communication is relatively slow, typically around 100 kbps in slom mode rising to up to 5Mbps in ultra-fast mode. The two pins are:

1. **SDA** – Serial Data, the line that is used to send and receive the data.
2. **SCL** – Serial Clock, the line used to carry the clock signal.

Each connected device is assigned a 7 bit address (27 unique addresses available) which is built into the hardware which means in normal circumstances only one device of each type can be connected in the same network although some devices allow the address to be changed to one of a fixed number of values. The address of a device should be found in its datasheet and some sites such as [Adafruit](https://learn.adafruit.com/i2c-addresses/the-list) publish a master list.

Each message sent through the I2C bus is preceeded by an _address frame_ which tells the bus which device the data is for. This is read by all the devices and, if the address does not match, the data that follows is ignored. Once the address frame has been sent, this is followed by one or more 8 bit _Data Frames_, each followed by an ACK/NACK checksum bit to ensure the data was received correctly.

In this tutorial we will configure the I2C interface so that you can both send and receive data. By the end of section 3 section you should have a screen that displays the computer host name and ip address whenever you power it on. Section 4 shows how to capture data from an I2C enabled sensor. The materials assume you have already built a base server by following the instructions in the **Getting Started** tutorial. If you have not completed this do it now.

## 2 Enabling the I2C Interface

Out of the box the I2C interface is disabled by default. Out first step is therefore to enable this interface.

@@ -12,7 +25,7 @@ After enabling the interface exit the `raspi-config` tool, you will be asked to

When it has rebooted, log in.

### 1.1 Fixing I2C Permissions
### 2.1 Fixing I2C Permissions

By default, the I2C bus is only accessible if you use root permissions. The problem is that we don't want to run all our python scripts as root! To fix this you need to edit the `/lib/udev/rules.d/60-i2c-tools.rules` file using the nano text editor.

@@ -29,7 +42,7 @@ KERNEL=="i2c-[1-9]*", GROUP="i2c", MODE="0666"

This splits this so that the permissions are retained for interface 0 but every user can access all the others (bus 1).

### 1.2 Changing the I2C Baud Rate
### 2.2 Changing the I2C Baud Rate

By default the I2C bus runs at a slow 100Kb/s speed however the Raspberry Pi has a "fast mode" (400Kb/s) driver. To enable this we need to edit the `/boot/config.txt` file using root privileges:

@@ -45,7 +58,7 @@ dtparam=i2c_arm=on,i2c_arm_baudrate=400000

Having made our changes we need to restart the server.

### 1.3 Scanning for I2C Devices
### 2.3 Scanning for I2C Devices

There is a command called `i2cdetect` which scans an I2C but looking for attached devices. We will be using I2C bus 1 so we run the following command:

@@ -64,7 +77,7 @@ $ sudo i2cdetect -y 1

As you can see, no devices are detected. You should now move on to either section 2 where we configure a small OLED screen or section 3 where we configure a simple sensor and read in data from it.

## 2 Connecting an OLED Screen
## 3 Connecting an OLED Screen

Now lets check the I2C bus is configured correctly by wiring up a small OLED screen. You should find one in your electronics kit. Notice that it has 4 pins labelled `GND`, `VCC`, `SCK`, `SDA`. These need to be connected to the matching pins on the Raspberry Pi header. Below you can see the pins labelled. This labelling applies to both the RPi v2 and v3. If you look carefully at the diagram you can see that the pins connected to the `I2C-1` bus are clearly labelled:

@@ -92,7 +105,7 @@ $ sudo i2cdetect -y 1

This demonstrates both that the i2c bus is configured correctly and that the screen is working.

### 2.4 Writing to the Screen
### 3.1 Writing to the Screen

Finally we can create a simple python script to test out the screen. We first need to install a couple of packages and then some libraries. The first is the Adafruit SSD1306 library that is needed to control the screen.

@@ -123,7 +136,7 @@ chmod +x oled.py

If you look at the screen you should see a message...

### 2.5 Running a Script at Boot
### 3.2 Running a Script at Boot

The final task in this section is to create a script that displays the hostname and ip address on the screen when the raspberry pi first boots. The first task is to create a script in the `/bin/` directory.

@@ -145,13 +158,12 @@ If we want the script to run automatically we need to add it as a _cron_ task. W

This tells the crontab to run the script when the device boots. The `&` character runs the script in the background. Try rebooting the server, you should see the information displayed as soon as the reboot is complete.

## 3 Reading Data from a Sensor
## 4 Reading Data from a Sensor

Many common sensors make use of the I2C protocol which makes it very easy for us to add them to our

In this task you will be adding a TSL2561 light sensor that will allow you to measure the ambient temperature.

In this task you will be adding a GY-61 ADXL335 / 345 Module which can be used to measure the angle of the raspberry pi.
## References

- TC74 temperature sensor
- VEML6070 UV light sensor
- TSL2561 light sensor
- 4 channel ADC
- BMP280 temperature/humidity (also SPI)
- HMC5883L Compass
- [Basics of the I2C Communication Protocol](http://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/)

0 comments on commit 3d3ae92

Please sign in to comment.