Skip to content
Learning Site
Home

About

  • About Robotical
  • Case Studies
  • Meet the Team
  • Blog
  • Contact Us

Our Products

  • Marty the Robot
  • Axiom
  • Cog

Teach

  • Lessons & Resources
  • Marty for Educators
  • Funding & Grants
  • Professional Development
  • Why Teach STEM?
  • Coding Environments
  • Student Led Learning

Let's Code

  • Marty the Robot
  • Code Online
  • Marty Apple App
  • Marty Android App
  • Update your Marty
  • Cog
  • Code Online
  • Cog iOS App
  • Cog Android App
  • Axiom
  • Axiom Science

Support

  • Marty Get Started
  • Cog Quick Start Guide
  • Axiom Science Quick Start Guide
  • Support & FAQs
  • Marty V2 User Guides
  • Marty V1 Resources
  • Request Support
  • Request Top-Up Training

Shop

  • Marty the Robot and Physical Computing
  • Class Packs
  • Add Ons & Extras
  • Account
  • Shipping, Returns & Cancellations
  • Distributors
HomeLessonsUnits of workCurriculum standardsTutorials

Information

  • Lessons & Resources
  • Knowledge Base & User Guides
  • About Us
  • Shipping, Returns & Cancellation
  • Privacy Policy
  • Learning site privacy
  • Terms of Service
  • Partner Hub

Support & Help

  • Support & Help Center
  • Getting Started with Marty V2
  • Getting Started with Marty V1

Learning, Resources & Guides

  • Marty V2 Lessons Site
  • Marty V1 Lessons Site

Contact Us in the UK

Stanhope House,12 Stanhope Place,Edinburgh,United Kingdom,EH12 5HH
(+44) 0131 564 0922
hello@robotical.io
Contact Us

Contact Us in the US

2810N Church St, PMB 61694,Wilmington,Delaware,USA19802-4447
(+1) 833 886 1661
hello@robotical.io
Contact Us

Why not join our mailing list?

For News, New Products and Special Offers

Join mailing list

Copyright 2016-2026 © Robotical Ltd

All lessons
Marty app
CurrentPrevious

4.07 Building a Python Project for Marty on a Raspberry Pi

Let's extend python to make use of other libraries. What other Python resources can we tap into to use with Marty. Why not control Marty with the keyboard?

4.07 Building a Python Project for Marty on a Raspberry Pi
Estimated time
90 minutes
Difficulty
Advanced
Products
Marty V2

On this page

  1. Lesson overview
  2. Objectives & resources
  3. Key vocabulary
  4. Lesson plan
  5. Extensions and challenges
  6. Additional reading
  7. Curriculum standards

Extensions and challenges

Extend

Have learners explore the possibility of connecting a wireless keyboard to the Raspberry Pi. Following the creation of the code, via the headless setup, learners should be able to take Marty for a walk without any visual feedback on key presses. Click here for the procedure to follow for setting up a wireless keyboard with a Raspberry Pi.

Support

Have the main commands for use with the remote control - keysym(), bind_all(), mainloop() and Tk() - along with the default parameters available for learners. Additionally, if needed, share the first two steps code with learners, here, or in the resources section of the lesson.

First Steps for a RemoteText · 934 B

Additional reading

Raspberry Pi setup with Marty

Setting up the python environment, for Marty

Programming in Python

Marty Uses a Raspberry Pi in Python

Tkinter documentation - a list of common commands used by Tkinter

Bind command - the code uses bind_all but the description for bind is better

keysym command - a list of the keys that can be used for Tkinter

mainloop command - a description of the mainloop function

Tk command - a question and description of the Tk function

Curriculum standards

22
Choose your curriculum to see the relevant standards.

22 of 22 linked standards shown.

Australian CurriculumAustralia2
Curriculum areaLinked standards
Digital Technologies, Design & technologies: Digital Technologies

Select a code to read the full standard.

Digital Technologies, Design & technologies: Digital Technologies

  • ACTDIP039Design the user experience of a digital system by evaluating alternative designs against criteria including functionality, accessibility, usability, and aesthetics
  • ACTDIP041Implement modular programs, applying selected algorithms and data structures including using an object-oriented programming language
CSTA Education StandardsUSA2
Curriculum areaLinked standards
General standards

