I2C works fine, but if you use a Python library, written for Raspberry Pi, remember the NanoPi Duo has 3 I2C-BUSes!
First take a look at the NanoPi Wiki, because we must know which I2C Bus we are using with the 2x9 Pin Header.
OK, we are using I2C0, this is Important to know, because in Python Scripts written for other Computer, they may using an other Bus. If I2C1 or I2C2 is addressed, we have to change the Script to I2C0.
Now we take a look at the NanoPi Software Configuration Tool (npi-config), because we want to know I2C is enabled, or we have to do so.
First select 5 Advanced Options, then select A4 I2C and last select i2c0 Enable/Disable.
If i2c0 is enabled, you can leave the menu. otherwise enable it and reboot.
Now we can use I2C. For me was a library for driving 20x4 Char LCD or OLED displays interesting. Many Python library's requires that you have previously installed some packets and some other can help us, so we do install some packets now.
If we have installed one ore more I2C devices on our NanoPi, we can check there addresses out now.
(Address 76 is a BME280 and address 3d is a Char OLED.)
I have attached a BME280 (Temperature Humidity Pressure Sensor) and an I2C 20x4 Char OLED (NHD-0420CW-AY3), because it works automatically with 3.3 V and with 5 V. To use the OLED, I installed I2cCharDisplay.
When I start the Demo, it will not work and ends with an Error, but we are using possibly an other I2C Bus, so let us take look into the lib.
Important for us is this Part: we have to change it to
Now it works!
First take a look at the NanoPi Wiki, because we must know which I2C Bus we are using with the 2x9 Pin Header.
OK, we are using I2C0, this is Important to know, because in Python Scripts written for other Computer, they may using an other Bus. If I2C1 or I2C2 is addressed, we have to change the Script to I2C0.
Now we take a look at the NanoPi Software Configuration Tool (npi-config), because we want to know I2C is enabled, or we have to do so.
Code: Select all
sudo npi-config
First select 5 Advanced Options, then select A4 I2C and last select i2c0 Enable/Disable.
If i2c0 is enabled, you can leave the menu. otherwise enable it and reboot.
Code: Select all
sudo shutdown -r now
Now we can use I2C. For me was a library for driving 20x4 Char LCD or OLED displays interesting. Many Python library's requires that you have previously installed some packets and some other can help us, so we do install some packets now.
Code: Select all
pi@npiduo:~$ sudo apt-get update
pi@npiduo:~$ sudo apt-get install build-essential python-pip python-dev python-smbus i2c-tools git
If we have installed one ore more I2C devices on our NanoPi, we can check there addresses out now.
Code: Select all
pi@npiduo:~$ sudo i2cdetect -y 0
(Address 76 is a BME280 and address 3d is a Char OLED.)
I have attached a BME280 (Temperature Humidity Pressure Sensor) and an I2C 20x4 Char OLED (NHD-0420CW-AY3), because it works automatically with 3.3 V and with 5 V. To use the OLED, I installed I2cCharDisplay.
Code: Select all
pi@npiduo:~$ git clone https://github.com/wht-io/i2c-char-display-raspberrypi.git
When I start the Demo, it will not work and ends with an Error, but we are using possibly an other I2C Bus, so let us take look into the lib.
Code: Select all
import smbus # import the i2c library
from time import sleep # import the sleep functions
# python SMBus commands: http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-pyt
i2c = smbus.SMBus(1) # create an i2c object for writing/reading from i2c
Important for us is this Part:
Code: Select all
i2c = smbus.SMBus(1)
Code: Select all
i2c = smbus.SMBus(0)
Now it works!
