Home > Allwinner H-Series, Hardware, Linux, Programming, Testing, Ubuntu, Video > Using GPIOs on NanoPi NEO 2 Board with BakeBit Starter Kit

Using GPIOs on NanoPi NEO 2 Board with BakeBit Starter Kit

NanoPi NEO 2 is a tiny 64-bit ARM development board powered by Allwinner H5 processor. FriendlyELEC sent me a couple of NEO 2 samples together with their BakeBit Start Kit with a NanoHat and various modules via GPIOs, analog input or I2C. I’ve already tested both Armbian with Linux 4.11 and Ubuntu Core Qt with Linux 3.10, and ran a few benchmarks on NanoPi NEO 2. You would normally prefer to use the Armbian image with Linux mainline since it provided better performance, but at the time I was told GPIO support was not there.

Configuring NanoPi NEO 2 board with BakeBit library

So this week-end, when I decided to test GPIO support and BakeBit Starter Kit, I decided to follow this advice, especially nanopi-neo2-ubuntu-core-qte-sd4g-20170329.img.zip image is still the recommended one in the Wiki. So I went with that image.

I’ll use Python examples from Bakebit library, but if you prefer something similar to WiringPi, you may consider using WiringNP library directly instead of using Bakebit. Since NanoHat Hub comes with header with digital I/O (including 2 PWM), analog input, I2C and UART interfaces, I’ll make sure I try samples for all interfaces I have hardware for. FriendlyELEC did not include a module with a UART interface, so I’ll skip that one.

I followed instructions in BakeBit wiki from a terminal which you can access from the serial console or SSH. First, we need to retrieve the source code:

Then we can start the installation:

The last line will install the following dependencies:

  • python2.7           python2.7
  • python-pip         alternative Python package installer
  • git                        fast, scalable, distributed revision control system
  • libi2c-dev           userspace I2C programming library development files
  • python-serial     pyserial – module encapsulating access for the serial port
  • i2c-tools              This Python module allows SMBus access through the I2C /dv
  • python-smbus   Python bindings for Linux SMBus access through i2c-dev
  • minicom             friendly menu driven serial communication program
  • psutil                   a cross-platform process and system utilities module for n
  • WiringNP           a GPIO access library for NanoPi NEO

This will take a while, and after it’s done, the board will automatically reboot.

We can check if everything is properly running, but try out one of the Python scripts:

hmm, python-smbus was supposed to be installed via the installation script. Let’s try to install it manually:

Running the command again with verbose option shows the download URL is not valid:

So I went to https://pypi.python.org/simple/ looking for another python-smbus library in case the name has changed, and I finally installed the pysmbus:

I could go further, but the I2C bus was not detected:

So maybe the driver needs to be loaded. But running sudo modprobe i2c_sunxi it does nothing, and I could notice the .ko file is missing from the image…

So let’s try to build the source code for the board following the Wiki intructions:

We also need to install required build packages…

… download gcc-linaro-aarch64.tar.xz toolchain, and copy it to lichee/brandy/toolchain directory (do not extract it, it will be done by the build script).

Now we can try to build the kernel for NanoPi NEO 2 (and other Allwinner H5 boards).

and it failed with more errors possible related to CROSS_COMPILE flag. There must be a better solution… FriendlyELEC guys might not work on Saturday afternoon, and while I did contact them, I decided to try one of their more recent images with Linux 4.11 available here.

Let’s pick nanopi-neo2_ubuntu-core-xenial_4.11.0_20170518.img.zip since it has a similar name, and is much newer (released 3 days ago). I repeated the installation procedure above, and …

Success! Albeit after 4 to 5 hours of work… Let’s connect hardware to ind out whether it actually works, and not just runs.

Analog Input and Digital Output – Sound Sensor Demo

The simplest demo would be to use the LED module, but let’s do something more fun with the Sound Sensor demo I found in BakerBit Starter Kit printed user’s manual, and which will allow us to use both digital output with the LED module connected to D5 header, and analog input with the Sound sensor module connected to A0 header. Just remember the long LED pin is the positive one.

You can run the code as follows:

I changed the source a bit including the detection threshold, and timing to make it more responsive:

The LED will turn on each time the the sound level (actually analog voltage) is above 1.46V.

PWM and Analog Input – Servo and Rotary Angle Sensor Demo

We can test PWM output using the Servo module connected to D5 header, and control it using the rotary angle sensor module connected the A0 analog input header .

Click to Enlarge

The sample for the demo runs fine, and use the potentiometer is detected:

However, the servo is not moving at all. Raspberry Pi relies on rpi-config to enable things like I2C and other I/Os, and I noticed npi-config in the Wiki for NEO 2. So I ran it, and sure enough PWM was disabled.

So I enabled it, and answered Yes when I was asked to reboot. The only problem is that it would not boot anymore, with the system blocked at:

So maybe something went wrong during the process, so I re-flashed the Ubuntu image, reinstalled BakeBit, and re-enabled PWM0. But before rebooting, I checked the boot directory, and noticed boot.cmd, boot.scr, and the device tree file (sun50i-h5-nanopi-neo2.dtb) had been modified. The DTB looks fine, as I could decode it, and find the pwm section:

