Programming with Python Turtle graphics
Turtles

© Lead Image © Baurzhan Moldabekov, 123RF.com
Create a bulb and tube analog thermometer with a Raspberry Pi and Python Turtle graphics.
Turtle graphics are a great way to get kids started programming. The Python turtle library offers simple step-by-step graphical methods that allow young programmers to create graphics with only a few lines of code.
I found that once my kids understood Turtle basics, we were able to do more advanced Raspberry Pi projects that used sensors, push buttons, and outputs. In this article, I share a Rasp Pi Turtle project that creates a graphic of an "old style" mercury thermometer animated with a Pi temperature sensor.
Getting Started
The Python turtle library is probably already loaded on your Raspberry Pi system, but if it is not, enter:
pip install turtles
The turtle library [1] is used a little bit as you would use a pen on a drawing board. With the use of methods (e.g., forward()
, backward()
, left()
, and right()
), you can draw lines. If you want to move a turtle without drawing a line, you use the penup()
method; then, once you arrive at the desired location, the pendown()
method lets you start drawing again. Turtle objects have properties for color and line thickness and a number of additional methods for shapes, fill, circles, and text.
To create a turtle object, you use the myturtle = Turtle()
statement. A new turtle is placed at the center of the screen, position (0,0), facing right.
The Python Turtle example in Listing 1 shows three basic concepts I use in this project: drawing lines, moving without drawing, and adding text (Figure 1).
Listing 1
turtle1.py
To draw t1
, named turtle
(line 7), the turtle (pen) rotates 90 degrees left to face toward the top of the drawing area (line 9). With a combination of left
and forward
methods, turtle t1
becomes an open rectangle (lines 9-14).
The t2
shape is red and drawn with a thick pen size (lines 18-20). The up()
method makes sure the pen does not draw while it moves (line 21-22). After reaching the new position, the pen goes back down to draw a circle (lines 23-24). The pen for turtle t3
goes to a new position without drawing a line and writes some text on the screen (line 27-31).
Once you (or your young programmer) have mastered drawing lines, moving without drawing, and adding text, you can start some more interesting projects.
Drawing a Thermometer
For this project, I wanted to draw an "old style" mercury thermometer with a bulb of red mercury at the bottom and a tube above it (Figure 2).
Simple turtle commands like move
, left
, right
, and forward
are great for drawing simple graphics, but they can be awkward if you want to create a more complex drawing. A more efficient approach is to define an array of (x, y) coordinates and move to each position in the array. For example, the upper tube can be drawn with:
# An array of coordinates for the tube outline = ((25,-50),(25,210),(-25,210),(-25,-50)) for pos in outline: # move to each tube x,y point t1.goto(pos)
To draw the lower "mercury" bulb in this project, I enhance the circle of my first example, turtle t2
, by creating a black circle with red fill. The fill is toggled with the begin_fill()
/end_fill()
methods:
# put the pen up and move to the circle starting point t2.penup() t2.goto(0,-137) t2.pendown() t2.pensize(5) t2.color("black","red") # draw the circle with fill t2.begin_fill() t2.circle(50) t2.end_fill()
At this point, I've created a static background for the thermometer. The next step is to read a temperature value from the Raspberry Pi and add dynamic information to the drawing.
Hardware Setup
A number of different temperature sensors can be used for this project. Here, I use a low-cost ($5) DHT11 temperature/humidity sensor [2]. This sensor has three pins, (signal, 5V, and GND; Figure 3). The signal pin connects to Rasp Pi physical pin 7 (PP7). The program in Listing 2 tests the DHT11 sensor. Note that because the sensor can return a humidity value, you could add that information in your Turtle code, as well (line 12).
Listing 2
test.py

To install the DHT temperature sensor Python library [3], enter:
sudo pip install Adafruit_DHT
When I'm doing Pi projects with my kids, I like to use prototyping expansion tops (Figure 4). These tops start at around $6 [4]. Not only do they make the wiring cleaner, it's a lot easier to move the projects around for testing.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News
-
KaOS 2022.06 Now Available With KDE Plasma 5.25
The newest iteration of KaOS Linux not only adds the latest KDE Plasma desktop but sets LibreOffice as the default.
-
Manjaro 21.3.0 Is Now Available
Manjaro “Ruah” has been released and includes the latest Calamares installer, GNOME 42, and much more.
-
SpiralLinux is a New Linux Distribution Focused on Simplicity
A new Linux distribution, from the creator of GeckoLinux, is a Debian-based operating system with a focus on simplicity and ease of use.
-
HP Dev One Linux Laptop is Now Available for Pre-Order
The System76/HP collaboration Dev One laptop, geared toward developers, is now available for pre-order.
-
NixOS 22.5 Is Now Available
The latest release of NixOS with a much-improved package manager and a user-friendly graphical installer.
-
System76 Teams up with HP to Create the Dev One Laptop
HP and System76 have come together to develop a new laptop, powered by Pop!_OS and aimed toward developers.
-
Titan Linux is a New KDE Linux Based on Debian Stable
Titan Linux is a new Debian-based Linux distribution that features the KDE Plasma desktop with a focus on usability and performance.
-
Danielle Foré Has an Update for elementary OS 7
Now that Ubuntu 22.04 has been released, the team behind elementary OS is preparing for the upcoming 7.0 release.
-
Linux New Media Launches Open Source JobHub
New job website focuses on connecting technical and non-technical professionals with organizations in open source.
-
Ubuntu Cinnamon 22.04 Now Available
Ubuntu Cinnamon 22.04 has been released with all the additions from upstream as well as other features and improvements.