Power & Source of Big Ideas

Using I2C on NanoPi Duo with Python

Moderators: chensy, FATechsupport

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.
NanoPi Duo 2x9 Pin Header smaller.jpg
NanoPi Duo 2x9 Pin Header smaller.jpg (124.49 KiB) Viewed 7056 times

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.
npi-config I2C.jpg
npi-config I2C.jpg (149.03 KiB) Viewed 7056 times

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

i2cDetect.jpg (55.89 KiB) Viewed 7056 times
(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)
we have to change it to

Code: Select all

i2c = smbus.SMBus(0)

Now it works! 8-)
phpBB [video]
I2C works fine, but if you use a Python library, written for Raspberry Pi,

Why isn;t there one for the NanoPi Duo, then ? that would be much better to use.
Great writeup, thank you.
Why doesn't Friendly Elec do much, much more in the way of giving examples of how to do things.
Like I2C, SPI and so on. I think more people would gravitate to it if they did that.
Just today I went here for WiringNP:

And the NanoPi Duo is not even listed there. I think they are pumping boards out faster than they can keep up with the documentation and support.

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests (based on users active over the past 5 minutes)
Most users ever online was 5185 on Wed Jan 22, 2020 1:44 pm

Users browsing this forum: No registered users and 5 guests