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.
led.py
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
#readability.
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
accel.py
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 *
Board().begin()
while True:
print(accelerometer.get_x())
print(accelerometer.get_y())
print(accelerometer.get_z())
print(accelerometer.get_strength())
print(gyroscope.get_x())
print(gyroscope.get_y())
print(gyroscope.get_z())
print("------------------")
time.sleep(1)
accel_gui.py
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
Board().begin()
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
led.write_digital(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()
#print(acc_x)
#print(acc_y)
#print(acc_z)
#print(acc_strength)
#print(gyr_x)
#print(gyr_y)
#print(gyr_z)
#print("------------------")
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"
else:
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)
time.sleep(0.1)
accel_gui.py in operation