How does a Python function work to control other hardware?

Atreyo Bhattacharjee

Feb 7, 2017
I'm sorry if I'm misusing the word package. I have little experience in Python, and am still learning.

So in my computer science class, we are using these robots called Finch to learn the very basics of robotics. So using the Finch package, we can make the robot do things with simple function calls. I was curious to how those functions, which would be written in Python, be able to interface with a piece of hardware that the language is not naively created to be able to communicate with? What makes that package so special, that it allows Python to communicate with the robot?

I guess in a broader sense, I'm asking how any function can have code to control and interface with any hardware the language is not naively designed to.

As I said, I'm really new to Python and programming in general (around 6 months of experience), so please don't rip at me for being a noob.

Here's the link to the robot we are using:


One way to consider how the functions work is to look at it as if you had some controller or joystick in your hand connected to the robot.

You might push one button to make the robot go forward. Another button to go backward. Likewise other buttons to turn left or turn right. Or combinations of buttons for more complex actions. Or a sequence of actions.

All the controller buttons do is complete some electrical circuit that makes the applicable robot motors turn on or off and move (step) in some incremental direction.

So when you code a Python function that function is written to provide the necessary electrical signals to the robot just as if you were pushing buttons on a controller.

What your Python function must do is convert your desired action, e.g., "turn right" into robot speak.

Something to the effect of "Button #2 has been pushed - motor X do 90 little steps clockwise".

The following links should help:

And specifically with respect to DC motors:

The value in a function is simply that you only need to write the function (which may consist of multiple lines of code) and then CALL that function whenever you need to have your robot make a right turn.

Versus writing all of the same lines of code over and over again whenever a right turn may be needed.

And a single function is easier to debug and change. You go to the function (one place) and fix or change as necessary. Instead of having to go to any number of lines in your overall program.

Very good that you are interested in the details and the mechanics of it all. Keep learning.
In addition to excellent @Ralston_18 answer: Your robot is connected over USB to the computer executing Python code. So, for the computer, that robot appears as some device Python know how to talk to. What happens is that somewhere in the Finch package there is "translation" between function calls, and actual commands sent to the robot.

Atreyo Bhattacharjee

Feb 7, 2017

So does the package call on the devices driver or something to that effect? What exactly "bridges" native Python code, to something the Finch can understand?



Consider PCL. Printer Control Language.

Printers are designed via their circuit boards, chips, and motors to do certain things when some string of 0's and 1's are presented via the input port.

Here is a link showing many of the PCL commands:

I believe you will immediately note the similarity to commands being sent to a robot. Especially with respect to positioning.

You can use a programming language to send commands to a printer (or for even more fun, a large scale plotter).

Key is that the native command; "Ec &a#R" (Move to Row) causes the printhead to move. Or a robot to move.

The important part is that the printer understands that "Ec &a#R" is an instruction to the printer and not a character string to print on paper or on monitor as you are reading these characters.

For the most part, command strings to printers, robots, and other devices are preceded by some special or otherwise identifying set of characters to indicate command versus "print'.

What has to happen then is your Python functions needs some sort of interpreter or lookup table to take the Python function for "turn right" and convert the command to Finch robot speak which might use something such as "Ec &a#P Rotation # degrees".

Or your function may just include the character string needed to tell the printer/robot that here comes a command and the command string for "turn right 90 degrees" is presented.

(Note the string I am using as an example is actually for landscape/portrait manipulations.)

So what Finch has done is created a robot that recognizes certain strings of characters, which actually are unique combinations of 0's and 1's being sent to it.

What Python does for you is to simplify the process. Your function may literally be coded as "Turn right".

And an interpreter or look up table takes those "Turn right" characters, converts them to "Ec &a#90"
Rotation # degrees" and communicates that string to the robot via the USB cable per Alabalcho's post.

Basically what does the bridging work is indeed a driver. Could be hardcoded, firmware, or just a look up table.

Sphero robots use much the same process. You can program them with functions via drag and drop development tools. Or command line coding if you chose to do so.

No intent to overwhelm or confuse you but check the following link:

Much the same and you will quickly begin to understand how it all comes about.

Have fun.

Thread starter Similar threads Forum Replies Date
A Apps General Discussion 5
T Apps General Discussion 5
A Apps General Discussion 4
A Apps General Discussion 1
R Apps General Discussion 1
M Apps General Discussion 1
A Apps General Discussion 1
H Apps General Discussion 1
G Apps General Discussion 5
M Apps General Discussion 11
A Apps General Discussion 6
G Apps General Discussion 19
C Apps General Discussion 1
Jaakey Apps General Discussion 2
T Apps General Discussion 3
B Apps General Discussion 4
R Apps General Discussion 1
D Apps General Discussion 6
G Apps General Discussion 3
shoob0x Apps General Discussion 3