Category Archives: Robotics

Digital Input Events – Practical Python Tutorial Series

Welcome to the Practical Python Tutorial Series

Welcome to the first tutorial in our Practical Python series. Throughout this series, you’ll learn how to write Python programs that interact with real electronic hardware using the Elecrow All-in-One Starter Kit for Arduino and the Robo-Tx API.

Unlike traditional Arduino development, which typically requires programming the microcontroller in C++, Robo-Tx allows you to write familiar Python code on your computer while controlling sensors, LEDs, buzzers, motors and other hardware connected to the Arduino. This makes it an excellent platform for developing practical programming skills while seeing your code produce immediate, real-world results.

Before You Begin

If you own an Elecrow All-in-One Starter Kit for Arduino, begin by following the setup instructions provided in the GitHub repository. These instructions explain how to prepare both the Arduino hardware and your Python development environment before running any of the examples. The Python example used in this tutorial (1-button.py) can be found at the same repository.

Don’t have the hardware? You can still learn the Robo-Tx API and Python concepts by enrolling on the Python Programming: Robotics Foundation Course, where students learn at their own pace and receive supervised tuition with remote access to the hardware.

Tutorial Objective

In this tutorial you will learn how to:

  • connect to the Robo-Tx firmware running on the Arduino
  • respond to digital input events generated by button interaction
  • use a background thread alongside your main program
  • safely disconnect from the hardware when the program finishes

Although this example uses the green push button on the All-in-One Starter Kit, exactly the same programming technique can later be applied to:

  • collision detection microswitches
  • limit switches
  • infrared proximity sensors
  • touch sensors
  • other digital input devices

Understanding events is therefore an important foundation for robotics programming.

Understanding the Program

The complete example is surprisingly small, but introduces several important programming concepts.

1. Importing the Required Modules

import threading
import time
from app_config import *

The standard Python modules provide:

  • threading used here for monitoring the keyboard while the main program continues running.
  • time for adding a short delay inside the loop.

The final import:

from app_config import *

is supplied with the Robo-Tx examples. This file performs the necessary configuration of the Robo-Tx API and stores settings such as the serial port used to communicate with the Arduino. Keeping these settings in one location means the example programs remain clean and easy to understand.

2. Connecting to the Hardware

all_in_one_kit = RobotIO(serial_port)
all_in_one_kit.Connect()

The RobotIO object represents your connection to the Arduino in the All-in-One Starter kit. Calling Connect() establishes communication with the Robo-Tx firmware uploaded to the Arduino. Once connected, Python can immediately begin reading sensors and controlling outputs.

3. Running Two Tasks at Once

One interesting feature of this example is that two things happen simultaneously. First, a separate thread waits for the user to press Enter.

detectEnterKey = threading.Thread(target=input)
detectEnterKey.start()

Meanwhile, the main thread runs in a while loop and continues communicating with the hardware. This introduces students to one of the most useful ideas in programming—performing multiple tasks concurrently. Most of the examples in the Python tutorials use this technique as an OS independent way of exiting the main while loop and safely terminating the program when the Enter key is pressed.

4. Monitoring Button Events

The heart of the program is:

button_event = all_in_one_kit.Digital.GetInputEvent()

Instead of continuously asking the Arduino in the All-in-One Starter kit “Is the button pressed?”, the Robo-Tx API reports events. Events describe what has happened rather than simply the button’s current state. This allows programs to respond much more intelligently.

The API can generate four different events.

EventDescriptionExample Use
Input.BUTTON_1_PRESSEDGenerated once when the button is first pushed down.Switching on an LED or beginning a timer.
Input.BUTTON_1_SUSTAINEDGenerated repeatedly after an initial delay and while the button remains pressed.A continuous action such as Increasing motor speed or scrolling through a menu.
Input.BUTTON_1_RELEASEDGenerated after a short button press and release.Use this event to confirm an action when Input.BUTTON_1_SUSTAINED is also responded to.
Input.BUTTON_1_SUSTAIN_RELEASEDGenerated when the button has been released after being held for a while.Stop a previously initiated continuous action.
Description of digital input events generated by the Robo-Tx API

5. Preventing Excessive CPU Usage

Inside the while loop is a small delay.

time.sleep(0.05)

Without this pause the program would continuously poll the hardware thousands of times every second. Adding a 50 ms delay keeps the program responsive while greatly reducing processor usage.

6. Closing the Connection

The example finishes with:

finally:
    all_in_one_kit.Close()

Using a finally block guarantees that communication with the Arduino is closed correctly—even if the program encounters an unexpected error. This is considered good programming practice whenever external hardware is involved.

Programming Concepts Covered

This single example introduces a surprising number of Python concepts:

  • importing modules
  • creating objects
  • calling methods
  • variables
  • loops
  • conditional statements (if, elif)
  • multithreading
  • event-driven programming
  • exception-safe resource handling
  • communicating with external hardware

These are all transferable programming skills that will be used repeatedly throughout the remainder of this tutorial series.

Looking Ahead

This example introduced one of the most important concepts in robotics software development—responding to digital input events generated by sensors.

As the series progresses you’ll work with a variety of sensors and actuators of the Elecrow All-in-One Starter Kit for Arduino, and increasingly sophisticated robotics programming techniques using the Robo-Tx API.

By the end of the series you’ll have developed not only stronger Python programming skills, but also a solid understanding of event-driven software, hardware interfacing and the principles that underpin modern robotics systems.