Skip to content
Permalink
Browse files
updated lab exercise
  • Loading branch information
aa7401 committed Aug 13, 2019
1 parent ceb5b97 commit de89810ed3c2e71ea8b592110ced2a2b4fef0ee5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
@@ -19,21 +19,21 @@ In this tutorial we will configure the I2C interface so that you can both send a

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

Start by running the `raspi-config` command. This time access the _interface options_ and choose `Enable/Disable automatic loading of I2C kernel module` to enable I2C. After exiting the tool you need to reboot. We are now no longer logged in as `root` but rebooting requires root permissions. Since we are part of the _sudoers_ group we can run a command with root privileges by prefixing it with the `sudo` keyword.
Start by running the `raspi-config` command. This requires root privileges but since we are part of the _sudoers_ group we can run a command with root privileges by prefixing it with the `sudo` keyword. This time access the _interface options_ and choose `Enable/Disable automatic loading of I2C kernel module` to enable I2C.

After enabling the interface exit the `raspi-config` tool, you will be asked to reboot the server and this will be needed to activate the I2C interface.
After enabling the interface exit the `raspi-config` tool, you will be asked to `reboot` the server and this will be needed to activate the I2C interface.

When it has rebooted, log in.

### 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.
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 modify the `/lib/udev/rules.d/60-i2c-tools.rules` file using the nano text editor.

```
sudo nano /lib/udev/rules.d/60-i2c-tools.rules
```

Notice that we have to edit the file with root privileges. The Raspberry Pi includes two I2C buses, labelled `i2c-0` and `i2c-1`, We will be using bus 1. Replace the existing contents with the following:
The Raspberry Pi includes two I2C buses, labelled `i2c-0` and `i2c-1`, We will be using bus 1. Add the following:

```
KERNEL=="i2c-0" , GROUP="i2c", MODE="0660"
@@ -60,7 +60,13 @@ Having made our changes we need to restart the server.

### 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:
There is a command called `i2cdetect` which scans an I2C but looking for attached devices. This needs to be installed:

```shell
sudo apt install -y i2c-tools
```

We will be using I2C bus 1 so we run the following command:

```
$ sudo i2cdetect -y 1
@@ -7,22 +7,29 @@
# $ sudo apt-get install python3 python3-pip python-dev python-smbus
# $ sudo python3 -m pip install --upgrade pip setuptools wheel
# $ sudo pip3 install smbus
#!/usr/bin/env python3

#!/usr/bin/env python3

import smbus
import time

COMPASS_ADDRESS = 0x60
import statistics

bus = smbus.SMBus(1)

if __name__ == "__main__":
while 1:
try:
print(smbus.SMBus(1).read_byte_data(COMPASS_ADDRESS, 0x1E)) # print calibration state
bearing_LSB = bus.read_byte_data(COMPASS_ADDRESS, 2)
bearing_MSB = bus.read_byte_data(COMPASS_ADDRESS, 3)
bearing = ((bearing_LSB << 8) + bearing_MSB) / 10
print(bearing)
time.sleep(0.1)
except OSError:
print("OSError")
DEVICE_ADDRESS = 0x60
DEVICE_REG_BEARING = 0x01

values = []

while 1:
try:
bearing = bus.read_byte_data(DEVICE_ADDRESS, DEVICE_REG_BEARING)
bearing = int((bearing / 255) * 360)
values.append(bearing)
if len(values) > 10:
values.pop(0)
print(statistics.mode(values))
time.sleep(0.1)
except:
pass

0 comments on commit de89810

Please sign in to comment.