Let’s reboot the board. Exact same problem with the boot stuck at “Starting kernel…”. So there’s something wrong with the way npi-config modifies one or more of the files. With hindsight, I should have made a backup of those three files before enabling PWM the second time… I’ll give up on PWM for now, and ask FriendlyELEC to look into it.

I2C and Analog Input – OLED UI controlled with Joystick

The final test I’ll use the I2C OLED display module connected to one of the I2C headers, together with the analog joystick module connected to A0 header.

Click to Enlarge

Let’s run the sample for the demo:

It works, but there’s a bit of a lag, and the sample may have to be improved to better detect various states. I’ll show what I mean in the video below.

The bad parts are that documentation is not up-to-date, enabling PWM will crash the image, and while the Python sample do demonstrate IO capabilities, they should probably be improved to be more responsive. The good part is that we’re getting there, the hardware kit is a really nice, and I think the documentation and software should become much better in June, as FriendlyELEC has shown to be responsive to the community issues.

What? Python sucks? You can use C language with GPIOs too

If Python is not your favorite language, FriendlyELEC also provided some C languages samples in the C directory:

As we’ve seen above, Bakebit library appears to rely on WiringNP, and you’d normally be able to list the GPIOs as follows:

The utility is not too happy about seeing an Allwinner H5 board. But maybe the library in the board is not up-to-date, so I have built it from source:

and run the gpio sample again:

Excellent! It’s not quite a work-out-of-box experience, but NanoPi NEO 2 can be used with (most) GPIOs.

My adventures with NanoPi NEO 2 board are not quite done, as I still have to play with NanoHat PCM5102A audio add-on board, which I may end up combining with a USB microphone to play with Google Assistant SDK, and I’m expecting NanoPi NAS Kit v1.2 shortly. I’ll also update this post once PWM is working.

  1. Mark Birss
    May 21st, 2017 at 15:48 | #1

    Very nice review. I also had to install a missing package for getting a (diy) NanoHat OLED working on NanoPi NEO with the Ubuntu Core image

    sudo apt-get install python-imaging

    oled python scripts under NanoHatOLED/BakeBit/Software/Python

  2. theguyuk
    May 21st, 2017 at 16:15 | #2

    Good review and nice to see friendlyelec are still improving, lets hope they respond to the positive feedback software issues.

    These kits need group product testing more, some quality control imho.

    Cnx have you notice friendlyelec have a clear Nanopi K2 case listed now!

  3. tkaiser
  4. tkaiser
    May 21st, 2017 at 17:14 | #4

    And wrt ‘Now we can try to build the kernel for NanoPi NEO 2 (and other Allwinner H5 boards)’: Please don’t recommend this since this repo contains Allwinner’s broken H5 BSP stuff. It will only cause others wasting the same amount of time as you already.

    Few weeks ago we had intensive email conversation with FriendlyELEC and they got it that dropping Allwinner’s BSP is the best idea with both NEO and NEO 2 (since the only use cases for this smelly 3.10 kernel contained in the BSP would be related to display output and video which is no issue on these boards since lacking any display connector). Weidong struggled a bit in the beginning but they finally managed to get their hardware up and running with 4.11 (no idea though whether with their OS images kernel/u-boot is also packaged so necessary updates are part of ‘apt upgrade’ process)

    I hope they improve even more here and become part of the mainlining process (submitting the necessary patches/drivers for their own hardware upstream and also adopting maintainer role for their hardware)

  5. May 21st, 2017 at 20:49 | #5

    Well, it’s not really a recommendation, I just documented the steps I went through before I gave up on Linux 3.10 image. That’s because before testing I thought that the Linux 4.11 image might not support GPIO well, as I wrote in the introduction. But finally considering Linux 4.11 is mostly working, there’s little reason to use Linux 3.10 anymore.

  6. gizmoduck
    May 21st, 2017 at 21:54 | #6

    I’d like to use this for a DSP (digital signal processing) project. Does the NanoPi NEO2 with the ADC on the NanoHat Hub support uniform sampling say at 50 – 100 ksps?

    If not has anyone used the NEO2 with an external ADC over SPI where the ADC provides an interrupt to tell the CPU to read from the ADC? If so, what sample rates have you achieved?

    My ultimate goal is to sample an analog signal at 50 – 100 ksps, do some signal processing (ie. filtering, scaling, FFT, etc.) and send the processed signal out over Ethernet for more heavy processing.

    Thank you.

  7. May 22nd, 2017 at 09:29 | #7

    The ADC is handled by Atmel ATMega328 AVR MCU, so I would not expect a high sampling rate. The answers about about sampling rate on Arduino Uno may give an idea: https://arduino.stackexchange.com/questions/699/how-do-i-know-the-sampling-frequency?answertab=votes#tab-top

    I don’t know the answer to your question about ADC over SPI.

  8. gizmoduck32
    May 22nd, 2017 at 20:49 | #8


    Thank you for the reply.

    That link seems to indicate that the ATMega328 can potentially get up to 200 ksps but requires changing the ADC clock via its register settings. Can the ATMega328 on the NanoHat Hub be reprogrammed?

    Thank you.

  9. May 22nd, 2017 at 21:10 | #9

    Yes, it can be reprogrammed. There’s a section about that in the Wiki.

  10. gizmoduck32
    May 23rd, 2017 at 10:18 | #10

    Thank you!

  1. No trackbacks yet.