Select a code to read the full standard.

  • 3A-AP-16Design and iteratively develop computational artifacts for practical intent, personal expression, or to address a societal issue by using events to initiate instructions.
  • 3A-AP-18Create artifacts by using procedures within a program, combinations of data and procedures, or independent but interrelated programs.
Curriculum for ExcellenceScotland15
Curriculum areaLinked standards
Health and Wellbeing: Mental, Emotional, Social and Physical Wellbeing
Literacy & English: Listening and Talking
Literacy & English: Reading
Literacy & English: Writing
Technologies: Computing Science

Select a code to read the full standard.

Health and Wellbeing: Mental, Emotional, Social and Physical Wellbeing

  • HWB 0-10a / HWB 1-10a / HWB 2-10a / HWB 3-10a / HWB 4-10aSocial wellbeing
  • HWB 0-11a / HWB 1-11a / HWB 2-11a / HWB 3-11a / HWB 4-11Social wellbeing
  • HWB 0-14a / HWB 1-14a / HWB 2-14a / HWB 3-14a / HWB 4-14aSocial wellbeing

Literacy & English: Listening and Talking

  • LIT 4-02aTools for listening and talking
  • LIT 3-05a / LIT 4-05aFinding and using information
  • LIT 4-09aCreating texts

Literacy & English: Reading

  • LIT 4-13aTools for reading
  • LIT 3-15a / LIT 4-15aFinding and using information

Literacy & English: Writing

  • LIT 3-20a / LIT 4-20aEnjoyment and choice
  • LIT 4-21aTools for writing
  • LIT 3-22a / LIT 4-22aTools for writing
  • LIT 4-23aTools for writing
  • LIT 4-28aCreating texts

Technologies: Computing Science

  • TCH 4-14bUnderstanding and analysing computing technology
  • TCH 4-15aDesigning, building and testing computing solutions
England's National CurriculumEngland1
Curriculum areaLinked standards
Computing, Design and Technology: Computing

Select a code to read the full standard.

Computing, Design and Technology: Computing

  • ✔️Understand what algorithms are; how they are implemented as programs on digital devices; and that programs execute by following precise and unambiguous instructions
ISTEUSA2
Curriculum areaLinked standards
International Society for Technology in Education (ISTE)

Select a code to read the full standard.

International Society for Technology in Education (ISTE)

  • 1aStudents articulate and set personal learning goals, develop strategies leveraging technology to achieve them and reflect on the learning process itself to improve learning outcomes.
  • 4dStudents exhibit a tolerance for ambiguity, perseverance and the capacity to work with open-ended problems.

Resources

Learner resources

  • Workbook for the Raspberry Pi Remote ControlFile links loading
  • Python Code for Full Remote ControlFile links loading
  • Python Code for First StepsFile links loading

Educator resources

  • Teacher GuideFile links loading
  • Presentation for the Raspberry Pi Remote ControlFile links loading

Lesson overview

This lesson assumes learners have an understanding of Python documentation and how to import functions from libraries.

In this lesson, learners will be accessing the Tynker library to bind keyboard keys to actions, for Marty. This will allow learners to control Marty via a keyboard. There are extension activities that will allow users to connect a wireless keyboard enabling learners to take Marty for a walk, without the need for a device.

Prerequisite knowledge

Experience using Command line syntax, experience with a Raspberry Pi and python, experience accessing importing python libraries

Learning objectives

  • I can write the code in order to control a device using a text-based programming language.
  • I can describe improvements to an algorithm that increase the efficiency.
  • I can explore documentation for a library and implement some methods.

Resources and equipment

  • Marty V2 with a Rasperry Pi installed in the head
  • Device with VNC installed for connection with Raspberry Pi
  • Wireless keyboard for extension activity

Key vocabulary

  • Raspberry Pi
  • library
  • documentation
  • commenting

Lesson plan

Warm Up

Have a freely available web game running on the display. There are a wealth of resources that should be available following this link . If this is not available to access at your school, perhaps displaying on an app on your phone, or asking learners how they control characters on their pc in games like paper.io, minecraft, or classics like snake and pacman (feel free to replace these examples with other titles with which you know your class is familiar). If this is not available, there is a video in the presentation showing a user with a game controlled by a keyboard. You can decide whether you want to display a game, show the video, from the presentation, or begin with the objectives and success criteriam, from the presentation.

