element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Embedded Forum Who still uses Firmata (and the Johnny Five library)
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 9 replies
  • Subscribers 499 subscribers
  • Views 291 views
  • Users 0 members are here
  • firmata
  • hil testing
  • johnny-five
  • arduino
Related

Who still uses Firmata (and the Johnny Five library)

BigG
BigG 8 days ago

When "Johnny-Five" was mentioned in a comment in this forum post, I being the IoT geek that I am, first thought of this Johnny-Five, and not the celebrity robot. Sad, I know.

I then remembered from way back that my Johnny-Five worked with this Arduino library.

So, who still uses Firmata? If so, have you ever tried it outside of Arduino? What fun/interesting things have you done with Firmata/Johnny-Five?

I have just rediscovered it's usefulness for hardware in the loop (HIL) testing together with my ADALM2000 (as logic analyser) and the libm2k Python library when using Codex and a custom MCP.

image

  • Sign in to reply
  • Cancel

Top Replies

  • meera_hussien
    meera_hussien 7 days ago +1
    I remember using Firmata to control micro:BIT using the Arduino IDE
Parents
  • meera_hussien
    meera_hussien 7 days ago

    I remember using Firmata to control micro:BIT using the Arduino IDE

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • BigG
    BigG 7 days ago in reply to meera_hussien

    I shamelessly orchestrated multiple AI agents to generate this Firmata demo code for my BBC micro:bit.

    Thank you Gemini, for creating this useful infographic of how it works:

    image

    As it illustrates, you require a Firmata Client to send commands to the host firmware. Thankfully there's a bunch available via https://github.com/firmata/protocol#firmata-client-libraries

    I used Codex to generate my host firmware. I used Zephyr RTOS! Behind the scenes I have the workings of an "out-of-tree" Firmata library for Zephyr (work-in-progress).

    /*
     * SPDX-FileCopyrightText: Copyright (c) 2026 Firmata Zephyr Contributors
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <errno.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <string.h>
    
    #include <zephyr/device.h>
    #include <zephyr/drivers/display.h>
    #include <zephyr/drivers/uart.h>
    #include <zephyr/kernel.h>
    #include <zephyr/sys/util.h>
    
    #include <firmata/engine.h>
    #include <firmata/session.h>
    
    #define FIRMATA_UART_NODE DT_NODELABEL(uart0)
    #define DISPLAY_NODE      DT_CHOSEN(zephyr_display)
    
    #define FIRMWARE_NAME          "ZephyrFirmata BBC micro:bit"
    #define FIRMWARE_MAJOR_VERSION 0U
    #define FIRMWARE_MINOR_VERSION 7U
    
    #define DISPLAY_WIDTH  5U
    #define DISPLAY_HEIGHT 5U
    #define PIXEL_COUNT    (DISPLAY_WIDTH * DISPLAY_HEIGHT)
    #define PORT_COUNT     DIV_ROUND_UP(PIXEL_COUNT, 8U)
    
    #define SYSEX_BUFFER_SIZE    128U
    #define RESPONSE_BUFFER_SIZE 128U
    
    static const struct device *const firmata_uart = DEVICE_DT_GET(FIRMATA_UART_NODE);
    static const struct device *const display = DEVICE_DT_GET(DISPLAY_NODE);
    
    static struct firmata_engine engine;
    static struct firmata_session session;
    static uint8_t sysex_buffer[SYSEX_BUFFER_SIZE];
    static uint8_t response_buffer[RESPONSE_BUFFER_SIZE];
    static uint8_t display_buffer[DISPLAY_HEIGHT];
    static struct firmata_pin_descriptor pin_descriptors[PIXEL_COUNT];
    
    static const struct display_buffer_descriptor display_descriptor = {
    	.buf_size = sizeof(display_buffer),
    	.width = DISPLAY_WIDTH,
    	.height = DISPLAY_HEIGHT,
    	.pitch = 8U,
    };
    
    static const struct firmata_pin_capability pixel_capabilities[] = {
    	{
    		.mode = FIRMATA_PIN_MODE_OUTPUT,
    		.resolution = 1U,
    	},
    };
    
    static int write_display(const uint8_t *buffer)
    {
    	int ret = display_write(display, 0U, 0U, &display_descriptor, buffer);
    
    	if (ret == 0) {
    		memcpy(display_buffer, buffer, sizeof(display_buffer));
    	}
    	return ret;
    }
    
    static int pixel_get_pin_state(void *context, uint8_t pin, struct firmata_pin_state *state)
    {
    	ARG_UNUSED(context);
    
    	if ((state == NULL) || (pin >= PIXEL_COUNT)) {
    		return -EINVAL;
    	}
    
    	state->mode = FIRMATA_PIN_MODE_OUTPUT;
    	state->state = (display_buffer[pin / DISPLAY_WIDTH] >> (pin % DISPLAY_WIDTH)) & 1U;
    	return 0;
    }
    
    static int pixel_set_pin_mode(void *context, uint8_t pin, uint8_t mode)
    {
    	ARG_UNUSED(context);
    
    	return ((pin < PIXEL_COUNT) && (mode == FIRMATA_PIN_MODE_OUTPUT)) ? 0 : -ENOTSUP;
    }
    
    static int pixel_set_value(void *context, uint8_t pin, uint32_t value)
    {
    	uint8_t next_buffer[DISPLAY_HEIGHT];
    
    	ARG_UNUSED(context);
    
    	if ((pin >= PIXEL_COUNT) || (value > 1U)) {
    		return -EINVAL;
    	}
    
    	memcpy(next_buffer, display_buffer, sizeof(next_buffer));
    	WRITE_BIT(next_buffer[pin / DISPLAY_WIDTH], pin % DISPLAY_WIDTH, value != 0U);
    	return write_display(next_buffer);
    }
    
    static int pixel_set_port(void *context, uint8_t port, uint8_t value)
    {
    	uint8_t next_buffer[DISPLAY_HEIGHT];
    	uint8_t first_pin = port * 8U;
    
    	ARG_UNUSED(context);
    
    	if (port >= PORT_COUNT) {
    		return -EINVAL;
    	}
    
    	memcpy(next_buffer, display_buffer, sizeof(next_buffer));
    	for (uint8_t offset = 0U; (offset < 8U) && ((first_pin + offset) < PIXEL_COUNT); offset++) {
    		uint8_t pin = first_pin + offset;
    
    		WRITE_BIT(next_buffer[pin / DISPLAY_WIDTH], pin % DISPLAY_WIDTH,
    			  (value & BIT(offset)) != 0U);
    	}
    
    	return write_display(next_buffer);
    }
    
    static int pixel_get_port(void *context, uint8_t port, uint8_t *value)
    {
    	uint8_t first_pin = port * 8U;
    	uint8_t port_value = 0U;
    
    	ARG_UNUSED(context);
    
    	if ((value == NULL) || (port >= PORT_COUNT)) {
    		return -EINVAL;
    	}
    
    	for (uint8_t offset = 0U; (offset < 8U) && ((first_pin + offset) < PIXEL_COUNT); offset++) {
    		uint8_t pin = first_pin + offset;
    
    		WRITE_BIT(port_value, offset,
    			  (display_buffer[pin / DISPLAY_WIDTH] & BIT(pin % DISPLAY_WIDTH)) != 0U);
    	}
    
    	*value = port_value;
    	return 0;
    }
    
    static int pixel_set_reporting(void *context, uint8_t port, bool enabled)
    {
    	ARG_UNUSED(context);
    	ARG_UNUSED(enabled);
    
    	return (port < PORT_COUNT) ? 0 : -EINVAL;
    }
    
    static int pixel_system_reset(void *context)
    {
    	uint8_t blank[DISPLAY_HEIGHT] = {0};
    
    	ARG_UNUSED(context);
    
    	return write_display(blank);
    }
    
    static const struct firmata_pin_provider pixel_provider = {
    	.pins = pin_descriptors,
    	.pin_count = ARRAY_SIZE(pin_descriptors),
    	.get_pin_state = pixel_get_pin_state,
    	.set_pin_mode = pixel_set_pin_mode,
    	.set_digital_pin_value = pixel_set_value,
    	.set_digital_port_value = pixel_set_port,
    	.get_digital_port_value = pixel_get_port,
    	.set_digital_reporting = pixel_set_reporting,
    	.system_reset = pixel_system_reset,
    };
    
    static int uart_transmit(void *context, const uint8_t *data, size_t length)
    {
    	const struct device *uart = context;
    
    	for (size_t i = 0U; i < length; i++) {
    		uart_poll_out(uart, data[i]);
    	}
    	return 0;
    }
    
    static int initialize_display(void)
    {
    	struct display_capabilities capabilities;
    	uint8_t blank[DISPLAY_HEIGHT] = {0};
    	int ret;
    
    	if (!device_is_ready(display)) {
    		return -ENODEV;
    	}
    
    	display_get_capabilities(display, &capabilities);
    	if ((capabilities.x_resolution != DISPLAY_WIDTH) ||
    	    (capabilities.y_resolution != DISPLAY_HEIGHT) ||
    	    ((capabilities.supported_pixel_formats & PIXEL_FORMAT_MONO01) == 0U)) {
    		return -ENOTSUP;
    	}
    
    	ret = display_set_pixel_format(display, PIXEL_FORMAT_MONO01);
    	if (ret < 0) {
    		return ret;
    	}
    	ret = write_display(blank);
    	if (ret < 0) {
    		return ret;
    	}
    	return display_blanking_off(display);
    }
    
    static int initialize_firmata(void)
    {
    	for (size_t pin = 0U; pin < ARRAY_SIZE(pin_descriptors); pin++) {
    		pin_descriptors[pin].capabilities = pixel_capabilities;
    		pin_descriptors[pin].capability_count = ARRAY_SIZE(pixel_capabilities);
    		pin_descriptors[pin].analog_channel = FIRMATA_DATA_MASK;
    	}
    
    	const struct firmata_engine_config config = {
    		.firmware_name = FIRMWARE_NAME,
    		.firmware_name_length = sizeof(FIRMWARE_NAME) - 1U,
    		.firmware_major = FIRMWARE_MAJOR_VERSION,
    		.firmware_minor = FIRMWARE_MINOR_VERSION,
    		.pin_provider = &pixel_provider,
    	};
    	int ret = firmata_engine_init(&engine, &config);
    
    	if (ret < 0) {
    		return ret;
    	}
    	return firmata_session_init(&session, &engine, sysex_buffer, sizeof(sysex_buffer),
    				    response_buffer, sizeof(response_buffer), uart_transmit,
    				    (void *)firmata_uart);
    }
    
    static void indicate_fatal_error(void)
    {
    	uint8_t center_on[DISPLAY_HEIGHT] = {0U, 0U, BIT(2), 0U, 0U};
    	uint8_t blank[DISPLAY_HEIGHT] = {0};
    
    	while (true) {
    		if (device_is_ready(display)) {
    			(void)display_write(display, 0U, 0U, &display_descriptor, center_on);
    			(void)display_blanking_off(display);
    		}
    		k_msleep(150U);
    		if (device_is_ready(display)) {
    			(void)display_write(display, 0U, 0U, &display_descriptor, blank);
    		}
    		k_msleep(150U);
    	}
    }
    
    int main(void)
    {
    	if (!device_is_ready(firmata_uart) || (initialize_display() < 0) ||
    	    (initialize_firmata() < 0)) {
    		indicate_fatal_error();
    	}
    
    	while (true) {
    		unsigned char byte;
    
    		if (uart_poll_in(firmata_uart, &byte) == 0) {
    			(void)firmata_session_feed(&session, byte);
    		} else {
    			k_msleep(1U);
    		}
    	}
    }
    

    This then interacts with the client. In my case, I used Python:

    #!/usr/bin/env python3
    # SPDX-License-Identifier: Apache-2.0
    
    """Display a heart on the Firmata-controlled BBC micro:bit LED matrix."""
    
    import argparse
    import sys
    import time
    from pathlib import Path
    
    # Make the workspace's reusable Firmata client importable when this file is
    # launched directly, without requiring the caller to set PYTHONPATH.
    TOOLS_DIRECTORY = Path(__file__).resolve().parents[2] / "custom" / "firmata" / "tools"
    sys.path.insert(0, str(TOOLS_DIRECTORY))
    
    from firmata_test import FirmataClient, SerialTransport  # noqa: E402
    from firmata_test.protocol import PIN_MODE_OUTPUT  # noqa: E402
    
    PIXEL_COUNT = 25
    HEART_PINS = frozenset(
        {
            1,
            3,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            16,
            17,
            18,
            22,
        }
    )
    
    
    def parse_arguments() -> argparse.Namespace:
        parser = argparse.ArgumentParser(description=__doc__)
        parser.add_argument(
            "port",
            nargs="?",
            default="/dev/ttyACM0",
            help="Firmata serial device (default: /dev/ttyACM0)",
        )
        parser.add_argument(
            "--seconds",
            type=float,
            default=5.0,
            help="how long to display the heart (default: 5)",
        )
        arguments = parser.parse_args()
        if arguments.seconds < 0.0:
            parser.error("--seconds must not be negative")
        return arguments
    
    
    def main() -> int:
        arguments = parse_arguments()
    
        with SerialTransport(arguments.port, 57600) as transport:
            client = FirmataClient(transport)
            firmware = client.firmware()
            capabilities = client.capabilities()
    
            if firmware.name != "ZephyrFirmata BBC micro:bit":
                raise RuntimeError(f"unexpected Firmata firmware: {firmware.name!r}")
            if len(capabilities) != PIXEL_COUNT:
                raise RuntimeError(
                    f"expected {PIXEL_COUNT} Firmata pixels, found {len(capabilities)}"
                )
            if any((PIN_MODE_OUTPUT, 1) not in modes for modes in capabilities):
                raise RuntimeError("not every Firmata pixel supports digital output")
    
            client.system_reset()
            try:
                for pin in range(PIXEL_COUNT):
                    client.set_pin_mode(pin, PIN_MODE_OUTPUT)
                    client.set_digital_pin(pin, int(pin in HEART_PINS))
    
                print(
                    f"Displaying a heart on {firmware.name} "
                    f"{firmware.version[0]}.{firmware.version[1]} for "
                    f"{arguments.seconds:g} seconds"
                )
                time.sleep(arguments.seconds)
            finally:
                client.system_reset()
    
        print("Display cleared")
        return 0
    
    
    if __name__ == "__main__":
        try:
            raise SystemExit(main())
        except KeyboardInterrupt:
            print("\nInterrupted; display cleared", file=sys.stderr)
            raise SystemExit(130) from None
        except (OSError, RuntimeError, TimeoutError, ValueError) as error:
            print(f"Heart demo failed: {error}", file=sys.stderr)
            raise SystemExit(1) from None

    And here is the result:

    image

    Visually this is great fun, but the real power is in the actual testing of GPIO state.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • BigG
    BigG 7 days ago in reply to meera_hussien

    I shamelessly orchestrated multiple AI agents to generate this Firmata demo code for my BBC micro:bit.

    Thank you Gemini, for creating this useful infographic of how it works:

    image

    As it illustrates, you require a Firmata Client to send commands to the host firmware. Thankfully there's a bunch available via https://github.com/firmata/protocol#firmata-client-libraries

    I used Codex to generate my host firmware. I used Zephyr RTOS! Behind the scenes I have the workings of an "out-of-tree" Firmata library for Zephyr (work-in-progress).

    /*
     * SPDX-FileCopyrightText: Copyright (c) 2026 Firmata Zephyr Contributors
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <errno.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <string.h>
    
    #include <zephyr/device.h>
    #include <zephyr/drivers/display.h>
    #include <zephyr/drivers/uart.h>
    #include <zephyr/kernel.h>
    #include <zephyr/sys/util.h>
    
    #include <firmata/engine.h>
    #include <firmata/session.h>
    
    #define FIRMATA_UART_NODE DT_NODELABEL(uart0)
    #define DISPLAY_NODE      DT_CHOSEN(zephyr_display)
    
    #define FIRMWARE_NAME          "ZephyrFirmata BBC micro:bit"
    #define FIRMWARE_MAJOR_VERSION 0U
    #define FIRMWARE_MINOR_VERSION 7U
    
    #define DISPLAY_WIDTH  5U
    #define DISPLAY_HEIGHT 5U
    #define PIXEL_COUNT    (DISPLAY_WIDTH * DISPLAY_HEIGHT)
    #define PORT_COUNT     DIV_ROUND_UP(PIXEL_COUNT, 8U)
    
    #define SYSEX_BUFFER_SIZE    128U
    #define RESPONSE_BUFFER_SIZE 128U
    
    static const struct device *const firmata_uart = DEVICE_DT_GET(FIRMATA_UART_NODE);
    static const struct device *const display = DEVICE_DT_GET(DISPLAY_NODE);
    
    static struct firmata_engine engine;
    static struct firmata_session session;
    static uint8_t sysex_buffer[SYSEX_BUFFER_SIZE];
    static uint8_t response_buffer[RESPONSE_BUFFER_SIZE];
    static uint8_t display_buffer[DISPLAY_HEIGHT];
    static struct firmata_pin_descriptor pin_descriptors[PIXEL_COUNT];
    
    static const struct display_buffer_descriptor display_descriptor = {
    	.buf_size = sizeof(display_buffer),
    	.width = DISPLAY_WIDTH,
    	.height = DISPLAY_HEIGHT,
    	.pitch = 8U,
    };
    
    static const struct firmata_pin_capability pixel_capabilities[] = {
    	{
    		.mode = FIRMATA_PIN_MODE_OUTPUT,
    		.resolution = 1U,
    	},
    };
    
    static int write_display(const uint8_t *buffer)
    {
    	int ret = display_write(display, 0U, 0U, &display_descriptor, buffer);
    
    	if (ret == 0) {
    		memcpy(display_buffer, buffer, sizeof(display_buffer));
    	}
    	return ret;
    }
    
    static int pixel_get_pin_state(void *context, uint8_t pin, struct firmata_pin_state *state)
    {
    	ARG_UNUSED(context);
    
    	if ((state == NULL) || (pin >= PIXEL_COUNT)) {
    		return -EINVAL;
    	}
    
    	state->mode = FIRMATA_PIN_MODE_OUTPUT;
    	state->state = (display_buffer[pin / DISPLAY_WIDTH] >> (pin % DISPLAY_WIDTH)) & 1U;
    	return 0;
    }
    
    static int pixel_set_pin_mode(void *context, uint8_t pin, uint8_t mode)
    {
    	ARG_UNUSED(context);
    
    	return ((pin < PIXEL_COUNT) && (mode == FIRMATA_PIN_MODE_OUTPUT)) ? 0 : -ENOTSUP;
    }
    
    static int pixel_set_value(void *context, uint8_t pin, uint32_t value)
    {
    	uint8_t next_buffer[DISPLAY_HEIGHT];
    
    	ARG_UNUSED(context);
    
    	if ((pin >= PIXEL_COUNT) || (value > 1U)) {
    		return -EINVAL;
    	}
    
    	memcpy(next_buffer, display_buffer, sizeof(next_buffer));
    	WRITE_BIT(next_buffer[pin / DISPLAY_WIDTH], pin % DISPLAY_WIDTH, value != 0U);
    	return write_display(next_buffer);
    }
    
    static int pixel_set_port(void *context, uint8_t port, uint8_t value)
    {
    	uint8_t next_buffer[DISPLAY_HEIGHT];
    	uint8_t first_pin = port * 8U;
    
    	ARG_UNUSED(context);
    
    	if (port >= PORT_COUNT) {
    		return -EINVAL;
    	}
    
    	memcpy(next_buffer, display_buffer, sizeof(next_buffer));
    	for (uint8_t offset = 0U; (offset < 8U) && ((first_pin + offset) < PIXEL_COUNT); offset++) {
    		uint8_t pin = first_pin + offset;
    
    		WRITE_BIT(next_buffer[pin / DISPLAY_WIDTH], pin % DISPLAY_WIDTH,
    			  (value & BIT(offset)) != 0U);
    	}
    
    	return write_display(next_buffer);
    }
    
    static int pixel_get_port(void *context, uint8_t port, uint8_t *value)
    {
    	uint8_t first_pin = port * 8U;
    	uint8_t port_value = 0U;
    
    	ARG_UNUSED(context);
    
    	if ((value == NULL) || (port >= PORT_COUNT)) {
    		return -EINVAL;
    	}
    
    	for (uint8_t offset = 0U; (offset < 8U) && ((first_pin + offset) < PIXEL_COUNT); offset++) {
    		uint8_t pin = first_pin + offset;
    
    		WRITE_BIT(port_value, offset,
    			  (display_buffer[pin / DISPLAY_WIDTH] & BIT(pin % DISPLAY_WIDTH)) != 0U);
    	}
    
    	*value = port_value;
    	return 0;
    }
    
    static int pixel_set_reporting(void *context, uint8_t port, bool enabled)
    {
    	ARG_UNUSED(context);
    	ARG_UNUSED(enabled);
    
    	return (port < PORT_COUNT) ? 0 : -EINVAL;
    }
    
    static int pixel_system_reset(void *context)
    {
    	uint8_t blank[DISPLAY_HEIGHT] = {0};
    
    	ARG_UNUSED(context);
    
    	return write_display(blank);
    }
    
    static const struct firmata_pin_provider pixel_provider = {
    	.pins = pin_descriptors,
    	.pin_count = ARRAY_SIZE(pin_descriptors),
    	.get_pin_state = pixel_get_pin_state,
    	.set_pin_mode = pixel_set_pin_mode,
    	.set_digital_pin_value = pixel_set_value,
    	.set_digital_port_value = pixel_set_port,
    	.get_digital_port_value = pixel_get_port,
    	.set_digital_reporting = pixel_set_reporting,
    	.system_reset = pixel_system_reset,
    };
    
    static int uart_transmit(void *context, const uint8_t *data, size_t length)
    {
    	const struct device *uart = context;
    
    	for (size_t i = 0U; i < length; i++) {
    		uart_poll_out(uart, data[i]);
    	}
    	return 0;
    }
    
    static int initialize_display(void)
    {
    	struct display_capabilities capabilities;
    	uint8_t blank[DISPLAY_HEIGHT] = {0};
    	int ret;
    
    	if (!device_is_ready(display)) {
    		return -ENODEV;
    	}
    
    	display_get_capabilities(display, &capabilities);
    	if ((capabilities.x_resolution != DISPLAY_WIDTH) ||
    	    (capabilities.y_resolution != DISPLAY_HEIGHT) ||
    	    ((capabilities.supported_pixel_formats & PIXEL_FORMAT_MONO01) == 0U)) {
    		return -ENOTSUP;
    	}
    
    	ret = display_set_pixel_format(display, PIXEL_FORMAT_MONO01);
    	if (ret < 0) {
    		return ret;
    	}
    	ret = write_display(blank);
    	if (ret < 0) {
    		return ret;
    	}
    	return display_blanking_off(display);
    }
    
    static int initialize_firmata(void)
    {
    	for (size_t pin = 0U; pin < ARRAY_SIZE(pin_descriptors); pin++) {
    		pin_descriptors[pin].capabilities = pixel_capabilities;
    		pin_descriptors[pin].capability_count = ARRAY_SIZE(pixel_capabilities);
    		pin_descriptors[pin].analog_channel = FIRMATA_DATA_MASK;
    	}
    
    	const struct firmata_engine_config config = {
    		.firmware_name = FIRMWARE_NAME,
    		.firmware_name_length = sizeof(FIRMWARE_NAME) - 1U,
    		.firmware_major = FIRMWARE_MAJOR_VERSION,
    		.firmware_minor = FIRMWARE_MINOR_VERSION,
    		.pin_provider = &pixel_provider,
    	};
    	int ret = firmata_engine_init(&engine, &config);
    
    	if (ret < 0) {
    		return ret;
    	}
    	return firmata_session_init(&session, &engine, sysex_buffer, sizeof(sysex_buffer),
    				    response_buffer, sizeof(response_buffer), uart_transmit,
    				    (void *)firmata_uart);
    }
    
    static void indicate_fatal_error(void)
    {
    	uint8_t center_on[DISPLAY_HEIGHT] = {0U, 0U, BIT(2), 0U, 0U};
    	uint8_t blank[DISPLAY_HEIGHT] = {0};
    
    	while (true) {
    		if (device_is_ready(display)) {
    			(void)display_write(display, 0U, 0U, &display_descriptor, center_on);
    			(void)display_blanking_off(display);
    		}
    		k_msleep(150U);
    		if (device_is_ready(display)) {
    			(void)display_write(display, 0U, 0U, &display_descriptor, blank);
    		}
    		k_msleep(150U);
    	}
    }
    
    int main(void)
    {
    	if (!device_is_ready(firmata_uart) || (initialize_display() < 0) ||
    	    (initialize_firmata() < 0)) {
    		indicate_fatal_error();
    	}
    
    	while (true) {
    		unsigned char byte;
    
    		if (uart_poll_in(firmata_uart, &byte) == 0) {
    			(void)firmata_session_feed(&session, byte);
    		} else {
    			k_msleep(1U);
    		}
    	}
    }
    

    This then interacts with the client. In my case, I used Python:

    #!/usr/bin/env python3
    # SPDX-License-Identifier: Apache-2.0
    
    """Display a heart on the Firmata-controlled BBC micro:bit LED matrix."""
    
    import argparse
    import sys
    import time
    from pathlib import Path
    
    # Make the workspace's reusable Firmata client importable when this file is
    # launched directly, without requiring the caller to set PYTHONPATH.
    TOOLS_DIRECTORY = Path(__file__).resolve().parents[2] / "custom" / "firmata" / "tools"
    sys.path.insert(0, str(TOOLS_DIRECTORY))
    
    from firmata_test import FirmataClient, SerialTransport  # noqa: E402
    from firmata_test.protocol import PIN_MODE_OUTPUT  # noqa: E402
    
    PIXEL_COUNT = 25
    HEART_PINS = frozenset(
        {
            1,
            3,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            16,
            17,
            18,
            22,
        }
    )
    
    
    def parse_arguments() -> argparse.Namespace:
        parser = argparse.ArgumentParser(description=__doc__)
        parser.add_argument(
            "port",
            nargs="?",
            default="/dev/ttyACM0",
            help="Firmata serial device (default: /dev/ttyACM0)",
        )
        parser.add_argument(
            "--seconds",
            type=float,
            default=5.0,
            help="how long to display the heart (default: 5)",
        )
        arguments = parser.parse_args()
        if arguments.seconds < 0.0:
            parser.error("--seconds must not be negative")
        return arguments
    
    
    def main() -> int:
        arguments = parse_arguments()
    
        with SerialTransport(arguments.port, 57600) as transport:
            client = FirmataClient(transport)
            firmware = client.firmware()
            capabilities = client.capabilities()
    
            if firmware.name != "ZephyrFirmata BBC micro:bit":
                raise RuntimeError(f"unexpected Firmata firmware: {firmware.name!r}")
            if len(capabilities) != PIXEL_COUNT:
                raise RuntimeError(
                    f"expected {PIXEL_COUNT} Firmata pixels, found {len(capabilities)}"
                )
            if any((PIN_MODE_OUTPUT, 1) not in modes for modes in capabilities):
                raise RuntimeError("not every Firmata pixel supports digital output")
    
            client.system_reset()
            try:
                for pin in range(PIXEL_COUNT):
                    client.set_pin_mode(pin, PIN_MODE_OUTPUT)
                    client.set_digital_pin(pin, int(pin in HEART_PINS))
    
                print(
                    f"Displaying a heart on {firmware.name} "
                    f"{firmware.version[0]}.{firmware.version[1]} for "
                    f"{arguments.seconds:g} seconds"
                )
                time.sleep(arguments.seconds)
            finally:
                client.system_reset()
    
        print("Display cleared")
        return 0
    
    
    if __name__ == "__main__":
        try:
            raise SystemExit(main())
        except KeyboardInterrupt:
            print("\nInterrupted; display cleared", file=sys.stderr)
            raise SystemExit(130) from None
        except (OSError, RuntimeError, TimeoutError, ValueError) as error:
            print(f"Heart demo failed: {error}", file=sys.stderr)
            raise SystemExit(1) from None

    And here is the result:

    image

    Visually this is great fun, but the real power is in the actual testing of GPIO state.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • meera_hussien
    meera_hussien 7 days ago in reply to BigG

    I remember doing some simple programming. But does the program on the micro:bit stays after powering off?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • BigG
    BigG 6 days ago in reply to meera_hussien

    The instruction + data to create the heart does not stay on the micro:bit after powering off, only the Firmata firmware to interpret the instructions and to manage the data transferred between external device (e.g. PC) and the micro:bit. It's really just manual hardware control software for embedded devices.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube