Exploring the DFRobot Unihiker: Screen, GUI and Python 

 December 23, 2023

By  Peter

Join Our Mailing List

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community.

One email per week, no spam, unsubscribe at any time.

In my latest experiment, I dive into the intriguing world of the Unihiker device by DFRobot. This exploration is not just about tinkering with the hardware, but also about understanding the Python libraries that enhance its capabilities. The Unihiker is equipped with an array of onboard sensors and connectors, which I navigated through by developing a simple demo application.

The application is quite straightforward; it involves taking inputs from the gyroscope and accelerometer and displaying these values. Additionally, I experimented with controlling an LED via the touchscreen, a process I found surprisingly simple. I aimed to demystify the process of writing Python scripts to leverage the Unihiker’s built-in features. The result is a Python script that graphically represents the accelerometer’s X, Y, and Z values while allowing interaction with an LED through the screen.

I also provided a walkthrough of setting up and programming the Unihiker, emphasizing the use of Python libraries like Pinpong and Unihica. These libraries are critical in accessing and controlling the device’s various components, from LEDs to sensors. My programming journey involved utilizing the Jupyter service for a more interactive and web-based environment, showcasing different ways to run and test Python scripts.

My curiosity didn’t stop at the demo application. I’m now keen on delving deeper into the device’s capabilities, particularly its multi-threading capabilities due to its multi-core microprocessor. The Unihiker is not just a tool but a canvas for creative and practical experimentation, and I look forward to uncovering more of its potential.

I invite you to watch the full video on my YouTube channel for a detailed guide through this Python adventure with the Unihiker device. There, you’ll see each step of the process, from initial setup to script execution, and maybe get inspired for your own experiments!

The code

Here’s the code that I used in my Unihiker experiments.


This code blinks an LED. Pin 25 is for the on-board LED and pin 21 is for the external LED.

# -*- coding: UTF-8 -*-
#This is a sample code using the PinPong Library to control the on and off of an LED and
#print corresponding messages when the LED is on/off. Comments are added after each line of the code to improve its
import time
from pinpong.board import Board, Pin

Board().begin() # Initialize the UNIHIKER
led = Pin(Pin.P25, Pin.OUT)

# Set Pin P25 as an output pin for the LED

while True:
    led.write_digital(1) # Turn on the LED by setting the pin value to high
    print("The LED light is on") # Display a message indicating that the LED light is on
    time.sleep(1) # Pause the program for 1 second
    led.write_digital(0) # Turn off the LED by setting the pin value to low
    print("The LED light is off") # Display a message indicating that the LED light is off
    time.sleep(1) # Pause the program for 1 second


This code reads values from the on-board motion sensor and prints them to the console.

# -*- coding: utf-8 -*-
import time
from pinpong.board import *
from pinpong.extension.unihiker import *


while True:


​This code extends accel.py to display the values on the Unihiker LCD, including some graphics.

# -*- coding: utf-8 -*-
import time
from pinpong.board import *
from pinpong.extension.unihiker import *
from unihiker import GUI # Import the package

gui = GUI() # Instantiate the GUI class


led_state    = 1 # Keeps track of the external LED state
button_color = "red"
led          = Pin(Pin.P21, Pin.OUT)
led.write_digital(led_state) # Start with the LED on

def toggle_led():
    global led_state
    led_state = not led_state

while True:
    acc_x = accelerometer.get_x()
    acc_y = accelerometer.get_y()
    acc_z = accelerometer.get_z()
    acc_strength = accelerometer.get_strength()
    gyr_x = gyroscope.get_x()
    gyr_y = gyroscope.get_y()
    gyr_z = gyroscope.get_z()

    gui.fill_rect(x=0, y=3, w=160, h=20, width=1, color=(255, 0, 0))
    gui.draw_text(x=0, y=0, text='Acc x: ' + str(acc_x), color="white", origin='nw')
    gui.fill_rect(x=0, y=29, w=160, h=20, width=1, color=(0, 153, 0))
    gui.draw_text(x=0, y=26, text='Acc y: ' + str(acc_y), color="white", origin='nw')
    gui.fill_rect(x=0, y=53, w=160, h=20, width=1, color=(0, 0, 204))
    gui.draw_text(x=0, y=50, text='Acc z: ' + str(acc_y), color="white", origin='nw')
    # Clear the lines
    gui.fill_rect(x=0, y=150, w=240, h=70, width=0, color="white")
    #draw the X accel line
    gui.draw_line(x0=120, y0=160, x1=(120 + acc_y * 100), y1=160, width=10, color=(255, 0, 0))

    #draw the Y accel line
    gui.draw_line(x0=120, y0=180, x1=(120 + acc_x * 100), y1=180, width=10, color=(0, 153, 0))
    #draw the C accel line
    gui.draw_line(x0=120, y0=200, x1=(120 + acc_z * 100), y1=200, width=10, color=(0, 0, 204))

    if led_state == 0:
        button_color = "black"
        button_color = "red"
    gui.fill_round_rect(x=20, y=280, w=200, h=30, r=8, width=3, color=button_color, onclick=toggle_led)

accel_gui.py in operation​



You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

The Khadas VIM1S is a high-quality yet affordable single-board computer well suited to hobbyists’ needs. While it doesn’t have the power of its siblings, such as the Edge2, it strikes a fine balance between performance

Read More
Khadas VIM1S: Unbox, Set Up, and Review

With the right tools, creators can transform ideas into tangible innovations, driving the maker movement forward. 3D design tools have become indispensable for makers, hobbyists, and professionals because they allow us to capture ideas of

Read More
The best 3D design tools for Makers