Some might suggest that they use the mouse for these games, others might mention a connected controller but it is hoped that some will suggest a keyboard and in particular the arrows keys or 'wasd'. Ask learners to suggest their preference for keyboard, mouse or controllers for interacting with games. Record responses on the board. Share with learners that today they will be coding a keyboard controller for Marty, but first they need to decide on the keys, what each key will do and how to write the code to bind the keys to an action.

Get Learning

Key binding is a very common practice when interacting with computers. Ask any learners if they have ever needed to copy or paste text quickly, in their computer, and what did they do? Did they use the mouse to access the menu? Right click to access the menu? Use ctrl and a letter for a shortcut? It is the key press, in the case mentioned Ctrl-C or Ctrl-V that is an example of a key binding that someone coded into a PC browser or word processor, for example. If you are using another operating system, it might be command-C or command-V.

Developers, who code games or physical devices, often bind keys to perform specific commands. The most commonly used key bindings are wasd or the arrow keys for the four directions and the mouse for turning. Let's witness this in action with Marty, making use of a keyboard to control the movements.

In order to do any work for Marty, we have to import the martypy library, as we have since using Marty and Python. However, Marty does not come with built in functionality for key binding. For that we need to import a second library called Tkinter (the T is silent, when pronouncing). Tkinter is imported much the same way as martypy:

from martypy import Marty
marty = Marty('exp', '/dev/ttyAMA0')
import tkinter as tk
marty.get_ready()

The only difference here is that tkinter is imported in full and is renamed as tk in one line.

Below these lines in the code are where tkinter starts its work: this is where an event object is named, which will handle the input from the keyboard. In the example, the object is called remote_key but this could be anything you or the learners want to use. Below this code, a variable is declared with the task of storing the key that is pressed; key_press = event.keysym.lower() 'keysym' is the part of the command that recognises the key pressed, for the example code, only letters are used but keys like 'space', 'uparrow' or 'downarrow' could also be used; 'lower' converts any keys to lower case to reduce confusion, on the off-chance learners press the shift key or have pressed the caps lock key. Following this code are a series of conditional statements that access Marty and run the associated command. You will notice that some commands make use of the parameters: turning and walking backward, this is because these two commands make use of the walk() command but, by default, walk() commands Marty to walk two steps straight forward.

In addition to the commands, for Marty, I have included a print command for each keypress. The reason for this is that while building, learners get some onscreen feedback that the code works, in case there is not a space available for Marty to walk, yet.

The workbook highlights keysym(), bind_all(), mainloop() and Tk(), which are instrumental in the code functioning properly. If learners research these four commands, they should have everything they need to be able to complete the remote control code.

The example below shows a conditional statement for two steps forward, the code for this is included in the presentation so learners can get started with the conditional statements they will need.

if key_press == 'w':
        print('Marty walks two steps forward')
        marty.walk()

In addition to the support in this lesson plan, the code in the resources document is properly commented. It is up to you how much you want to share with learners and how much you want them to explore themselves; this could be a good excercise for learners to search for documentation and how to implement the libraries.

Time For Practice

Learners will need some time to do two things for this lesson in order to achieve success: write the conditional statements and properly use the commands from the Tkinter library. The conditional statements should be straightforward, the only point to consider is the difference between one if statement and multiple elif statements or multiple if statements. There is an exercise in the workbook for learners to consider the impact of these two options on the efficiency of the code.

Unless learners have previously used the Tkinter library, implementation of the functions will take time. Remind learners about exploring documentation and explaining what the different parts of the code means, when asked: use the martypy documentation as the model. Learners need to apply their understanding to the unfamiliar Tkinter library so this should be a student-led exercise: be available to support and answer questions as and when they arise.

Cool Down

Bring learners back together to discuss the learning that occurred. Have groups share their thoughts about the challenge that was presented. Encourage every group to suggest what were key takeways that promoted success with their code.

Suggested questions you might ask:

  • How challenging was it to work with an unfamiliar library?
  • What helped you to effectively code with Tkinter?
  • If any part of the code was shared with the learners, did the comments from the code support your coding?
  • Did you write comments in your code to support others? If you did, what difference do you think they will make to someone reading what you wrote?

Carry out any end of lesson routines.

Log off devices and clear everything away.

Watch on YouTube
Watch on YouTube