Electronic Hourglass + Ai-M62-CBS + 8×8 LED Matrix

1. Project Overview

This project uses the Ai-M62-CBS module paired with two 8×8 LED matrix displays and a mercury switch for gravity detection to create an electronic hourglass. The shell is 3D-printed to provide a compact, appropriately sized enclosure. Combined with a lithium battery and a charge/discharge module, the hourglass can be used portably without needing to be plugged in.

2. Hardware Description

The hardware consists mainly of the Ai-M62-CBS development board, two MAX7219 matrix modules, a mercury switch, a charging/discharging module, and a lithium battery. Details are as follows:

2.1 Main Control Board

The main controller used is the Ai-M62-CBS development board. Its size is extremely compact — even smaller than a typical USB-TTL adapter. See image below for reference.

2.2 MAX7219 8×8 LED Matrix

The MAX7219 chip includes a BCD decoder, multiplexer scan controller, segment and digit drivers, and 8×8 static RAM. A single resistor sets the current for all LEDs. Communication with a microcontroller requires only three wires. Each digit can be BCD decoded or raw input, with support for shutdown mode, brightness control, scan limit (1–8 digits), and display test mode.

2.3 Mercury Switch

Also known as a tilt switch, the mercury switch contains a small drop of mercury inside a sealed container (typically vacuum or filled with inert gas) with internal electrodes. With the rise of affordable accelerometers, this component is less common today.

In this project, the mercury switch detects the orientation of the hourglass, determining the flow direction of the sand animation.

 

3. Code Implementation

The two matrix modules are connected to the M62 board as follows:

#define Max7219_pinDIN          GPIO_PIN_10

#define Max7219_pinCS           GPIO_PIN_11

#define Max7219_pinCLK          GPIO_PIN_12

 

#define Max7219_2_pinDIN        GPIO_PIN_28

#define Max7219_2_pinCS         GPIO_PIN_29

#define Max7219_2_pinCLK        GPIO_PIN_30

In the main function, a timer is enabled, with each cycle representing one step in the hourglass sand animation.

Example code snippet:

void timer0_isr(int irq, void *arg){

    bool status = bflb_timer_get_compint_status(timer0, TIMER_COMP_ID_0);

    if (status) {

        bflb_timer_compint_clear(timer0, TIMER_COMP_ID_0);

        printf("timer0 comp0 trigger\r\n");

        step();

    }

}

Timer configuration (1 second per cycle):

void timer_init(){

    struct bflb_timer_config_s cfg0;

    cfg0.counter_mode = TIMER_COUNTER_MODE_PROLOAD;

    cfg0.clock_source = TIMER_CLKSRC_XTAL;

    cfg0.clock_div = 39; // 1 MHz

    cfg0.trigger_comp_id = TIMER_COMP_ID_0;

    cfg0.comp0_val = 1000000;

    cfg0.comp1_val = 1500000;

    cfg0.comp2_val = 2500000;

    cfg0.preload_val = 0;

 

    timer0 = bflb_device_get_by_name("timer0");

 

    bflb_timer_init(timer0, &cfg0);

    bflb_irq_attach(timer0->irq_num, timer0_isr, NULL);

    bflb_irq_enable(timer0->irq_num);

    bflb_timer_start(timer0);

}

4. Enclosure Design

The case is based on a design from MakerWorld. The front panel houses the two LED matrix displays, with a Type-C charging port on the side. The back panel...

Read more »