Python Turtles are a great way to start kids in programming. Turtles offer a simple step-by-step graphical presentation that has tons of tutorials and examples.
Turtles can also be used on Raspberry Pi projects. In this blog I wanted to look at a Turtle example that reads a temperature sensor and graphically shows the result as an “old style” thermometer.
Python Turtles is probably already loaded on your system, if not enter:
pip install turtles
The turtle library opens a graphic screen with the very center of the screen being (0,0). This is a little different than many other graphic systems (like PyGame) where the top left is (0,0).
Different turtle objects can be defined and moved around the screen. A useful feature of turtles is that you can clear all the drawing from one turtle without effecting the what the other turtles have done. (Note: this is useful in this thermometer example where we can have static background turtle and a dynamic turtle that updates with new information).
Below is an example with 3 turtles. The first turtle (t1) is set to red and then moved forward, left, forward and then sent home. The second turtle (t2) is set to purple and given a turtle symbol. The third turtle (t3) is set to green and then moved to a position and a thick circle is drawn.
from turtle import * setup(500, 400) Screen() title(" 3 Turtles") # First red turtle goes forward, left, forward and back home t1=Turtle() t1.color("red") t1.forward(100) t1.left(90) t1.forward(100) t1.home() # Second purple turtle has a turtle shape t2=Turtle() t2.shape("turtle") t2.color("purple") t2.right(45) t2.forward(100) t2.left(90) t2.forward(100) # Third green turtle goes to a location and makes a thick circle t3=Turtle() t3.color("green") t3.pensize(10) t3.up() t3.goto(-100,-10) t3.down() t3.circle(80)
Drawing an “Old Style” Thermometer
For this project we wanted to draw an “old style” mercury thermometer, with a bulb of red mercury at the bottom and a tube above it.
Using the simple turtle commands like : move, left, right, forward etc. is great to learn but it can be awkward for more difficult drawings.
A more efficient approach is to define an array of x,y coordinates and then move to each position in the array. For example the upper tube can be drawn by:
# Define an array of x,y coordinates for the top tube outline = ((0,-50),(25,-50),(25,210),(-25,210),(-25,-50),(0,-50)) for pos in outline: # move to each tube x,y point thermo.goto(pos) thermo.pendown()
Circles are created using a turtle.circle(width) command. To fill an object or a group of objects a turtle.begin_fill() and a turtle.end_fill() set of command is used.
For our example the filled circle for the bulb is created by:
# put the pen up and move to the circle starting thermo.penup() thermo.goto(0,-137) thermo.pendown() thermo.pensize(5) thermo.color("black","red") # draw the circle with fill thermo.begin_fill() thermo.circle(50) thermo.end_fill()
The complete code to draw the complete thermometer background would be:
# Create a background for an "old style" thermometer from turtle import Turtle,Screen, mainloop import random, time # Define a Turtle object thermo = Turtle() thermo.penup() thermo.hideturtle() thermo.pensize(5) # Define an array of x,y coordinates for the top tube outline = ((0,-50),(25,-50),(25,210),(-25,210),(-25,-50),(0,-50)) for pos in outline: # move to each tube x,y point thermo.goto(pos) thermo.pendown() # put the pen up and move to the circle starting thermo.penup() thermo.goto(0,-137) thermo.pendown() thermo.pensize(5) thermo.color("black","red") # draw the circle with fill thermo.begin_fill() thermo.circle(50) thermo.end_fill() mainloop()
Raspberry Pi Hardware Setup
There are a number of different temperature sensors that can be used. For our example we used a low cost ($5) DHT11 temperature/humidity sensor. The DHT11 sensor that we used had 3 pins, (Signal, 5V and GND), and we wired the Signal pin to the Pi physical pin 7.
There is a DHT temperature/sensor Python library that is installed by:
sudo pip install Adafruit_DHT
A Python DHT test program would be:
#!/usr/bin/python import sys import Adafruit_DHT sensor_type = 11 # sensor type could also be 22, for DHT22 dht_pin = 4 # Note: BCM pin 4 = physical pin 7 humidity, temperature = Adafruit_DHT.read_retry(sensor_type, dht_pin) print( "Temp: ", temperature, " deg C") print( "humidity: ", humidity, " %")
Now for the final project we can start pulling things together.
For the thermometer project we used 2 turtles, a static background turtle (thermo) and a dynamic turtle (bar). The bar turtle is cleared and redrawn in the drawbar() function.
A screen object wn is used to resize the window and add a title.
For testing a random integer can be used. This is also useful for check the 0-40C range of the bar.
The full code and an screen shot are below:
from turtle import Turtle,Screen, mainloop import random, time import Adafruit_DHT # Update the temperature bar height and value def drawbar(temp): top = (-50 + 260 * temp/40) boutline = ((0,-50),(20,-50),(20,top),(-20,top),(-20,-50),(0,-50)) bar.penup() bar.clear() # clear the old bar and text bar.begin_fill() for pos in boutline: bar.goto(pos) bar.end_fill() bar.goto(30,top) bar.write(str(temp) + " C",font=("Arial",24, "bold")) # Setup a default screen size and Title wn = Screen() wn.setup(width = 500, height = 500) wn.title("RaspPi Temperature Sensor") # define a static thermo backgroup object and a dynamic bar object thermo = Turtle() thermo.penup() bar = Turtle() bar.color("red") bar.hideturtle() # define an array for the top tube outline = ((0,-50),(25,-50),(25,210),(-25,210),(-25,-50),(0,-50)) thermo.hideturtle() thermo.pensize(5) for pos in outline: thermo.goto(pos) thermo.pendown() # add some temperature labels thermo.penup() thermo.goto(50,-50) thermo.write("0 C") thermo.goto(50,210) thermo.write("40 C") # draw the filled bulb at the bottom thermo.goto(0,-137) thermo.pendown() thermo.pensize(5) thermo.color("black","red") thermo.begin_fill() thermo.circle(50) thermo.end_fill() # Update the temperature while True: humidity, temperature = Adafruit_DHT.read_retry(11, 4) # use a random number for testing #temp = random.randint(0,40) drawbar(temp) time.sleep(5)
Compared to other Python graphic libraries (like PyGame, Tkinter or Qt) Turtle graphics can be slow and perhaps limiting, but for kids Turtles projects are super easy to configure. If you are doing simple stuff Turtles requires a lot less code than the other graphic libraries (keyboard input is a good example of this).
There a lot of possible fun Raspberry Pi projects that can be done with Turtle. Some of the other projects that we have done include:
- use a Wii remote to draw pictures
- create a Turtle drawing as you drive a rover (show the path)